-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW18_decorators.py
More file actions
91 lines (71 loc) · 1.81 KB
/
HW18_decorators.py
File metadata and controls
91 lines (71 loc) · 1.81 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
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
#Task 1
# import time
# def timer(func):
# def wrapper(*args, **kwargs):
# st = time.time()
# result = func(*args, **kwargs)
# fn = time.time()
# print(f"{func.__name__} поработала {fn-st} секунд")
# return result
# return wrapper
# @timer
# def slow_calculation(n):
# result = 0
# for i in range(n):
# result += i ** 2
# return result
# slow_calculation(1_000_000)
#Task 2
# def cache(func):
# d = dict()
# def inside_func(x):
# if x not in d:
# d[x] = func(x)
# return d[x]
# return inside_func
# @cache
# def f(x):
# if x <= 1: return x
# return f(x-1) + f(x-2)
# for i in range(100):
# print(i, f(i))
#Task 3
import time
def logging(func):
def wrapper(*args, **kwargs):
with open("log.txt", 'a', encoding='utf-16') as file:
file.write(f"{func.__name__} started with args: {args}\n")
result = func(*args, **kwargs)
file.write(f"{func.__name__} finished\n")
return result
return wrapper
@logging
def work(*args):
for x in args:
time.sleep(1)
if x%2 != 0:
x+= 1
print("work is done")
work(1, 4, 6, 7, 9, 0)
#Task 4
# import time
# import random
# def retry(func):
# def wrapper(*args, **kwargs):
# check = func()
# if check == None:
# for x in range(5):
# print(f"Вызываю функцию {func.__name__}")
# time.sleep(1)
# check = func()
# if check != None:
# return check
# else: return check
# return wrapper
# @retry
# def f():
# x = random.randint(1, 5)
# if x <= 3:
# return None
# else: return x
# print(f())