Saturday, 1 March 2014

Debugging with colour in Python

It's the little things that make life worth living, debugging with colour for example:

  

#!/usr/bin/python

debug_level = 3

def debug(level, string):  
    level_char=["+", "*", "**", "***"]
    level_colour=['\033[1;96m', '\033[1;92m', '\033[1;93m', '\033[1;91m']
  
    if (level <= debug_level):
        print " %(level_colour)s[%(level_char)s]\033[0;39m\t%(string)s" % \
            {"level_colour": level_colour[level], \
            "level_char": level_char[level], \
            "string": string}  

def main():
    debug(0, "General information")
    debug(1, "Debug 1")
    debug(2, "Debug 2")
    debug(3, "Debug 3")
  
          
if __name__ == "__main__":
    main()


No comments:

Post a Comment