From 89157eb981e038f16079b6ca9a8f29ddca733885 Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Mon, 30 Mar 2026 19:21:09 +0300 Subject: [PATCH 1/9] Week_04 / homework_okay_sezer --- Week04/decorators_okay_sezer.py | 23 +++++++++++++++++++++++ Week04/functions_okay_sezer.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Week04/decorators_okay_sezer.py create mode 100644 Week04/functions_okay_sezer.py diff --git a/Week04/decorators_okay_sezer.py b/Week04/decorators_okay_sezer.py new file mode 100644 index 00000000..267f7fcc --- /dev/null +++ b/Week04/decorators_okay_sezer.py @@ -0,0 +1,23 @@ +import time +import tracemalloc + +def perdormance(func): + + def wrapper(*args, **kwargs) + wrapper.counter += 1 + + start_time = time.perf_counter() + tracemalloc.start() + + result = func(*args, **kwargs) + + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + end_time = time.perf_counter() + + wrapper.total_time += (end_time - start_time) + + print(f"Call #{wrapper.counter}: {func.__name__} took {end_time - start_time:.6f}s, " + f"Current memory: {current / 10**6:.6f}MB, Peak memory: {peak / 10**6:.6f}MB") + return result \ No newline at end of file diff --git a/Week04/functions_okay_sezer.py b/Week04/functions_okay_sezer.py new file mode 100644 index 00000000..71b7669d --- /dev/null +++ b/Week04/functions_okay_sezer.py @@ -0,0 +1,33 @@ +custom_power = lambda x=0, / , e=1: x ** e #custom_power is a lambda function that takes two parameters: x and e. x is the base and e is the exponent. The function returns x raised to the power of e. The default value for x is 0 and the default value for e is 1. The / symbol indicates that x is a positional-only parameter, meaning it cannot be passed as a keyword argument. + +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. + """ + + return (x**a + y**b) / c + + +def fn_w_counter() -> tuple[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 = __name__ + + 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 + From 53fc372a36de154496fbc7cc413782e5a28373f5 Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Mon, 30 Mar 2026 19:30:45 +0300 Subject: [PATCH 2/9] Fix typo in performance decorator function --- Week04/decorators_okay_sezer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Week04/decorators_okay_sezer.py b/Week04/decorators_okay_sezer.py index 267f7fcc..ec238296 100644 --- a/Week04/decorators_okay_sezer.py +++ b/Week04/decorators_okay_sezer.py @@ -1,9 +1,9 @@ import time import tracemalloc -def perdormance(func): +def performance(func): - def wrapper(*args, **kwargs) + def wrapper(*args, **kwargs): wrapper.counter += 1 start_time = time.perf_counter() @@ -20,4 +20,4 @@ def wrapper(*args, **kwargs) print(f"Call #{wrapper.counter}: {func.__name__} took {end_time - start_time:.6f}s, " f"Current memory: {current / 10**6:.6f}MB, Peak memory: {peak / 10**6:.6f}MB") - return result \ No newline at end of file + return result From 003f508db6323747d8246d172192a0c55362a245 Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Mon, 30 Mar 2026 19:42:45 +0300 Subject: [PATCH 3/9] Update functions_okay_sezer.py --- Week04/functions_okay_sezer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Week04/functions_okay_sezer.py b/Week04/functions_okay_sezer.py index 71b7669d..fcb35840 100644 --- a/Week04/functions_okay_sezer.py +++ b/Week04/functions_okay_sezer.py @@ -1,3 +1,5 @@ +from __future__ import annotations + custom_power = lambda x=0, / , e=1: x ** e #custom_power is a lambda function that takes two parameters: x and e. x is the base and e is the exponent. The function returns x raised to the power of e. The default value for x is 0 and the default value for e is 1. The / symbol indicates that x is a positional-only parameter, meaning it cannot be passed as a keyword argument. def custom_equation(x:int = 0, y:int = 0 , / , a:int = 1 , b:int = 1 , * , c:int = 1 ) -> float: From 817042be90ca1b886efbbd7cd13cf921a15dbde0 Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Mon, 30 Mar 2026 19:44:56 +0300 Subject: [PATCH 4/9] Update functions_okay_sezer.py --- Week04/functions_okay_sezer.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Week04/functions_okay_sezer.py b/Week04/functions_okay_sezer.py index fcb35840..345a50bb 100644 --- a/Week04/functions_okay_sezer.py +++ b/Week04/functions_okay_sezer.py @@ -1,6 +1,4 @@ -from __future__ import annotations - -custom_power = lambda x=0, / , e=1: x ** e #custom_power is a lambda function that takes two parameters: x and e. x is the base and e is the exponent. The function returns x raised to the power of e. The default value for x is 0 and the default value for e is 1. The / symbol indicates that x is a positional-only parameter, meaning it cannot be passed as a keyword argument. +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: @@ -32,4 +30,3 @@ def fn_w_counter() -> tuple[int, dict[str, int]]: fn_w_counter.callers_dict[caller] = 1 return fn_w_counter.total_calls, fn_w_counter.callers_dict - From 76d124df2c34970c8fce6173cf9e161ceb72bc95 Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Mon, 30 Mar 2026 19:56:24 +0300 Subject: [PATCH 5/9] Update functions_okay_sezer.py --- Week04/functions_okay_sezer.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Week04/functions_okay_sezer.py b/Week04/functions_okay_sezer.py index 345a50bb..ffe824a6 100644 --- a/Week04/functions_okay_sezer.py +++ b/Week04/functions_okay_sezer.py @@ -1,7 +1,8 @@ +from typing import Tuple, Dict + 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: - +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. @@ -12,16 +13,17 @@ def custom_equation(x:int = 0, y:int = 0 , / , a:int = 1 , b:int = 1 , * , c:int :param c: The constant term in the equation. Default is 1. :return: The result of the equation. """ - return (x**a + y**b) / c -def fn_w_counter() -> tuple[int, dict[str, int]]: +def fn_w_counter() -> Tuple[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 + # Test sistemleri bazen modül ismini kontrol eder, + # genel kullanım için __name__ doğru tercihtir. caller = __name__ if caller in fn_w_counter.callers_dict: From 69883e075447d3dba31f054eb916c4927f1b0ebc Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Tue, 31 Mar 2026 14:39:29 +0300 Subject: [PATCH 6/9] Update functions_okay_sezer.py Added type checks for parameters in custom_equation and updated return type in fn_w_counter. --- Week04/functions_okay_sezer.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Week04/functions_okay_sezer.py b/Week04/functions_okay_sezer.py index ffe824a6..6fe28995 100644 --- a/Week04/functions_okay_sezer.py +++ b/Week04/functions_okay_sezer.py @@ -1,6 +1,4 @@ -from typing import Tuple, Dict - -custom_power = lambda x=0, / , e=1: x ** e +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: """ @@ -13,18 +11,19 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int :param c: The constant term in the equation. Default is 1. :return: The result of the equation. """ - return (x**a + y**b) / c + 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() -> Tuple[int, Dict[str, int]]: +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 - # Test sistemleri bazen modül ismini kontrol eder, - # genel kullanım için __name__ doğru tercihtir. - caller = __name__ + caller = fn_w_counter.__module__ if caller in fn_w_counter.callers_dict: fn_w_counter.callers_dict[caller] += 1 @@ -32,3 +31,4 @@ def fn_w_counter() -> Tuple[int, Dict[str, int]]: fn_w_counter.callers_dict[caller] = 1 return fn_w_counter.total_calls, fn_w_counter.callers_dict + From 36d569db2e809dcb9cebc836c0d3d7f2a04ee3d5 Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Tue, 31 Mar 2026 14:52:52 +0300 Subject: [PATCH 7/9] Update decorators_okay_sezer.py Added detailed docstring to performance decorator and updated memory tracking. --- Week04/decorators_okay_sezer.py | 44 ++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/Week04/decorators_okay_sezer.py b/Week04/decorators_okay_sezer.py index ec238296..5c87c45a 100644 --- a/Week04/decorators_okay_sezer.py +++ b/Week04/decorators_okay_sezer.py @@ -1,23 +1,43 @@ -import time +import time import tracemalloc +from functools import wraps def performance(func): + """ + A decorator that measures and tracks the performance of decorated functions. - def wrapper(*args, **kwargs): - wrapper.counter += 1 + 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. - start_time = time.perf_counter() - tracemalloc.start() + Args: + func (callable): The function to be decorated. - result = func(*args, **kwargs) + 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 - end_time = time.perf_counter() - - wrapper.total_time += (end_time - start_time) - print(f"Call #{wrapper.counter}: {func.__name__} took {end_time - start_time:.6f}s, " - f"Current memory: {current / 10**6:.6f}MB, Peak memory: {peak / 10**6:.6f}MB") - return result +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 From e1bb207b2ab2d682dd406bdb52f48ae821b24cdd Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Tue, 31 Mar 2026 15:30:55 +0300 Subject: [PATCH 8/9] Add files via upload --- Week05/awaitme_okay_sezer.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Week05/awaitme_okay_sezer.py 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 From be419b10bb9fe0d6ac5a9a94dc2e756c87f6263a Mon Sep 17 00:00:00 2001 From: OkaySezer Date: Wed, 1 Apr 2026 10:46:45 +0300 Subject: [PATCH 9/9] Add files via upload --- Week06/timer_okay_sezer.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Week06/timer_okay_sezer.py 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