Skip to content

Commit 4c5a411

Browse files
committed
Bug fixing 9th trial
1 parent 1807528 commit 4c5a411

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

Week04/decorators_tarik_bozgan.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
def performance(f):
2-
def w(*a, **k):
3-
import time, tracemalloc
4-
tracemalloc.start()
5-
t = time.time()
6-
r = f(*a, **k)
7-
w.total_time += time.time() - t
8-
w.total_mem += tracemalloc.get_traced_memory()[0]
9-
tracemalloc.stop()
10-
w.counter += 1
11-
return r
12-
w.counter = w.total_time = w.total_mem = 0
13-
return w
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)
13+
14+
wrapper.counter += 1
15+
wrapper.total_time += (end_time - start_time)
16+
wrapper.total_mem += mem_usage
17+
18+
return result
19+
20+
wrapper.counter = 0
21+
wrapper.total_time = 0
22+
wrapper.total_mem = 0
23+
24+
return wrapper

Week04/functions_tarik_bozgan.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
custom_power = lambda x=0, /, e=1: x ** e
1+
import sys
2+
3+
custom_power = lambda x, /, e=1: x**e
24

35
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
46
"""
5-
Calculate (x**a + y**b) / c
6-
7-
:param x: First base value
8-
:param y: Second base value
9-
:param a: Exponent for x
10-
:param b: Exponent for y
11-
:param c: Divisor
12-
:return: The result of (x**a + y**b) / c
7+
Belirtilen formüle göre kayan noktalı bir sayı döndürür.
8+
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.
1315
:rtype: float
1416
"""
15-
return (x ** a + y ** b) / c
17+
return (x**a + y**b) / c
1618

17-
_calls = {}
19+
def fn_w_counter() -> tuple:
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
1828

19-
def fn_w_counter() -> tuple[int, dict[str, int]]:
20-
caller = __import__('inspect').currentframe().f_back.f_code.co_name
21-
_calls[caller] = _calls.get(caller, 0) + 1
22-
return _calls[caller], dict(_calls)
29+
return fn_w_counter.count, fn_w_counter.callers

0 commit comments

Comments
 (0)