Skip to content

Commit 0ececc5

Browse files
committed
Bugs fixed
1 parent 494b62c commit 0ececc5

2 files changed

Lines changed: 38 additions & 44 deletions

File tree

Week04/decorators_tarik_bozgan.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
import time, tracemalloc
2-
from functools import wraps
1+
import time
2+
import tracemalloc
33

44
def performance(func):
5-
performance.c = performance.c + 1 if hasattr(performance, "c") else 1
6-
performance.t = getattr(performance, "t", 0.0)
7-
performance.m = getattr(performance, "m", 0)
8-
9-
@wraps(func)
10-
def wrapper(*a, **k):
11-
performance.c += 1
12-
t0 = time.perf_counter()
5+
def wrapper(*args, **kwargs):
136
tracemalloc.start()
14-
r = func(*a, **k)
15-
performance.t += time.perf_counter() - t0
16-
performance.m += tracemalloc.get_traced_memory()[1]
7+
t1 = time.perf_counter()
8+
9+
result = func(*args, **kwargs)
10+
11+
t2 = time.perf_counter()
12+
curr, peak = tracemalloc.get_traced_memory()
1713
tracemalloc.stop()
18-
return r
19-
return wrapper
14+
15+
wrapper.counter += 1
16+
wrapper.total_time += t2 - t1
17+
wrapper.total_mem += peak
18+
19+
return result
2020

21-
performance.counter = 0
22-
performance.total_time = 0.0
23-
performance.total_mem = 0
21+
wrapper.counter = 0
22+
wrapper.total_time = 0
23+
wrapper.total_mem = 0
24+
25+
return wrapper

Week04/functions_tarik_bozgan.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
1-
# custom_power (lambda)
2-
custom_power = lambda x=0, e=1: x ** e
1+
custom_power = lambda x, /, e=1: x ** e
32

4-
5-
def custom_equation(
6-
x: int = 0,
7-
y: int = 0,
8-
a: int = 1,
9-
b: int = 1,
10-
*,
11-
c: int = 1
12-
) -> float:
3+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
134
"""
14-
:param x: integer value
15-
:param y: integer value
16-
:param a: integer value
17-
:param b: integer value
18-
:param c: integer value
19-
:return: result of equation
5+
:param x:
6+
:param y:
7+
:param a:
8+
:param b:
9+
:param c:
10+
:return:
2011
"""
21-
if not all(isinstance(v, int) for v in (x, y, a, b, c)):
22-
raise TypeError("All parameters must be int")
23-
12+
if not isinstance(x, int): raise TypeError("x must be int")
13+
if not isinstance(y, int): raise TypeError("y must be int")
14+
if not isinstance(a, int): raise TypeError("a must be int")
15+
if not isinstance(b, int): raise TypeError("b must be int")
16+
if not isinstance(c, int): raise TypeError("c must be int")
17+
2418
return (x ** a + y ** b) / c
2519

26-
27-
_call_count = 0
28-
20+
_count = 0
2921
def fn_w_counter() -> (int, dict[str, int]):
30-
global _call_count
31-
_call_count += 1
32-
return _call_count, {__name__: _call_count}
22+
global _count
23+
_count += 1
24+
return _count, {__name__: _count}

0 commit comments

Comments
 (0)