Skip to content

Commit beeab75

Browse files
authored
Refactor functions and update docstrings in functions_Helin_Harman.py
Updated function definitions and docstrings for clarity and compliance with Pytest requirements. Adjusted return types and removed unnecessary global declarations.
1 parent 71f5b39 commit beeab75

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Week04/functions_Helin_Harman.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import inspect
2+
import time
3+
import tracemalloc
4+
from functools import wraps
5+
6+
custom_power = lambda x=0, /, e=1: x**e
7+
8+
9+
def custom_equation(x=0, y=0, /, a=1, *, b=1, c=1) -> float:
10+
"""
11+
This function calculates (x*a + y*b)/c
12+
:param x: first value
13+
:param y: second value
14+
:param a: multiplier for x
15+
:param b: multiplier for y
16+
:param c: divisor
17+
"""
18+
return (x*a + y*b)/c
19+
20+
global_counter = 0
21+
caller_log = {}
22+
23+
def fn_w_counter(c: int, d: dict) -> tuple[int, dict[str, int]]:
24+
global global_counter
25+
# Flake8 F824 hatasını önlemek için 'global caller_log' kaldırıldı.
26+
27+
caller_name = 'unknown_run'
28+
try:
29+
caller_name = inspect.stack()[1].f_globals.get('__name__', 'system_call')
30+
except (IndexError, AttributeError, ValueError):
31+
caller_name = 'ide_console'
32+
33+
global_counter += 1
34+
caller_log[caller_name] = caller_log.get(caller_name, 0) + 1
35+
36+
return (global_counter, caller_log.copy())
37+
38+
def performance(func):
39+
@wraps(func)
40+
def _performance(*args, **kwargs):
41+
tracemalloc.start()
42+
t1 = time.perf_counter()
43+
44+
result = func(*args, **kwargs)
45+
46+
current, peak = tracemalloc.get_traced_memory()
47+
tracemalloc.stop()
48+
t2 = time.perf_counter()
49+
50+
_performance.counter += 1
51+
_performance.total_time += (t2 - t1)
52+
_performance.total_mem += peak
53+
54+
return result
55+
56+
_performance.counter = 0
57+
_performance.total_time = 0.0
58+
_performance.total_mem = 0
59+
return _performance

0 commit comments

Comments
 (0)