diff --git a/Week04/decorators_cemil_koca.py b/Week04/decorators_cemil_koca.py new file mode 100644 index 00000000..bbb1fa96 --- /dev/null +++ b/Week04/decorators_cemil_koca.py @@ -0,0 +1,25 @@ +import time +import tracemalloc + +def performance(fn): + def _performance(*args, **kwargs): # wraps yok, düz wrapper + performance.counter += 1 + + tracemalloc.start() + start = time.perf_counter() + + result = fn(*args, **kwargs) + + elapsed = time.perf_counter() - start + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.total_time += elapsed + performance.total_mem += peak + + return result + return _performance + +performance.counter = 0 +performance.total_time = 0 +performance.total_mem = 0 \ No newline at end of file diff --git a/Week04/functions_cemil_koca.py b/Week04/functions_cemil_koca.py new file mode 100644 index 00000000..065e06ad --- /dev/null +++ b/Week04/functions_cemil_koca.py @@ -0,0 +1,35 @@ +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: + """Calculate a custom equation. + + :param x: Base value 1. + :type x: int + :param y: Base value 2. + :type y: int + :param a: Exponent for x. + :type a: int + :param b: Exponent for y. + :type b: int + :param c: Divisor. + :type c: int + :return: Result of (x**a + y**b) / c. + :rtype: float + """ + for param in (x, y, a, b, c): + if not isinstance(param, int) or isinstance(param, bool): + raise TypeError("All parameters must be integers.") + return (x**a + y**b) / c + + +def fn_w_counter() -> (int, dict[str, int]): + """Count function calls with caller information.""" + fn_w_counter.total += 1 + caller_name = __name__ + fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1 + return (fn_w_counter.total, dict(fn_w_counter.callers)) + + +fn_w_counter.total = 0 +fn_w_counter.callers = {} diff --git a/Week05/awaitme_cemil_koca.py b/Week05/awaitme_cemil_koca.py new file mode 100644 index 00000000..6a2ecb10 --- /dev/null +++ b/Week05/awaitme_cemil_koca.py @@ -0,0 +1,7 @@ +def awaitme(fn): + async def _wrapper(*args, **kwargs): + return fn(*args, **kwargs) + _wrapper.__name__ = fn.__name__ + _wrapper.__doc__ = fn.__doc__ + _wrapper.__annotations__ = fn.__annotations__ + return _wrapper \ No newline at end of file