coloring python debug output

method 1: bash script

vimcat (deprecated)

1
2
3
4
5
6
7
8
9
10
11
vimcat


~$ wget https://raw.githubusercontent.com/vim-scripts/vimcat/master/vimcat vimcat
~$ chmod +x vimcat
~$ mv vimcat /usr/share/bin
~$ echo $PATH
/usr/bin:/usr/sbin etc.

~$ source ~/.bashrc
~$ vimcat monitorRAM.sh

upload successful

nice!
thanks https://stackoverflow.com/a/34029108/851192

1
2
~$ vi testcoloring.sh 
~$ source ./testcoloring.sh; colorized calibrate.py

upload successful

My answer: https://stackoverflow.com/a/59507107/851192
Note that this does take noticeably more time to run!

1
2
3
4
5
6
7
8
9
$ time colorized calibrate.py 
real 0m0.484s
user 0m0.392s
sys 0m0.085s

rui@chaiX1YG2:~$ $ time python calibrate.py
real 0m0.343s
user 0m0.271s
sys 0m0.072s

bashrc (inuse)

The slowdown is a bit annoying. Let’s do something super lightweight.

https://stackoverflow.com/a/20910449/851192

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
rui@chaiX1YG2:~/project_academic/throwdini$ (mergePrevWork)$ time copython calibrate.py 
[2019-12-27 19:25:40,310] {calibrate.py:30} WARNING - hi
('Calibrating using this many grid points: ', 6)
Connecting to robot...
Traceback (most recent call last):
File "calibrate.py", line 82, in <module>
robot = Robot(False, None, None, workspace_limits,
NameError: name 'Robot' is not defined

real 0m0.397s
user 0m0.355s
sys 0m0.048s
rui@chaiX1YG2:~/project_academic/throwdini$ (mergePrevWork)$ time cpython^C
rui@chaiX1YG2:~/project_academic/throwdini$ (mergePrevWork)$ time python calibrate.py
[2019-12-27 19:25:45,521] {calibrate.py:30} WARNING - hi
('Calibrating using this many grid points: ', 6)
Connecting to robot...
Traceback (most recent call last):
File "calibrate.py", line 82, in <module>
robot = Robot(False, None, None, workspace_limits,
NameError: name 'Robot' is not defined

real 0m0.403s
user 0m0.307s
sys 0m0.096s
1
2
3
4
5
6
7
8
9
norm="$(printf '\033[0m')" #returns to "normal"
bold="$(printf '\033[0;1m')" #set bold
red="$(printf '\033[0;31m')" #set red
boldred="$(printf '\033[0;1;31m')" #set bold, and set red.

coloredpython() {
python $@ 2>&1 | sed -e "s/Traceback/${boldred}&${norm}/g" # will color any occurence of someregexp in Bold red

alias python=coloredpython

method 2: directly in python script

as per https://stackoverflow.com/a/52797444/851192
using ipython
(have to add snippet to top of file!

colors: https://unix.stackexchange.com/questions/124407/what-color-codes-can-i-use-in-my-ps1-prompt

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

upload successful

https://stackoverflow.com/a/28938235
list of colors: https://stackoverflow.com/a/54649803

https://askubuntu.com/a/17304

1
2
3
4
5
6
7
8
9
The colour code consists of three parts:

The first part before the semicolon represents the text style.
00=none, 01=bold, 04=underscore, 05=blink, 07=reverse, 08=concealed.

The second and third part are the colour and the background color:
30=black, 31=red, 32=green, 33=yellow, 34=blue, 35=magenta, 36=cyan, 37=white.

Each part can be omitted, assuming starting on the left. i.e. "01" means bold, "01;31" means bold and red. And you would get your terminal to print in colour by escaping the instruction with \33[ and ending it with an m. 33, or 1B in hexadecimal, is the ASCII sign "ESCAPE" (a special character in the ASCII character set). Example

wow makes so much more sense now!!!

print in colour by escaping the instruction with \33[ and ending it with an m. 33, or 1B in hexadecimal, is the ASCII sign “ESCAPE” (a special character in the ASCII character set).

colored_python

https://pypi.org/project/colored-traceback/
didn’t like the coloring… I really just want to see where hte traceback starts

logger in python3!

https://stackoverflow.com/a/56944256/851192

one way to use logger

1
2
3
4
5
6
7
8
9
10
11
12
logging.basicConfig(stream=sys.stdout, level=logging.WARNING,
format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')

if sys.stderr.isatty():
logging.addLevelName(
logging.WARNING, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName(
logging.ERROR, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.ERROR))


logger = logging.getLogger('Calibration Logger')
logger.warning('hi')