Skip to content

Commit f515a8d

Browse files
authored
Merge pull request #1012 from rft0/patch-4
create functions_rafet_taskin.py and decorators_rafet_taskin.py
2 parents 8ade37b + 165d99c commit f515a8d

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Week04/decorators_rafet_taskin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import tracemalloc
2+
import time
3+
4+
def performance(func):
5+
performance.counter = 0
6+
performance.total_time = 0.0
7+
performance.total_mem = 0.0
8+
9+
def wrapper(*args, **kwargs):
10+
tracemalloc.start()
11+
start_time = time.time()
12+
result = func(*args, **kwargs)
13+
end_time = time.time()
14+
current, peak = tracemalloc.get_traced_memory()
15+
tracemalloc.stop()
16+
performance.counter += 1
17+
performance.total_time += (end_time - start_time)
18+
performance.total_mem += peak
19+
return result
20+
return wrapper

Week04/functions_rafet_taskin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
custom_power = lambda x=0, /, e=1: x ** e
2+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
3+
"""
4+
Computes a custom equation.
5+
6+
:param x: First base (positional only)
7+
:param y: Second base (positional only)
8+
:param a: First exponent
9+
:param b: Second exponent
10+
:param c: Divisor (keyword only)
11+
:return: The result of (x^a + y^b) / c
12+
"""
13+
14+
for param_name, value in [('x', x), ('y', y), ('a', a), ('b', b), ('c', c)]:
15+
if not isinstance(value, int):
16+
raise TypeError(f"{param_name} must be an integer")
17+
18+
return float((x ** a + y ** b) / c)
19+
20+
21+
def fn_w_counter() -> (int, dict[str, int]):
22+
if not hasattr(fn_w_counter, "count"):
23+
fn_w_counter.count = 0
24+
25+
fn_w_counter.count += 1
26+
27+
return fn_w_counter.count, {__name__: fn_w_counter.count}

0 commit comments

Comments
 (0)