• Stars
    star
    377
  • Rank 113,535 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small utility to pretty-print Python tracebacks. ⛺

TBVaccine

TBVaccine is a utility that pretty-prints Python tracebacks. It automatically highlights lines you care about and deemphasizes lines you don't, and colorizes the various elements in a traceback to make it easier to parse.

Here are some screenshots. This is the before:

misc/before.png

And this is the after:

misc/after.png

If you add the hook or call TBVaccine in your code, it can also print all variables in each stack frame. That is, it turns this:

misc/before-vars.png

into this:

misc/after-vars.png

Installation

To install, use pip:

pip install tbvaccine

You are done!

Global usage

You can have TBVaccine insert itself all up in your system and stick its tentacles in all your libraries, like a cute, useful Cthulhu. That way, every single Python traceback in your system will be pretty. Just set the TBVACCINE environment variable to 1, and you're done.

E.g. for bash:

export TBVACCINE=1

Or fish:

set -x TBVACCINE=1

If you want to prettify tracebacks even when stderr is not a tty, set TBVACCINE_FORCE to 1:

export TBVACCINE=1
export TBVACCINE_FORCE=1
python -c '1/0' 2>&1 | cat  # pretty!

NOTE: If you're on Ubuntu, you most likely have Apport installed, which overrides TBVaccine's hook with its own. To disable Apport for Python, delete a file named /etc/python<version>/sitecustomize.py. Note that this will disable Apport for Python, and you won't be asked to submit info to Ubuntu when Python programs crash any more. For some, this is a good thing.

Usage as a command-line utility

TBVaccine can be used from the command line several ways.:

python -m tbvaccine myscript.py

Or just pipe STDERR into it from the program you want to watch:

./myscript.py 2>&1 | tbvaccine

And all the tracebacks will now be pretty!

Usage as a Python library

There are various ways to use TBVaccine as a Python library.

Initialize it like so:

from tbvaccine import TBVaccine
tbv = TBVaccine(
    code_dir="/my/code/dir",
    isolate=True
)

code_dir marks the directory we code about. Files under that directory that appear in the traceback will be highlighted. If not passed, the current directory, as returned by os.getcwd() will be used.

If isolate is False, all lines are colorized, and code_dir is ignored.

If show_vars is False, variables will not be printed in each stack frame.

To use it in an except block:

from tbvaccine import TBVaccine
try:
    some_stuff()
except:
    print(TBVaccine().format_exc())

To make it the default way of printing tracebacks, use add_hook() (which also accepts any argument the TBVaccine class does):

import tbvaccine
tbvaccine.add_hook(isolate=False)

1 / 0

Bam! Instant pretty tracebacks.

Logging integration

You can integrate TBVaccine with logging like so:

class TbVaccineFormatter(logging.Formatter):
    def  formatException(self, exc_info):
        return TBVaccine(isolate=True).format_exc()

sh = logging.StreamHandler()
sh.setFormatter(TbVaccineFormatter('[%(levelname)s] %(asctime)s : %(message)s', '%Y-%m-%d %H:%M:%S'))
logger.addHandler(sh)

Configuration

