diff --git a/Week04/decorators_gokhan_koray_bulbul.py b/Week04/decorators_gokhan_koray_bulbul.py new file mode 100644 index 00000000..8d069b1d --- /dev/null +++ b/Week04/decorators_gokhan_koray_bulbul.py @@ -0,0 +1,38 @@ +import time +import tracemalloc + +def performance(func): + """ + A decorator that measures execution time and memory usage. + Statistics are stored as attributes on the wrapper function. + """ + + # Initialize the required attributes on the wrapper function + performance.counter = 0 + performance.total_time = 0.0 + performance.total_mem = 0 + + def _performance(*args, **kwargs): + + # 1. Increment the call counter + performance.counter += 1 + + # 2. Start memory tracking + tracemalloc.start() + + # 3. Start timer + start_time = time.perf_counter() + + # Execute the original function + result = func(*args, **kwargs) + + # 4. Stop timer and calculate duration + end_time = time.perf_counter() + performance.total_time += (end_time - start_time) + + # 5. Get memory stats (current, peak) and stop tracking + _, peak = tracemalloc.get_traced_memory() + performance.total_mem += peak + tracemalloc.stop() + + return _performance diff --git a/Week04/functions_gokhan_koray_bulbul.py b/Week04/functions_gokhan_koray_bulbul.py new file mode 100644 index 00000000..fba56a3b --- /dev/null +++ b/Week04/functions_gokhan_koray_bulbul.py @@ -0,0 +1,25 @@ +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 returns a float value of the equation (x ** a + y ** b) / c + + :param x: The first term of the equation. Default is 0. + :param y: The second term of the equation. Default is 0. + :param a: The exponent for the first term of the equation. Default is 1. + :param b: The exponent for the second term of the equation. Default is 1. + :param c: The divisor for the entire equation. Default is 1. + :return: The result of the equation (x ** a + y ** b) / c as a float. + """ + return (custom_power(x, e = a) + custom_power(y, e = b)) / c + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, "counter"): + fn_w_counter.counter = 0 + fn_w_counter.callers = {} + fn_w_counter.counter += 1 + caller_name = __name__ + if caller_name in fn_w_counter.callers: + fn_w_counter.callers[caller_name] += 1 + else: + fn_w_counter.callers[caller_name] = 1 + return fn_w_counter.counter, fn_w_counter.callers diff --git a/Week05/awaitme_gokhan_koray_bulbul.py b/Week05/awaitme_gokhan_koray_bulbul.py new file mode 100644 index 00000000..308cb323 --- /dev/null +++ b/Week05/awaitme_gokhan_koray_bulbul.py @@ -0,0 +1,10 @@ +import asyncio + +def awaitme(givenFunction): + if asyncio.iscoroutinefunction(givenFunction): + return givenFunction + + async def wrapper(*args, **kwargs): + return givenFunction(*args, **kwargs) + + return wrapper diff --git a/Week06/timer_gokhan_koray_bulbul.py b/Week06/timer_gokhan_koray_bulbul.py new file mode 100644 index 00000000..a575b932 --- /dev/null +++ b/Week06/timer_gokhan_koray_bulbul.py @@ -0,0 +1,14 @@ +import time + +class Timer: + def __init__(self): + self.start_time = 0.0 + self.end_time = 0.0 + + def __enter__(self): + self.start_time = time.time() + return self + + def __exit__(self, a, b, c): + self.end_time = time.time() + return True \ No newline at end of file