SHOW:
|
|
- or go back to the newest paste.
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 | - | def repeat(times): |
12 | + | else: |
13 | - | """ повторить вызов times раз, и вернуть среднее значение """ |
13 | + | return fn(*x) |
14 | - | def decorator(fn): |
14 | + | |
15 | - | def decorated2(*x): |
15 | + | return new_decorated |
16 | - | total = 0 |
16 | + | |
17 | - | for i in range(times): |
17 | + | decorator.enabled = True |
18 | - | total += fn(*x) |
18 | + | return new_decorator |
19 | - | return total / times |
19 | + | |
20 | - | return decorated2 |
20 | + | |
21 | - | return decorator |
21 | + | |
22 | start = time() | |
23 | - | @repeat(5) |
23 | + | |
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() |