Guest User

пример 3

a guest
Jul 22nd, 2013
298
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import psutil
  2. from time import time
  3.  
  4. def toggle(decorator):
  5.     """ позволить "подключать" и "отключать" декоратор """
  6.     def new_decorator(fn):
  7.         decorated = decorator(fn)
  8.  
  9.         def new_decorated(*x):
  10.             if decorator.enabled:
  11.                 return decorated(*x)
  12.             else:
  13.                 return fn(*x)
  14.  
  15.         return new_decorated
  16.  
  17.     decorator.enabled = True
  18.     return new_decorator
  19.  
  20. def timed(fn):
  21.     def decorated(*x):
  22.         start = time()
  23.         result = fn(*x)
  24.         print "Executing %s took %d ms" % (fn.__name__, (time()-start)*1000)
  25.         return result
  26.     return decorated
  27.  
  28. @toggle(timed)
  29. def cpuload():
  30.     load = psutil.cpu_percent()
  31.     print "cpuload() returns %d" % load
  32.     return load
  33.  
  34. print "cpuload.__name__==" + cpuload.__name__
  35. print "CPU load is %d%%" % cpuload()
  36. timed.enabled = False
  37. print "CPU load is %d%%" % cpuload()
RAW Paste Data