Skip to content

Commit cf080c3

Browse files
authored
Merge pull request #1073 from ArdaDenizKinikli/patch-4
Create: decorators_ardadeniz_kinikli.py and functions_ardadeniz_kinikli.py
2 parents 03db7cc + 9917264 commit cf080c3

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import time
2+
import tracemalloc
3+
def performance(func):
4+
if not hasattr(performance,"counter"):
5+
setattr(performance,"counter",0)
6+
7+
if not hasattr(performance,"total_time"):
8+
setattr(performance,"total_time",0)
9+
10+
if not hasattr(performance,"total_mem"):
11+
setattr(performance,"total_mem",0)
12+
13+
def _d1(*args,**kwargs):
14+
tracemalloc.start()
15+
start_time = time.perf_counter()
16+
result = func(*args,**kwargs)
17+
end_time = time.perf_counter()
18+
elapsed = end_time - start_time
19+
current_mem, peak_mem = tracemalloc.get_traced_memory()
20+
performance.counter += 1
21+
performance.total_time += elapsed
22+
performance.total_mem += peak_mem
23+
return result
24+
return _d1
25+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
custom_power = lambda x = 0, /, e = 1 : x ** e
2+
3+
def custom_equation(x: int = 0, y: int= 0, /, a: int=1, b: int=1, *, c: int=1) -> float:
4+
"""
5+
This function computes the result of the expression (x**a + y**b) / c.
6+
7+
:param x: The base for the first exponentiation. Positional-only.
8+
:type x: int
9+
:param y: The base for the second exponentiation. Positional-only.
10+
:type y: int
11+
:param a: The exponent for x. Positional-or-keyword.
12+
:type a: int
13+
:param b: The exponent for y. Positional-or-keyword.
14+
:type b: int
15+
:param c: The divisor. Keyword-only. Defaults to 1.
16+
:type c: int
17+
:returns: The result of the calculation.
18+
:rtype: float
19+
20+
"""
21+
return (custom_power(x,a) + custom_power(y,b))/c
22+
23+
def fn_w_counter() -> (int, dict[str, int]):
24+
if not hasattr(fn_w_counter, "count"):
25+
setattr(fn_w_counter, "count", 0)
26+
setattr(fn_w_counter,"dictionary",{})
27+
fn_w_counter.count += 1
28+
fn_w_counter.dictionary[(str)(__name__)] = fn_w_counter.count
29+
return fn_w_counter.count,fn_w_counter.dictionary
30+
31+

0 commit comments

Comments
 (0)