diff --git a/Week04/decorators_recepgoktug_avci.py b/Week04/decorators_recepgoktug_avci.py new file mode 100644 index 00000000..fe1b5872 --- /dev/null +++ b/Week04/decorators_recepgoktug_avci.py @@ -0,0 +1,28 @@ +import time +import tracemalloc + +def performance(func): + if not hasattr(performance, "counter"): + performance.counter = 0 + performance.total_time = 0.0 + performance.total_mem = 0 + + def wrapper(*args, **kwargs): + tracemalloc.start() + start_time = time.perf_counter() + + result = func(*args, **kwargs) + + end_time = time.perf_counter() + elapsed_time = end_time - start_time + + current_mem, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.counter += 1 + performance.total_time += elapsed_time + performance.total_mem += peak_mem + + return result + + return wrapper diff --git a/Week04/functions_recepgoktug_avci.py b/Week04/functions_recepgoktug_avci.py new file mode 100644 index 00000000..0c90c5c7 --- /dev/null +++ b/Week04/functions_recepgoktug_avci.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: + """ + Calculates the mathematical equation. + + :param x: The first base value. + :param y: The second base value. + :param a: The exponent for x. + :param b: The exponent for y. + :param c: The divisor. + :return: The result of the equation. + """ + + if not all(isinstance(arg, int) for arg in (x, y, a, b, c)): + raise TypeError("All arguments must be integers.") + + return float((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.caller_info = {} + + fn_w_counter.total_calls += 1 + + caller_name = __name__ + + fn_w_counter.caller_info[caller_name] = fn_w_counter.caller_info.get(caller_name, 0) + 1 + + return fn_w_counter.total_calls, fn_w_counter.caller_info.copy() diff --git a/Week05/awaitme_recepgoktug_avci.py b/Week05/awaitme_recepgoktug_avci.py new file mode 100644 index 00000000..0c828f4f --- /dev/null +++ b/Week05/awaitme_recepgoktug_avci.py @@ -0,0 +1,17 @@ +import asyncio + +def awaitme(fn): + if asyncio.iscoroutinefunction(fn): + return fn + + async def wrapper(*args, **kwargs): + return fn(*args, **kwargs) + + try: + wrapper.__name__ = fn.__name__ + wrapper.__doc__ = fn.__doc__ + wrapper.__annotations__ = fn.__annotations__ + except (AttributeError, TypeError): + pass + + return wrapper