Skip to content

Commit bc37b50

Browse files
committed
Bug fixing
1 parent e570787 commit bc37b50

File tree

2 files changed

+29
-44
lines changed

2 files changed

+29
-44
lines changed

Week04/decorators_tarik_bozgan.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import time
2-
import sys
3-
import functools
4-
def performance(func):
5-
@functools.wraps(func)
6-
def wrapper(*args, **kwargs):
7-
start_time = time.perf_counter()
8-
9-
result = func(*args, **kwargs)
10-
11-
end_time = time.perf_counter()
12-
mem_usage = sys.getsizeof(result)
1+
import time, tracemalloc
2+
from functools import wraps
133

14-
wrapper.counter += 1
15-
wrapper.total_time += (end_time - start_time)
16-
wrapper.total_mem += mem_usage
17-
18-
return result
4+
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)
198

20-
wrapper.counter = 0
21-
wrapper.total_time = 0
22-
wrapper.total_mem = 0
9+
@wraps(func)
10+
def wrapper(*a, **k):
11+
performance.c += 1
12+
t0 = time.perf_counter()
13+
tracemalloc.start()
14+
r = func(*a, **k)
15+
performance.t += time.perf_counter() - t0
16+
performance.m += tracemalloc.get_traced_memory()[1]
17+
tracemalloc.stop()
18+
return r
19+
return wrapper
2320

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

Week04/functions_tarik_bozgan.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
1-
import sys
2-
31
custom_power = lambda x=0, /, e=1: x**e
2+
"""lambda x=0 / e=1 → x**e"""
43

5-
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
6-
"""
7-
Belirtilen formüle göre kayan noktalı bir sayı döndürür.
84

9-
:param x: Konumsal-tek parametre (Varsayılan 0).
10-
:param y: Konumsal-tek parametre (Varsayılan 0).
11-
:param a: Konumsal-veya-anahtar kelime parametresi (Varsayılan 1).
12-
:param b: Konumsal-veya-anahtar kelime parametresi (Varsayılan 1).
13-
:param c: Anahtar kelime-tek parametre (Varsayılan 1).
14-
:returns: (x**a + y**b) / c formülünün sonucunu döndürür.
15-
:rtype: float
16-
"""
5+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
6+
"""(x**a + y**b) / c"""
177
return (x**a + y**b) / c
188

19-
def fn_w_counter() -> tuple[int, dict[str, int]]:
20-
if not hasattr(fn_w_counter, 'count'):
21-
fn_w_counter.count = 0
22-
fn_w_counter.callers = {}
23-
24-
caller_name = sys._getframe(1).f_globals['__name__']
25-
26-
fn_w_counter.count += 1
27-
fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1
289

29-
return fn_w_counter.count, fn_w_counter.callers
10+
def fn_w_counter() -> tuple[int, dict[str, int]]:
11+
caller = __name__ if __name__ != "<lambda>" else "lambda"
12+
fn_w_counter.total = getattr(fn_w_counter, "total", 0) + 1
13+
fn_w_counter.callers = getattr(fn_w_counter, "callers", {})
14+
fn_w_counter.callers[caller] = fn_w_counter.callers.get(caller, 0) + 1
15+
return fn_w_counter.total, fn_w_counter.callers.copy()

0 commit comments

Comments
 (0)