To configure TBVaccine, open its configuration file in ~/.config/tbvaccine/tbvaccine.cfg (or your operating system's equivalent) and edit it. You can currently configure the color style there by specifying one of the Pygments styles <http://pygments.org/demo/6778888/?style=monokai>.

Epilogue

This library is still pretty new, please contribute patches if something doesn't work as intended, and also please tell your friends about it! Hopefully one day it will be implemented in the Python interpreters themselves.

-- Stavros

More Repositories

1

catt

Cast All The Things allows you to send videos from many, many online sources to your Chromecast.
Python
3,314
star
2

shortuuid

A generator library for concise, unambiguous and URL-safe UUIDs.
Python
1,905
star
3

django-annoying

A django application that tries to eliminate annoying things in the Django framework. ⛺
Python
918
star
4

expounder

A library for explaining things in HTML.
JavaScript
552
star
5

django-loginas

"Log in as user" for the Django admin.
Python
355
star
6

python-yeelight

A Python library for controlling YeeLight WiFi RGB bulbs (mirror of https://gitlab.com/stavros/python-yeelight).
Python
266
star
7

goatfish

A small, schemaless Python ORM that is backed by SQLite. ⛺
Python
233
star
8

netproxy

A Netflix/Hulu/Pandora/etc proxy in a box.
Python
230
star
9

python-fuse-sample

A sample FUSE filesystem in Python.
Python
178
star
10

django-project-template

The Django project template I use, for installation with django-admin.
Python
162
star
11

gr8w8upd8m8

Gr8W8Upd8M8 is a script to automatically read your weight from a Wii Balance Board. ⛺
Python
145
star
12

sysaidmin

A GPT-powered sysadmin.
Python
136
star
13

imdbapi

An API for the IMDB site. ⛺
Python
131
star
14

A6lib

An ESP8266/Arduino library for communicating with the A6 GSM module. ⛺
C++
127
star
15

jsane

A saner way to traverse JSON in Python
Python
126
star
16

encbup

Encrypted backups (without the backups)
Python
121
star
17

tiny-ESP8266-breakout

A very small and basic ESP8266 breakout board
111
star
18

omnisync

omnisync is a universal file synchroniser and backup program (think rsync) that supports multiple transport systems (such as plain files, sftp, s3 and virtual, currently, and support for ftp, http, et al is planned). It is designed to be fast, small, extensible, portable and bandwidth-efficient. It is available for Linux, Windows and Mac. ⛺
Python
87
star
19

iRotary

One man's quest to turn an old rotary phone into a mobile phone.
Arduino
83
star
20

django-cloudflare-push

A piece of middleware that uses Cloudflare's HTTP/2 Push to push static media to the clients. ⛺
Python
74
star
21

gweet

A message queue for the abhorrently named Internet of Things
Go
70
star
22

static-appengine-hoster

An App Engine application for hosting static sites under multiple domains in a single app.
Python
66
star
23

django-tokenauth

Django authentication backend that uses tokens sent over email.
Python
63
star
24

arduino-irrigation

A GSM-based Arduino remote controller for an irrigation system.
Arduino
62
star
25

esplights

A home automation sensor and controller based on an ESP8266.
Arduino
48
star
26

yeecli

A command-line utility for a YeeLight RGB lamp. (mirror of https://gitlab.com/stavros/yeecli)
Python
42
star
27

episode-renamer

Episode renamer is a simple python script that renames folders of TV episode video files to their proper names.
Python
41
star
28

kicad-lib

My KiCad components library.
35
star
29

Spamnesty

A service that tries to have some fun with spam (mirror of https://gitlab.com/stavros/Spamnesty/)
Python
33
star
30

captainwebhook

Captain Webhook eats webhooks and executes commands.
Python
30
star
31

spamgpt

SpamGPT, a bot that wastes spammers' damn time, like they waste mine.
Python
28
star
32

pastebinit

pastebinit is a command-line tool to send data to a "pastebin", a web site which allows its users to upload snippets of text for public viewing. ⛺
Python
27
star
33

ez-openai

Ez API, ez life.
Python
23
star
34

apache-config

A script for configuring apache with Django+wsgi/fcgi+virtualenv+etc.
Python
23
star
35

docker-fgallery

A Dockerfile for installing fgallery. ⛺
Shell
18
star
36

ArduiRC

Remotely control IR/RF devices from a computer with Arduino.
Arduino
18
star
37

landing-page

A very simple landing page app for AppEngine.
Python
15
star
38

stringphone

String phone is a secure communications protocol and library geared towards embedded devices.
Python
14
star
39

bakeit

A command-line utility for pastery, the best pastebin in the world.
Python
12
star
40

pythess-files

The code files/presentations/everything else used in the PyThess meetup
Jupyter Notebook
11
star
41

lektor-shortcodes

A shortcodes plugin for lektor
Python
11
star
42

shufflecast

A TV channel with all your favorite shows.
Python
11
star
43

photocopy

A script to archive photos off a camera to a directory.
Python
10
star
44

nxt-python

NXT-Python is a python driver/interface for the Lego Mindstorms NXT robot based on NXT_python.
Python
10
star
45

mediacenter-in-a-box

A media center in a box, compatible with Harbormaster for one-line deployments.
Python
10
star
46

Reverend

A clone of Reverend, the naive Bayesian classifier written in Python.
Python
10
star
47

A6-ESP8266-breakout

A breakout board that includes an ESP8266 and an A6 GSM module, for easily making GSM-enabled applications (mirror)
Shell
9
star
48

lektor-thumbnail-generator

A thumbnail generator for Lektor content
Python
9
star
49

pastery.vim

A Vim plugin for the sweetest pastebin in the world, pastery.net.
Vim Script
9
star
50

Pythia

High quality & fast "true random" number generator (shuffling system)
JavaScript
8
star
51

apt-btrfs-snapshot

A fork of apt-btrfs-snapshot.
Python
8
star
52

gamelights

An RGB LED strip WiFi controller, complete with code and PCB designs.
KiCad Layout
8
star
53

pylast

A clone of Amr Hassan's pylast: a python interface to last.fm (and other api-compatible websites)
Python
6
star
54

awesomeblog-club

An easily self hostable curation site for collecting blogs and links.
TypeScript
5
star
55

django-project-templates

Some Django project templates for various things.
4
star
56

ChatGPT-Bot

A small Python library that makes it easier to write a ChatGPT bot.
Python
4
star
57

requests-guard

requests-guard is a small library that allows you to impose size and time limits on your HTTP requests.
Python
4
star
58

rust-bakeit

A command-line client for pastery.net, the best pastebin in the world (official mirror of https://gitlab.com/stavros/rust-bakeit).
Rust
3
star
59

cat-feeder

A cat feeder with a lid that can open and close over wifi.
C++
3
star
60

the-spam-chronicles

The Spam Chronicles, a static website where I post all the spam I got and SpamGPT auto-replied to.
Python
2
star
61

progress

A simple progress bar in Python
Python
2
star
62

back-scratcher

A back scratching robot!
Python
2
star
63

blacklist-pre-commit-hook

A pre-commit hook for blacklisting certain functions
Python
2
star
64

harbormaster

The tiniest container orchestrator.
Python
2
star
65

strofara

A thing
C++
2
star
66

zulipbot

A ChatGPT Zulip bot
Python
1
star
67

iphoneconverter

A small script to convert a folder of videos into an iPhone format.
1
star
68

reddit-rage-faces

An Opera extension to put ragefaces in every subreddit.
JavaScript
1
star