diff --git a/Week04/decorators_beyza_arman.py b/Week04/decorators_beyza_arman.py new file mode 100644 index 00000000..2aa037ee --- /dev/null +++ b/Week04/decorators_beyza_arman.py @@ -0,0 +1,29 @@ +from functools import wraps +from time import perf_counter +import tracemalloc + + +def performance(function): + """Measure calls, elapsed time and peak traced memory.""" + @wraps(function) + def wrapper(*args, **kwargs): + owns_tracing = not tracemalloc.is_tracing() + if owns_tracing: + tracemalloc.start() + before, _ = tracemalloc.get_traced_memory() + start = perf_counter() + try: + return function(*args, **kwargs) + finally: + performance.counter += 1 + performance.total_time += perf_counter() - start + _, peak = tracemalloc.get_traced_memory() + performance.total_mem += max(0, peak - before) + if owns_tracing: + tracemalloc.stop() + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 diff --git a/Week04/functions_beyza_arman.py b/Week04/functions_beyza_arman.py new file mode 100644 index 00000000..ae6e83b7 --- /dev/null +++ b/Week04/functions_beyza_arman.py @@ -0,0 +1,30 @@ +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 sum of powers divided by c. + + :param x: First base. + :param y: Second base. + :param a: Exponent of x. + :param b: Exponent of y. + :param c: Divisor. + :return: The value of (x**a + y**b) / c. + :raises TypeError: If an argument is not an integer. + """ + if not all(isinstance(value, int) for value in (x, y, a, b, c)): + raise TypeError("All arguments must be integers") + return (x ** a + y ** b) / c + + +def fn_w_counter() -> (int, dict[str, int]): + """Count calls and keep a per-module record.""" + fn_w_counter.counter += 1 + calls = fn_w_counter.calls + calls[__name__] = calls.get(__name__, 0) + 1 + return fn_w_counter.counter, dict(calls) + + +fn_w_counter.counter = 0 +fn_w_counter.calls = {}