diff --git a/Week04/decorators_yunus_dermencioglu.py b/Week04/decorators_yunus_dermencioglu.py new file mode 100644 index 00000000..311e7e17 --- /dev/null +++ b/Week04/decorators_yunus_dermencioglu.py @@ -0,0 +1,32 @@ +import time +import tracemalloc +from functools import wraps + + +def performance(func): + + @wraps(func) + def wrapper(*args, **kwargs): + + tracemalloc.start() + start_time = time.perf_counter() + + try: + result = func(*args, **kwargs) + finally: + elapsed_time = time.perf_counter() - start_time + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.counter += 1 + performance.total_time += elapsed_time + performance.total_mem += peak + + return result + + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 diff --git a/Week04/functions_yunus_dermencioglu.py b/Week04/functions_yunus_dermencioglu.py new file mode 100644 index 00000000..25c10cdb --- /dev/null +++ b/Week04/functions_yunus_dermencioglu.py @@ -0,0 +1,39 @@ +import inspect + +custom_power = lambda x=0, /, e=1: x ** e + + +def custom_equation( + x: int = 0, + y: int = 0, + /, + a: int = 1, + b: int = 1, + *, + c: int = 1 +) -> float: + """ + Calculate the result of the equation. + + :param x: First integer. + :param y: Second integer. + :param a: Exponent of x. + :param b: Exponent of y. + :param c: Divisor. + :return: The calculated result. + :rtype: float + """ + return float((x ** a + y ** b) / c) + + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, "counts"): + fn_w_counter.counts = {} + + caller_name = fn_w_counter.__module__ + + counts = fn_w_counter.counts + counts[caller_name] = counts.get(caller_name, 0) + 1 + + return sum(counts.values()), counts.copy() +