diff --git a/Week04/decorators_okay_sezer.py b/Week04/decorators_okay_sezer.py new file mode 100644 index 00000000..5c87c45a --- /dev/null +++ b/Week04/decorators_okay_sezer.py @@ -0,0 +1,43 @@ +import time +import tracemalloc +from functools import wraps + +def performance(func): + """ + A decorator that measures and tracks the performance of decorated functions. + + Attributes: + counter (int): The number of times the decorated function has been called. + total_time (float): The cumulative execution time (in seconds) of all calls. + total_mem (int): The cumulative memory usage (in bytes) of all calls. + + Args: + func (callable): The function to be decorated. + + Returns: + callable: A wrapper function that executes the original function while tracking performance metrics + """ + + @wraps(func) + def wrapper(*args, **kwargs): + tracemalloc.start() + + start_time = time.perf_counter() + result = func(*args, **kwargs) + end_time = time.perf_counter() + + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.counter += 1 + performance.total_time += (end_time - start_time) + performance.total_mem += current + + return result + + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 diff --git a/Week04/functions_okay_sezer.py b/Week04/functions_okay_sezer.py new file mode 100644 index 00000000..6fe28995 --- /dev/null +++ b/Week04/functions_okay_sezer.py @@ -0,0 +1,34 @@ +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: + """ + This function solves the equation (x**a + y**b) / c. + + :param x: The value of x in the equation. Default is 0. + :param y: The value of y in the equation. Default is 0. + :param a: The coefficient of x in the equation. Default is 1. + :param b: The coefficient of y in the equation. Default is 1. + :param c: The constant term in the equation. Default is 1. + :return: The result of the equation. + """ + for name, val in [("x", x), ("y", y), ("a", a), ("b", b), ("c", c)]: + if not isinstance(val, int): + raise TypeError(f"{name} must be an integer") + return (x**a + y**b) / c + + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, "total_calls"): + fn_w_counter.total_calls = 0 + fn_w_counter.callers_dict = {} + + fn_w_counter.total_calls += 1 + caller = fn_w_counter.__module__ + + if caller in fn_w_counter.callers_dict: + fn_w_counter.callers_dict[caller] += 1 + else: + fn_w_counter.callers_dict[caller] = 1 + + return fn_w_counter.total_calls, fn_w_counter.callers_dict + diff --git a/Week05/awaitme_okay_sezer.py b/Week05/awaitme_okay_sezer.py new file mode 100644 index 00000000..ed1af02d --- /dev/null +++ b/Week05/awaitme_okay_sezer.py @@ -0,0 +1,6 @@ +def awaitme(func): + """A decorator that allows the decorated function to be called with 'await' syntax, enabling asynchronous behavior.""" + async def wrapper(*args, **kwargs): + return func(*args, **kwargs) + + return wrapper \ No newline at end of file diff --git a/Week06/timer_okay_sezer.py b/Week06/timer_okay_sezer.py new file mode 100644 index 00000000..3f13f670 --- /dev/null +++ b/Week06/timer_okay_sezer.py @@ -0,0 +1,13 @@ +import time + +class Timer: + def __init__(self): + self.start_time = None + self.end_time = None + + def __enter__(self): + self.start_time = time.time() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.end_time = time.time() \ No newline at end of file