diff --git a/Week04/decorators_berkay_altunbag.py b/Week04/decorators_berkay_altunbag.py new file mode 100644 index 00000000..0edf19c0 --- /dev/null +++ b/Week04/decorators_berkay_altunbag.py @@ -0,0 +1,30 @@ +import time +import tracemalloc +from functools import wraps + + +def performance(func): + @wraps(func) + def wrapper(*args, **kwargs): + start = time.perf_counter() + was_tracing = tracemalloc.is_tracing() + if not was_tracing: + tracemalloc.start() + tracemalloc.reset_peak() + start_mem = tracemalloc.get_traced_memory()[0] + try: + return func(*args, **kwargs) + finally: + peak = tracemalloc.get_traced_memory()[1] + if not was_tracing: + tracemalloc.stop() + performance.counter += 1 + performance.total_mem += peak - start_mem + performance.total_time += time.perf_counter() - start + + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 \ No newline at end of file diff --git a/Week04/functions_berkay_altunbag.py b/Week04/functions_berkay_altunbag.py new file mode 100644 index 00000000..c2799e3f --- /dev/null +++ b/Week04/functions_berkay_altunbag.py @@ -0,0 +1,29 @@ +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: + """ + Calculates (x**a + y**b) / c. + + :param x: base of the first term (positional-only) + :param y: base of the second term (positional-only) + :param a: exponent of x + :param b: exponent of y + :param c: divisor (keyword-only) + :return: the result of (x**a + y**b) / c + """ + for name, value in (("x", x), ("y", y), ("a", a), ("b", b), ("c", c)): + if not isinstance(value, int): + raise TypeError(f"{name} must be an int") + return (x**a + y**b) / c + + +_counter = 0 + + +def fn_w_counter() -> (int, dict[str, int]): + global _counter + _counter += 1 + return _counter, {__name__: _counter} \ No newline at end of file