From ec761d3e590aeeb6968cd85c4fdb4c4cf0438931 Mon Sep 17 00:00:00 2001 From: Jalil Guliyev Date: Sun, 1 Nov 2026 20:41:48 +0300 Subject: [PATCH 1/2] Add custom power and equation functions --- Week04/functions_jalil_guliyev.py | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week04/functions_jalil_guliyev.py diff --git a/Week04/functions_jalil_guliyev.py b/Week04/functions_jalil_guliyev.py new file mode 100644 index 00000000..67b35b81 --- /dev/null +++ b/Week04/functions_jalil_guliyev.py @@ -0,0 +1,36 @@ + +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 an equation. + + :param x: First base. + :param y: Second base. + :param a: Exponent of x. + :param b: Exponent of y. + :param c: Divisor. + :return: The result of (x**a + y**b) / c. + """ + if any(type(value) is not int for value in (x, y, a, b, c)): + raise TypeError("All parameters must be integers") + + return (x ** a + y ** b) / c + + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, "counter"): + fn_w_counter.counter = 0 + fn_w_counter.callers = {} + + fn_w_counter.counter += 1 + caller = __name__ + fn_w_counter.callers[caller] = ( + fn_w_counter.callers.get(caller, 0) + 1 + ) + + return fn_w_counter.counter, fn_w_counter.callers.copy() + From 06e57f11735b1af88e8b56ae92b07cdfb22f418b Mon Sep 17 00:00:00 2001 From: Jalil Guliyev Date: Sun, 1 Nov 2026 20:47:08 +0300 Subject: [PATCH 2/2] Add performance decorator for tracking metrics Implement a performance decorator to track function metrics. --- Week04/decorators_jalil_guliyev.py | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Week04/decorators_jalil_guliyev.py diff --git a/Week04/decorators_jalil_guliyev.py b/Week04/decorators_jalil_guliyev.py new file mode 100644 index 00000000..516495b1 --- /dev/null +++ b/Week04/decorators_jalil_guliyev.py @@ -0,0 +1,43 @@ + +"""Week 04: Decorators homework.""" + +import functools +import time +import tracemalloc + + +def performance(func): + """Track cumulative calls, elapsed time and peak memory allocation.""" + + @functools.wraps(func) + def wrapper(*args, **kwargs): + tracing_started_here = not tracemalloc.is_tracing() + + if tracing_started_here: + tracemalloc.start() + + before_current, before_peak = tracemalloc.get_traced_memory() + start = time.perf_counter() + + try: + return func(*args, **kwargs) + + finally: + elapsed = time.perf_counter() - start + current, peak = tracemalloc.get_traced_memory() + + allocated = max(0, peak - before_current) + + performance.counter += 1 + performance.total_time += elapsed + performance.total_mem += allocated + + if tracing_started_here: + tracemalloc.stop() + + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0