diff --git a/Week04/decorators_rafet_taskin.py b/Week04/decorators_rafet_taskin.py new file mode 100644 index 00000000..f6f6cad3 --- /dev/null +++ b/Week04/decorators_rafet_taskin.py @@ -0,0 +1,27 @@ +import time +import tracemalloc +import functools + +def performance(func): + @functools.wraps(func) # Preserve metadata of the original function + def wrapper(*args, **kwargs): + tracemalloc.start() + start_time = time.time() + + try: + result = func(*args, **kwargs) + finally: + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + end_time = time.time() + + performance.counter += 1 + performance.total_time += (end_time - start_time) + performance.total_mem += peak + + return result + return wrapper + +performance.counter = 0 +performance.total_time = 0 +performance.total_mem = 0 diff --git a/Week04/functions_rafet_taskin.py b/Week04/functions_rafet_taskin.py new file mode 100644 index 00000000..f9052da3 --- /dev/null +++ b/Week04/functions_rafet_taskin.py @@ -0,0 +1,27 @@ +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: + """ + Computes a custom equation. + + :param x: First base (positional only) + :param y: Second base (positional only) + :param a: First exponent + :param b: Second exponent + :param c: Divisor (keyword only) + :return: The result of (x^a + y^b) / c + """ + + for param_name, value in [('x', x), ('y', y), ('a', a), ('b', b), ('c', c)]: + if not isinstance(value, int): + raise TypeError(f"{param_name} must be an integer") + + return float((x ** a + y ** b) / c) + + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, "count"): + fn_w_counter.count = 0 + + fn_w_counter.count += 1 + + return fn_w_counter.count, {__name__: fn_w_counter.count}