diff --git a/Week04/decorators_aleyna_gultekin.py b/Week04/decorators_aleyna_gultekin.py new file mode 100644 index 00000000..32bff065 --- /dev/null +++ b/Week04/decorators_aleyna_gultekin.py @@ -0,0 +1,31 @@ +import time +import tracemalloc +import functools + + +class performance: + """Decorator class collecting performance statistics of functions.""" + counter = 0 + total_time = 0.0 + total_mem = 0 + + def __init__(self, func): + functools.update_wrapper(self, func) + self.func = func + + def __call__(self, *args, **kwargs): + was_tracing = tracemalloc.is_tracing() + if not was_tracing: + tracemalloc.start() + mem_before = tracemalloc.get_traced_memory()[0] + t0 = time.perf_counter() + try: + return self.func(*args, **kwargs) + finally: + t1 = time.perf_counter() + mem_after, peak = tracemalloc.get_traced_memory() + if not was_tracing: + tracemalloc.stop() + performance.counter += 1 + performance.total_time += t1 - t0 + performance.total_mem += max(peak - mem_before, 0) diff --git a/Week04/functions_aleyna_gultekin.py b/Week04/functions_aleyna_gultekin.py new file mode 100644 index 00000000..07f59ea4 --- /dev/null +++ b/Week04/functions_aleyna_gultekin.py @@ -0,0 +1,38 @@ +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: + """ + Raises x to the power a and y to the power b, adds them and divides by c. + + :param x: Base of the first term (positional-only) + :type x: int + :param y: Base of the second term (positional-only) + :type y: int + :param a: Power applied to x + :type a: int + :param b: Power applied to y + :type b: int + :param c: Number to divide the sum by (keyword-only) + :type c: int + :return: The calculated value + :rtype: 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 int") + return float((x ** a + y ** b) / c) + + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, "call_count"): + fn_w_counter.call_count = 0 + fn_w_counter.callers = {} + + fn_w_counter.call_count += 1 + if __name__ in fn_w_counter.callers: + fn_w_counter.callers[__name__] += 1 + else: + fn_w_counter.callers[__name__] = 1 + + return fn_w_counter.call_count, fn_w_counter.callers