-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw_7_1.py
98 lines (69 loc) · 1.85 KB
/
hw_7_1.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from functools import wraps
import datetime
from time import sleep
def func_timer(func):
@wraps(func)
def decorated(*args, **kwargs):
begin = datetime.datetime.now()
x = func(*args, **kwargs)
end = datetime.datetime.now()
print("{} seconds passed".format(end - begin))
return x
return decorated
class FuncCounter(object):
def __init__(self):
self.count = 0
def __call__(self, func):
@wraps(func)
def decorated(*args, **kwargs):
self.count += 1
print("{} вызвана {} раз".format(func.__name__, self.count))
return func(*args, **kwargs)
return decorated
# @func_timer
# def deep_copy(l):
# import copy
# return copy.deepcopy(l)
#
#
#
# for i in range(100):
# print(i)
# deep_copy([x for x in range(1000*i)])
@FuncCounter()
def do_smth(x, y):
return x + y
@FuncCounter()
def do_smth2(x, y):
return x + y
for i in range(10):
do_smth(1, 1)
do_smth2(1, 1)
def decor_process_logger(func):
@wraps(func)
def decorated(*args, **kwargs):
print("Function {} is started {}".format(func.__name__, datetime.datetime.now()))
x = func(*args, **kwargs)
print("Function {} is over {}".format(func.__name__, datetime.datetime.now()))
return x
print("decorator for {} is created {}".format(func.__name__, datetime.datetime.now()))
return decorated
@decor_process_logger
def sleep_func(sec):
sleep(sec)
for i in range(1, 4):
sleep_func(i)
def catch_exception(func):
@wraps(func)
def decorated(*args, **kwargs):
try:
x = func(*args, **kwargs)
return x
except Exception as e:
print(e)
return decorated
@catch_exception
def to_int(i):
return int(i)
for i in [1, "ad", "2", None]:
print(to_int(i))