Lately I find myself writing a lot of python scripts that involves a ton of text flying by, and me inventing increasingly creative combinations of obscure symbols to delineate different portions of the output. Luckily, if you’re not using windows using a sensible operating system, then you probably have an underappreciated tool at your disposal– bash! and it’s oh-so-exciting escape codes for colourized output.
so last night i wrote a really little module to do two things: a) colourize output of entire lines, and b) colourize output of any matching regular expressions within a string. it took less time to put this together than the time i was spending squinting at my screen
. hopefully it will be a useful starting point for others doing the same.
grab the file here. click the image to see basic operation.
Tags: bash, color, escape codes, print, python
5 Comments
That magenta is not.
you dare to doubt my colour naming scheme?! (actually, it totally wasnt mine, and, i agree
. i wonder if it would be better on another terminal.).
Very cool Jessy. I just started getting into python, so thanks for sharing!
The trick is best described as terminal-specific rather than a bashism. For best practice when using this technique to dump data analytics I think a scheme has to be developed for switching between screen dumping (viewing data in real time on the terminal) and viewing in other tools–those color codes are not so fun when you are trying to view the text in emacs or less.
I recently found this handy trick that accomplishes something similar by auto overriding the behavior of sys.stdout to indent output to show how deep your print statement is in the call stack. Most of the time it works effortlessly, and even in those cases where it does not, it’s pretty simple to hack up the logic of indent_level() to fit your code.
Works on windows too!
http://code.activestate.com/recipes/411791/