Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
colorize
# -*- coding: utf-8 -*-
def colorize(*args):
def getColor(color):
return {
'black' : '\033[30m',
'red' : '\033[31m',
'green' : '\033[32m',
'orange' : '\033[33m',
'blue' : '\033[34m',
'purple' : '\033[35m',
'cyan' : '\033[36m',
'light_grey' : '\033[37m',
'dark_grey' : '\033[90m',
'light_red' : '\033[91m',
'light_green' : '\033[92m',
'yellow' : '\033[93m',
'light_blue' : '\033[94m',
'pink' : '\033[95m',
'light_cyan' : '\033[96m'
}.get(color, "")
def _colorize(func):
def wrapper():
RESET = '\033[0m'
text = func()
if not isinstance(text, basestring):
text = str(text)
level = text.split(':', 1)[0]
color = {
'SUCCESS': args[0],
'WARNING': args[1],
'ERROR': args[2]
}.get(level, '')
return "{0}{1}{2}".format(getColor(color), text, RESET)
return wrapper
return _colorize
level = 1
@colorize("green", "orange", "red")
def test():
global level
if level == 1:
return "SUCCESS: level1"
elif level == 2:
return "WARNING: level2"
elif level == 3:
return "ERROR: level3"
else:
return "nothing happens"
if __name__ == '__main__':
print test()
level += 1
print test()
level += 1
print test()
level += 1
print test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.