Skip to content

Commit 37cc7dc

Browse files
authored
Merge pull request #722 from sinemtanriver/patch-4
Implement custom power and equation functions
2 parents 6a64caa + cb91663 commit 37cc7dc

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

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

‎Week04/functions_sinem_tanriver.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
Computes the value of (x**a + y**b) / c.
7+
8+
:param x: First base, positional-only
9+
:type x: int
10+
:param y: Second base, positional-only
11+
:type y: int
12+
:param a: Exponent of x
13+
:type a: int
14+
:param b: Exponent of y
15+
:type b: int
16+
:param c: Divisor, keyword-only
17+
:type c: int
18+
:return: Result of (x**a + y**b) / c
19+
:rtype: float
20+
"""
21+
return (x ** a + y ** b) / c
22+
23+
24+
def fn_w_counter() -> (int, dict[str, int]):
25+
caller = __name__
26+
fn_w_counter.total += 1
27+
fn_w_counter.callers[caller] = fn_w_counter.callers.get(caller, 0) + 1
28+
return fn_w_counter.total, fn_w_counter.callers
29+
30+
31+
fn_w_counter.total = 0
32+
fn_w_counter.callers = {}

0 commit comments

Comments
 (0)