(python)彩色打印与装饰功能 [英] (python) colour printing with decorator in a function
问题描述
如何装饰一个函数,以便它打印到 stdout
的任何内容都处于绿色以及打印到 stderr
处于红色?我有 termcolor
模块可用。
奖金业力:如何将参数传递给装饰器来指定颜色,默认为红色和绿色?
一个有趣的问题。最简单的解决方案与Pete的建议类似。在将函数运行到stderr和stdout之前,只需打印转义码即可。但是,如果stderr和stdout都是通过同一个终端进行传输的话,那么它们就会干扰。
因此,另一种解决方案是将stdout和stderr用一个微小的包装器在每次写入期间启用颜色,只有当我们在终端(而不是管道)时才需要这样做。
#!/ usr / bin / python2
import sys
def colorize(stdoutColor,stderrColor):
defaultColor ='\\ \\ 033 [0; 0m'
def applyColorize(f):
class colorWrapper(object):
def __init __(self,wrapee,color):
self .wrapee = wrapee
self.color = color
def __getattr __(self,attr):
if attr =='write'and self.wrapee.isatty():
return lambda x:self.wrapee.write(self.color + x + defaultColor)
else:
return getattr(self.wrapee,attr)
def wrapper(* args, ** kwds):
oldStdout = sys.s tdout
oldStderr = sys.stderr
sys.stdout = colorWrapper(oldStdout,stdoutColor)
sys.stderr = colorWrapper(oldStderr,stderrColor)
try:
f(* args,** kwds)
finally:
sys.stdout = oldStdout
sys.stderr = oldStderr
返回包装
返回applyColorize
greenColor ='\033 [01; 32m'
redColor ='\033 [01; 31m'
def foo() :
print我很平凡,很无聊!
print>> sys.stderr,'写给stderr!'
@colorize(greenColor,redColor)
def colorFoo():
print我色彩斑斓,令人兴奋!
print>> sys.stderr,'写入stderr!'
if __name__ =='__main__':
foo()
colorFoo()
foo()
这可以稍微磨光一些,但在大多数情况下它应该可以完成这项工作,并且可以正确地清理完毕。当然,请记住我正在使用特定于shell的转义码。如果您需要便携性,则必须将呼叫转义码替换为便携式终端控制模块。
How can I decorate a function so that anything it prints to stdout
is in green and anything it prints to stderr
is in red? I have the termcolor
module available.
Bonus karma: How can I pass arguments to the decorator to specify the colours, defaulting them to red and green?
An interesting question. The simplest solution would be similar to what Pete suggest. Just print the escape codes before running the function to each of stderr and stdout. However, if both stderr and stdout feed the same terminal, as is usually the case, they will interfere.
So, an alternative solution is to monkey-patch stdout and stderr with a tiny wrapper that enables the color for the duration of each write, taking care to do this only if we're in a terminal (and not piped).
#!/usr/bin/python2
import sys
def colorize(stdoutColor, stderrColor):
defaultColor = '\033[0;0m'
def applyColorize(f):
class colorWrapper(object):
def __init__(self, wrapee, color):
self.wrapee = wrapee
self.color = color
def __getattr__(self, attr):
if attr == 'write' and self.wrapee.isatty():
return lambda x: self.wrapee.write(self.color + x + defaultColor)
else:
return getattr(self.wrapee, attr)
def wrapper(*args, **kwds):
oldStdout = sys.stdout
oldStderr = sys.stderr
sys.stdout = colorWrapper(oldStdout, stdoutColor)
sys.stderr = colorWrapper(oldStderr, stderrColor)
try:
f(*args, **kwds)
finally:
sys.stdout = oldStdout
sys.stderr = oldStderr
return wrapper
return applyColorize
greenColor = '\033[01;32m'
redColor = '\033[01;31m'
def foo():
print "I'm ordinary and boring!"
print >> sys.stderr, 'Writing to stderr!'
@colorize(greenColor, redColor)
def colorFoo():
print "I'm colorful and exciting!"
print >> sys.stderr, 'Writing to stderr!'
if __name__ == '__main__':
foo()
colorFoo()
foo()
This can still be polished a bit, but it should do the job in most cases and cleans up after itself properly. Of course, keep in mind I'm using shell-specific escape codes. If you want portability, you'll have to replace the escape code writes with calls to a portable terminal control module.
这篇关于(python)彩色打印与装饰功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!