Skip to content

Commit 9ae3296

Browse files
authored
Merge pull request #1082 from sultanyuksel/patch-2
Add Week04 functions and decorators
2 parents 662482a + a2bc020 commit 9ae3296

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎Week04/decorators_sultan_yuksel.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time
2+
import tracemalloc
3+
4+
5+
def performance(f):
6+
if not hasattr(performance, "counter"):
7+
performance.counter = 0
8+
if not hasattr(performance, "total_time"):
9+
performance.total_time = 0.0
10+
if not hasattr(performance, "total_mem"):
11+
performance.total_mem = 0
12+
13+
def _performance(*args, **kwargs):
14+
performance.counter += 1
15+
16+
tracemalloc.start()
17+
start_time = time.time()
18+
19+
ret = f(*args, **kwargs)
20+
21+
performance.total_time += time.time() - start_time
22+
performance.total_mem += tracemalloc.get_traced_memory()[1]
23+
tracemalloc.stop()
24+
25+
return ret
26+
27+
return _performance

‎Week04/functions_sultan_yuksel.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
This function calculates the custom equation (x**a + y**b)/c
7+
8+
:param x: Positional-only x value, defaults to 0
9+
:type x: int, optional
10+
:param y: Positional-onlyr y value, defaults to 0
11+
:type y: int, optional
12+
:param a: Positional-or-keyword exponent for x, defaults to 1
13+
:type a: int, optional
14+
:param b: Positional-or-keyword exponent for y, defaults to 1
15+
:type b: int, optional
16+
:param c: Keyword-only division of the sum, defaults to 1
17+
:type c: int, optional
18+
:return: Result of (x**a + y**b)/c
19+
:rtype: float
20+
21+
'''
22+
return (x**a + y**b)/c
23+
24+
25+
def fn_w_counter() -> (int, dict[str, int]):
26+
if not hasattr(fn_w_counter, "counter"):
27+
fn_w_counter.counter = 0
28+
fn_w_counter.callers = {}
29+
fn_w_counter.counter += 1
30+
if __name__ in fn_w_counter.callers:
31+
fn_w_counter.callers[__name__] += 1
32+
else:
33+
fn_w_counter.callers[__name__] = 1
34+
return fn_w_counter.counter, fn_w_counter.callers
35+

0 commit comments

Comments
 (0)