-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.py
More file actions
50 lines (34 loc) · 1.02 KB
/
decorator.py
File metadata and controls
50 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from functools import wraps
import time
def time_of(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"running {func.__name__} took {end-start} seconds.")
return result
return wrapper
def time_of_args(name, show_state):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
if show_state:
print(f"running {name} took {end-start} seconds.")
return result
return wrapper
return decorator
def _process(n):
result = 0
for i in range(n):
result += i
return result
@time_of
def process(n):
return _process(n)
@time_of_args('SumBot', show_state=True)
def process_args(n): return _process(n)
process_args2 = time_of_args('UnknownBot', False)(_process)