diff --git a/Week01/info_jalil_guliyev.py b/Week01/info_jalil_guliyev.py new file mode 100644 index 000000000..4ed9c9b3d --- /dev/null +++ b/Week01/info_jalil_guliyev.py @@ -0,0 +1,2 @@ +student_id = "220315102" +full_name = "Jalil Guliyev" diff --git a/Week04/decorators_jalil_guliyev.py b/Week04/decorators_jalil_guliyev.py new file mode 100644 index 000000000..6541a3a5b --- /dev/null +++ b/Week04/decorators_jalil_guliyev.py @@ -0,0 +1,34 @@ +"""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 diff --git a/Week04/functions_jalil_guliyev.py b/Week04/functions_jalil_guliyev.py new file mode 100644 index 000000000..c64156223 --- /dev/null +++ b/Week04/functions_jalil_guliyev.py @@ -0,0 +1,34 @@ +"""Week 04: Functions homework.""" + +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 a polynomial-style expression. + + :param x: First integer base. + :param y: Second integer base. + :param a: Exponent of x. + :param b: Exponent of y. + :param c: Integer divisor, which must not be zero. + :return: The value of (x ** a + y ** b) / c as a float. + """ + for name, value in (("x", x), ("y", y), ("a", a), ("b", b), ("c", c)): + if type(value) is not int: + raise TypeError(f"{name} must be an integer") + return (x ** a + y ** b) / c + + +def fn_w_counter() -> (int, dict[str, int]): + """Return the total calls and per-module call counts.""" + fn_w_counter.total += 1 + caller = __name__ + fn_w_counter.callers[caller] = fn_w_counter.callers.get(caller, 0) + 1 + return fn_w_counter.total, fn_w_counter.callers.copy() + + +fn_w_counter.total = 0 +fn_w_counter.callers = {}