Colored python debugging output / errors / tracebacks (in bash)

The illegibility of python tracebacks has bugged me for a while, so I finally got around to adding color to my terminal (bash) output.

Basically, the “lightest” way I found is to redirect the python stdout output through a sed script that injects some color codes before spitting the output back to your terminal (and then to your eyes).

My system is Ubuntu 18.04, in bash. Not sure how it’d translate over to Mac or Windows — it relies on these annoying color escape codes. (according to internet, should work on OSX and Windows terminals)

How

Add to your ~/.bashrc :

norm="$(printf '\033[0m')" #returns to "normal"
bold="$(printf '\033[0;1m')" #set bold
red="$(printf '\033[0;31m')" #set red
boldyellowonblue="$(printf '\033[0;1;33;44m')"
boldyellow="$(printf '\033[0;1;33m')"
boldred="$(printf '\033[0;1;31m')" #set bold, and set red.

copython() {
        python $@ 2>&1 | sed -e "s/Traceback/${boldyellowonblue}&${norm}/g" \
        -e "s/File \".*\.py\".*$/${boldyellow}&${norm}/g" \
        -e "s/\, line [[:digit:]]\+/${boldred}&${norm}/g"
    }

Then to use it, run

$ source ~/.bashrc # reload file without restarting terminal
$ copython myprogram.py # copython = colored python

Originally, the program outputs:

With `copython`it now outputs

Based on:

https://stackoverflow.com/questions/20909942/color-linux-command-output/20910449#20910449

Mostly, I just tacked on more sed regex’s, and changed the colors around a bit.

The result is actually kinda inverted from a UX standpoint — probably want the actual code highlighted more than the file / line number — but we’re reaching the limits of my patience with regex’s here.

This is fairly lightweight but definitely still tacks on some time! (My regex’s are not very efficient I’m sure). Fortunately easy to switch between `copython` and `python` if concerned πŸ™‚

Explanation: ANSI Color Codes

Note that \033[ marks the beginning of a code, which helped me understand a bit better what is going on.

0m # normal 
0;1m # bold 
0;1;33;44m # bold yellow on blue 
0;1;33m # bold yellow 
0;1;31m # bold red

So I guess the first 1 indicates bold, then the second 33 indicates foreground color, and the third 44 indicates background color. Something like that.

I think there’s eight main possible colors, going from 30 to 37 for foreground, and 40 to 47 for background colors.

# Foreground Regular Colors 
30m' # Black 
31m' # Red 
32m' # Green 
33m' # Yellow 
34m' # Blue 
35m' # Purple 
36m' # Cyan 
37m' # White 

# Background Regular Colors 
40m' # Black 
41m' # Red 
42m' # Green 
43m' # Yellow 
44m' # Blue 
45m' # Purple 
46m' # Cyan 
47m' # White

Alternatives: Vimcat

Pipe it through vim!Β  Specifically my step-by-step answer is at the bottom. Unfortunately, this both (a) requires installing things and (b) is slow. See before and after in this image:

CON: It’s noticeably slow.

Alternatives: IPython

import sys
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose', color_scheme='Linux', call_pdb=False)

CON: Have to edit every single python file! Also incredibly slow.

[END]

 

 

Notes to self

next post todo: cover coloring output of python logger.py module

https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output

specifically this Β 

sneak preview:

I still don’t fully understand the nesting of loggers — you can see ‘white’ output from some other logger being called by the main file. (also a few print() messages I haven’t converted yet).

linkback to hexblog draft of this post

QWERTY keyboard made of barcodes

Played briefly with barcodes yesterday –
There was a usb barcode scanner lying around MITERS which Michael handed me, you plug it in and it behaves like a keyboard.

See video, which demonstrates scanning what I was hoping would be a backspace (but is actually reading as Ctrl-H), and then scanning a normal barcode on a box lying around.

(also hosted here: https://photos.app.goo.gl/GKBDrtjNtJhHXu448)

Anyway, so of course we have the terrible idea of a barcode keyboard, which given that I need to urgently work toward my quals exam at the end of January, seemed like a perfect thing to waste time on (short brainless self-contained project). Of course after the first step I made it more convoluted and worked on it for several hours… but anyway.

I present: The barcode keyboard! 😄

Video of it in action here, starring expert scanner Mike:

(also hosted here: https://photos.app.goo.gl/Cv4DfEJTbat5KwGT6).

And the source files:

The barcodes were generated using a website: https://barcode.tec-it.com/en/Code128? (Note that can only create five at a time).

That’s it! I should’ve stopped there! But…

Backspace

Well I noticed that my backspace barcode key doesn’t work :’( Despite checking β€œevaluate escape expression” on the barcode generator.

After some debugging (detailed below) I realized that my computer thinks I’m presseing β€œCtrl-H” (^H) which is what used to be backspace presumably on some ancient terminal system.

Page 17 (shown on left side of screenshot) lists the codes: https://www.tec-it.com/download/PDF/Barcode_Reference_EN.pdf

So, getting a backspace working is for future work.

Sunglasses

Of course, along the way as I was looking at ASCII tables, I realized there are some extended ASCII codes that look kinda like I might be able to piece together some of the emoji (? unicode art ish?) online. Specifically, I wanted to be able to scan a barcode and get out

( β€’_β€’)
( β€’_β€’)>βŒβ– -β– 
(βŒβ– _β– )

Source: https://gist.github.com/cheeaun/3720871#file-putonglasses-txt-L2

So the ASCII equivalents aren’t a perfect match, but the closest I got was:

( Β·_Β·)
( Β·_Β·)>β”Œβ– -β– 
( β”Œβ– -β– )

After looking up the ASCII codes, I entered into the barcode generator
(mentioned above) the keycode

\250_\250 )
(   \250_\250 ) > \218\254-\254
( \218\254_\254)

And I stuck the outputs into a word editor to get

 

But when I went to scan it, I only got out

_ )
(   _ ) > -
( _)
 _  )
 _  )

