-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunkcje.py
More file actions
49 lines (37 loc) · 1 KB
/
funkcje.py
File metadata and controls
49 lines (37 loc) · 1 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
import time
import functools
def testowa():
time.sleep(1)
return 123
def time_it(f, *args, **kwargs):
t0 = time.perf_counter()
reusutl = f(*args, **kwargs)
t1 = time.perf_counter()
print(f"Wywołanie {f.__name__} zajęło {t1 - t0:.2f} s")
return reusutl
def add_timing(f):
@functools.wraps(f)
def f_with_timing(*args, **kwargs):
return time_it(f, *args, **kwargs)
return f_with_timing
if __name__ == "__main__":
timed_f = add_timing(testowa)
x = timed_f() # Tu następuje wykonanie "testowa" i wydruk ile to trwało
print(f"Funkcja zwróciła {x}") # np. 123
@add_timing
def testowa2():
"Funkcja testowa"
time.sleep(1)
return 234
print(f"Funkcja {testowa2.__qualname__} zwróciła {testowa2()}")
print("Docstring funkcji testowej:", testowa2.__doc__)
"""
>>> @dekorator
... def add(a, b):
... return a + b
### ... znaczy tyle samo co:
>>> def add(a, b):
... return a + b
...
>>> add = dekorator(add)
"""