Colourizing Python Print Statements

By Jessy

python_colour_outputLately 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: , , , ,

5 Comments

  1. Ben McGraw commented on May 19, 2009 | Permalink

    That magenta is not.

  2. Jessy commented on May 19, 2009 | Permalink

    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.).

  3. Nick Peters commented on May 23, 2009 | Permalink

    Very cool Jessy. I just started getting into python, so thanks for sharing!

  4. SetJmp commented on August 27, 2009 | Permalink

    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.

  5. Stefan commented on August 30, 2009 | Permalink

    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/

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Archives

  • January 2010
  • November 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • Site Feeds

    Posts
    Comments

    Marginal Structure Posts RSS feed

    Site Tags