Sad :’(

So, getting special things is left for future steps. But it’s neat to know that I can do weird things like Ctrl-H just from scanning a barcode.

The rest of the blog post will mostly be notes from my debugging process.

Debug

To get closer to what the barcode scanner was β€œactually” outputting and to compare it to the difference from me pressing the key, I looked at

Xev and Xinput

$ dmesg | tail
[340287.860812] usb 3-2: new full-speed USB device number 6 using xhci_hcd
[340293.199985] usb 3-2: New USB device found, idVendor=05e0, idProduct=1200, bcdDevice= 2.01
[340293.199989] usb 3-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[340293.199993] usb 3-2: Product: Symbol Bar Code Scanner
[340293.199995] usb 3-2: Manufacturer: οΎ©Symbol Technologies, Inc, 2002
[340293.199997] usb 3-2: SerialNumber: S/N:F37202D96BE67D41B4B4CF7B41E2A6B8 Rev:NBRMIAAS3
[340293.203189] input: οΎ©Symbol Technologies, Inc, 2002 Symbol Bar Code Scanner as /devices/pci0000:00/0000:00:14.0/usb3/3-2/3-2:1.0/0003:05E0:1200.0007/input/input24
[340293.265599] hid-generic 0003:05E0:1200.0007: input,hidraw0: USB HID v1.10 Keyboard [οΎ©Symbol Technologies, Inc, 2002 Symbol Bar Code Scanner] on usb-0000:00:14.0-2/input0
chai@W530:/dev$ $ xinput list
⎑ Virtual core pointer                    	id=2	[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad              	id=12	[slave  pointer  (2)]
⎜   ↳ TPPS/2 IBM TrackPoint                   	id=13	[slave  pointer  (2)]
⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
    ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
    ↳ Power Button                            	id=6	[slave  keyboard (3)]
    ↳ Video Bus                               	id=7	[slave  keyboard (3)]
    ↳ Video Bus                               	id=8	[slave  keyboard (3)]
    ↳ Sleep Button                            	id=9	[slave  keyboard (3)]
    ↳ Integrated Camera: Integrated C         	id=10	[slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard            	id=11	[slave  keyboard (3)]
    ↳ ThinkPad Extra Buttons                  	id=14	[slave  keyboard (3)]
    ↳ οΎ©Symbol Technologies, Inc, 2002 Symbol Bar Code Scanner	id=15	[slave  keyboard (3)]
chai@W530:/dev$ $ xinput test 15

chai@W530:/dev$ $ xev

Example output

The “q” key:

$ xev
KeyPress event, serial 37, synthetic NO, window 0x5200001,
    root 0x181, subw 0x0, time 341559163, (55,-13), root:(1347,511),
    state 0x0, keycode 24 (keysym 0x71, q), same_screen YES,
    XLookupString gives 1 bytes: (71) "q"
    XmbLookupString gives 1 bytes: (71) "q"
    XFilterEvent returns: False


$ xinput test 11
key press   24
key release 24

Wow. Much confusion —

What are all these numbers?

More on keycodes:
https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

β€œQ” is 81 there because it is ASCII 81 (in decimal). But why is it 24 in xinput, and 71 in xev??

E.g. backspace key
In xinput list,

key press   22 
key release 22

In xev,

KeyRelease event, serial 37, synthetic NO, window 0x4400001,
    root 0x181, subw 0x0, time 347196546, (114,-9), root:(1504,380),
    state 0x0, keycode 22 (keysym 0xff08, BackSpace), same_screen YES,
    XLookupString gives 1 bytes: (08) "
    XFilterEvent returns: False

But also these ways??

\8
\b
^H

The last is the issue – gets interpreted as Ctrl-H.

Looking around I didn’t see much to help me.
https://unix.stackexchange.com/questions/49650/how-to-get-keycodes-for-xmodmap

Specifically, https://unix.stackexchange.com/a/49651

There are a lot of players between your keyboard and the process that finally handles the keyboard event. Among the major pieces of the landscape are the fact that the X system has its own keyboard-handling layer, and X associates different β€œkeycodes” with keys than your Linux base system does. The showkey command is showing you the keycodes in Linux-base-system lingo. For xmodmap you need the X keycodes, which are what xev is displaying. So long as you’re planning to work in X and do your key rebinding with xmodmap, then, ignore showkeys and just listen to what xev says.

Of course, being able to scan a barcode and hit β€œctrl-h” makes one wonder… can you make malicious barcodes? Turns out someone on the intenret has looked into this: http://www.irongeek.com/xss-sql-injection-fuzzing-barcode-generator.php

Future

  • Get another barcode scanner, dual wielding! Play Doom or something with barcode scanners XD
  • Get backspace working. The second barcode scanner, hopefully from different manufacturer, can help tell me if it’s something weird about this particular scanner such that it’s outputting Crl-H instead of backspace.
  • Get sunglasses emoji barcode working
  • Understand more about the barcode systems