Thursday, August 14, 2008

Python STDOUT Colors Script

This is a great script I use for debugging and/or general stdout colorization when working with python.If you run it from console with no parameters it loops through stdout colors. Displaying them with the string that represents that color. Notice: Some color codes may come out different than what they appear. It's very useful to import inside your other scripts and print your output in color. I though it was great. Makes for debugging large amounts of data a snap. Maybe someone else will find it useful as well. Enjoy!

This script is also located at:
code.google.com/p/python-stdout-colors/

Download PY!
Tell me what you think, leave a comment.

Runnable from terminal:
chmod +x stdout_colours.py
python stdout_colours.py

Use it in your code like this:

self.soc.write(["printing','a','list'],"red")
self.soc.write("printing a string","green")
self.soc.write({"printing":"dictionary","testing":"fun"},'blue')
self.soc.write(("printing","a","tuple"),'yellow')

Add it to your functions like this:

import stdout_colours

class some_class(object):
def __init__(self):
self.testing="fun"
self.func_me_color="white_on_blue"
self.soc=stdout_colours.stdout_colors()
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.write("doing something:","red")
self.do_something()
self.soc.me_him(['EXIT:',__name__],self.func_me_color)

def do_something(self):
self.soc.me(['ENTER:',__name__],self.func_me_color)
self.soc.write("doing something else:","green")
self.do_something_else()
self.soc.me(['EXIT:',__name__],self.func_me_color)

def do_something_else(self):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.write(['testing','is',testing],"yellow")
self.soc.me_him(['EXIT:',__name__],self.func_me_color)


EDIT: I actually like to use this over print when I deal with terminal/console apps. Much easier to tell what is going on when text is scrolling by so fast.
I hope this helps someone. Leave a comment. Enjoy.

No comments: