Skip to content

Commit 87af342

Browse files
authored
Merge pull request #1038 from Spohey/Week-04
Week 04 - Hilmi Aydın
2 parents 947d235 + b0006c9 commit 87af342

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎Week04/decorators_hilmi_aydin.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import time
2+
import tracemalloc
3+
4+
5+
def performance(func):
6+
def wrapper(*args, **kwargs):
7+
8+
tracemalloc.start()
9+
start = time.perf_counter()
10+
start_mem = tracemalloc.get_traced_memory()[0]
11+
tracemalloc.reset_peak()
12+
13+
try:
14+
result = func(*args, **kwargs)
15+
16+
finally:
17+
end = time.perf_counter()
18+
current, peak = tracemalloc.get_traced_memory()
19+
20+
performance.counter += 1
21+
performance.total_time += end - start
22+
performance.total_mem += peak - start_mem
23+
24+
tracemalloc.stop()
25+
26+
return result
27+
28+
return wrapper
29+
30+
31+
performance.counter = 0
32+
performance.total_time = 0
33+
performance.total_mem = 0

‎Week04/functions_hilmi_aydin.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
custom_power = lambda x=0, /, e=1: x ** e
2+
3+
4+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
5+
"""
6+
Calculates the result of (x**a + y**b) / c.
7+
8+
:param x: First base, positional-only (default 0)
9+
:param y: Second base, positional-only (default 0)
10+
:param a: Exponent of x (default 1)
11+
:param b: Exponent of y (default 1)
12+
:param c: Divisor, keyword-only (default 1)
13+
:return: The result as a float
14+
"""
15+
return (x ** a + y ** b) / c
16+
17+
18+
def fn_w_counter() -> (int, dict[str, int]):
19+
"""Counts the total number of calls and the number of calls per caller."""
20+
if not hasattr(fn_w_counter, "total"):
21+
fn_w_counter.total = 0
22+
fn_w_counter.callers = {}
23+
fn_w_counter.total += 1
24+
fn_w_counter.callers[__name__] = fn_w_counter.callers.get(__name__, 0) + 1
25+
return fn_w_counter.total, fn_w_counter.callers

0 commit comments

Comments
 (0)