class Logger(object):
def __init__(self, colour):
self.colour = colour
def log(self, msg):
print(self.colour + ": " + msg)
def logger_choice(colour):
def col_decor(f):
def f_decor(*args, **kwargs):
global dprint
prev_dprint = dprint
dprint = loggers[colour].log
f(*args, **kwargs)
dprint = prev_dprint
return f_decor
return col_decor
def f():
dprint("this should be black")
@logger_choice(colour='yellow')
def fu(x):
dprint("this should be yellow, and 5 = %d" % x)
@logger_choice(colour='red')
def fun():
dprint("this should be red")
loggers = {'black': Logger("black"), 'yellow': Logger("yellow"), 'red': Logger("red")}
dprint = loggers['black'].log
f()
fu(5)
fun()
Y2xhc3MgTG9nZ2VyKG9iamVjdCk6CglkZWYgX19pbml0X18oc2VsZiwgY29sb3VyKToKCQlzZWxmLmNvbG91ciA9IGNvbG91cgoJCglkZWYgbG9nKHNlbGYsIG1zZyk6CgkJcHJpbnQoc2VsZi5jb2xvdXIgKyAiOiAiICsgbXNnKQoKCmRlZiBsb2dnZXJfY2hvaWNlKGNvbG91cik6CglkZWYgY29sX2RlY29yKGYpOgoJCWRlZiBmX2RlY29yKCphcmdzLCAqKmt3YXJncyk6CgkJCWdsb2JhbCBkcHJpbnQKCQkJcHJldl9kcHJpbnQgPSBkcHJpbnQKCQkJZHByaW50ID0gbG9nZ2Vyc1tjb2xvdXJdLmxvZwoJCQlmKCphcmdzLCAqKmt3YXJncykKCQkJZHByaW50ID0gcHJldl9kcHJpbnQKCQlyZXR1cm4gZl9kZWNvcgoJcmV0dXJuIGNvbF9kZWNvcgoKZGVmIGYoKToKCWRwcmludCgidGhpcyBzaG91bGQgYmUgYmxhY2siKQoKQGxvZ2dlcl9jaG9pY2UoY29sb3VyPSd5ZWxsb3cnKQpkZWYgZnUoeCk6CglkcHJpbnQoInRoaXMgc2hvdWxkIGJlIHllbGxvdywgYW5kIDUgPSAlZCIgJSB4KQoKQGxvZ2dlcl9jaG9pY2UoY29sb3VyPSdyZWQnKQpkZWYgZnVuKCk6CglkcHJpbnQoInRoaXMgc2hvdWxkIGJlIHJlZCIpCgoKbG9nZ2VycyA9IHsnYmxhY2snOiBMb2dnZXIoImJsYWNrIiksICd5ZWxsb3cnOiBMb2dnZXIoInllbGxvdyIpLCAncmVkJzogTG9nZ2VyKCJyZWQiKX0KZHByaW50ID0gbG9nZ2Vyc1snYmxhY2snXS5sb2cKCmYoKQpmdSg1KQpmdW4oKQ==