Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psutil
- from time import time
- def toggle(decorator):
- """ позволить "подключать" и "отключать" декоратор """
- def new_decorator(fn):
- decorated = decorator(fn)
- def new_decorated(*x):
- if decorator.enabled:
- return decorated(*x)
- else:
- return fn(*x)
- return new_decorated
- decorator.enabled = True
- return new_decorator
- def timed(fn):
- def decorated(*x):
- start = time()
- result = fn(*x)
- print "Executing %s took %d ms" % (fn.__name__, (time()-start)*1000)
- return result
- return decorated
- @toggle(timed)
- def cpuload():
- load = psutil.cpu_percent()
- print "cpuload() returns %d" % load
- return load
- print "cpuload.__name__==" + cpuload.__name__
- print "CPU load is %d%%" % cpuload()
- timed.enabled = False
- print "CPU load is %d%%" % cpuload()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement