-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorators.py
48 lines (38 loc) · 876 Bytes
/
decorators.py
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
import time
import json
def log_duration(fun):
def wrapper():
start = time.time()
fun()
end = time.time()
print('log time = ', end-start)
return wrapper
def to_json(fun):
def wrapper():
result = fun()
if type(result) is dict:
new = json.dumps(result)
return new
return result
return wrapper
def ignore_exceptions(exception_class):
def ignore(fun):
def wrapper():
try:
fun()
except exception_class:
pass
return None
return wrapper
return ignore
#@log_duration
#@to_json
#@ignore_exceptions(ZeroDivisionError)
def something():
dict = {}
for i in range(pow(3,2)):
i **= 2
dict.update({i:'hi'})
#1 / 0
return dict
print('finally: ', something())