From d5be732ed0a822d0f4f52fcd16fe9855b0a48fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 6 Nov 2026 12:50:40 +0300 Subject: [PATCH 1/6] Add files via upload --- Week04/decorators_yunus_dermencioglu.py | 43 ++++++++++++++++++++++ Week04/functions_yunus_dermencioglu.py | 47 +++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 Week04/decorators_yunus_dermencioglu.py create mode 100644 Week04/functions_yunus_dermencioglu.py diff --git a/Week04/decorators_yunus_dermencioglu.py b/Week04/decorators_yunus_dermencioglu.py new file mode 100644 index 00000000..534cd5ea --- /dev/null +++ b/Week04/decorators_yunus_dermencioglu.py @@ -0,0 +1,43 @@ +import time +import tracemalloc +from functools import wraps + + +# Measure function performance +def performance(func): + + @wraps(func) + def wrapper(*args, **kwargs): + + start_time = time.perf_counter() + + was_tracing = tracemalloc.is_tracing() + + if not was_tracing: + tracemalloc.start() + + start_memory, _ = tracemalloc.get_traced_memory() + + try: + result = func(*args, **kwargs) + finally: + end_time = time.perf_counter() + + _, peak_memory = tracemalloc.get_traced_memory() + + wrapper.counter += 1 + wrapper.total_time += end_time - start_time + wrapper.total_mem += max( + 0, peak_memory - start_memory + ) + + if not was_tracing: + tracemalloc.stop() + + return result + + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 + + return wrapper \ No newline at end of file diff --git a/Week04/functions_yunus_dermencioglu.py b/Week04/functions_yunus_dermencioglu.py new file mode 100644 index 00000000..ffe1a2ba --- /dev/null +++ b/Week04/functions_yunus_dermencioglu.py @@ -0,0 +1,47 @@ +import inspect + + +# Calculate the power of a number +custom_power = lambda x=0, /, e=1: x ** e + + +# Calculate the result of a mathematical equation +def custom_equation( + x: int = 0, + y: int = 0, + /, + a: int = 1, + b: int = 1, + *, + c: int = 1 +) -> float: + """ + Calculate the result of a mathematical equation. + + :param x: First integer value. + :param y: Second integer value. + :param a: Coefficient of x. + :param b: Coefficient of y. + :param c: Divisor of the equation. + :return: The calculated result as a float. + """ + return (x * a + y * b) / c + + +# Count function calls and track caller information +def fn_w_counter(): + if not hasattr(fn_w_counter, "counter"): + fn_w_counter.counter = 0 + fn_w_counter.callers = {} + + caller_frame = inspect.currentframe().f_back + caller_name = caller_frame.f_globals.get("__name__", "__main__") + + fn_w_counter.counter += 1 + + 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.copy() \ No newline at end of file From d91db7c84cc83f4fdfe0f6dba4b60b5e5f067403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 6 Nov 2026 13:00:50 +0300 Subject: [PATCH 2/6] Refactor custom_equation and fn_w_counter functions- Updated the custom_equation function to use exponents for x and y instead of coefficients. Modified fn_w_counter to return a tuple with the counter and a copy of the callers dictionary. --- Week04/functions_yunus_dermencioglu.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Week04/functions_yunus_dermencioglu.py b/Week04/functions_yunus_dermencioglu.py index ffe1a2ba..3a79c7ea 100644 --- a/Week04/functions_yunus_dermencioglu.py +++ b/Week04/functions_yunus_dermencioglu.py @@ -1,11 +1,9 @@ import inspect -# Calculate the power of a number custom_power = lambda x=0, /, e=1: x ** e -# Calculate the result of a mathematical equation def custom_equation( x: int = 0, y: int = 0, @@ -20,16 +18,15 @@ def custom_equation( :param x: First integer value. :param y: Second integer value. - :param a: Coefficient of x. - :param b: Coefficient of y. + :param a: Exponent of x. + :param b: Exponent of y. :param c: Divisor of the equation. :return: The calculated result as a float. """ - return (x * a + y * b) / c + return (x ** a + y ** b) / c -# Count function calls and track caller information -def fn_w_counter(): +def fn_w_counter() -> tuple[int, dict[str, int]]: if not hasattr(fn_w_counter, "counter"): fn_w_counter.counter = 0 fn_w_counter.callers = {} @@ -44,4 +41,4 @@ def fn_w_counter(): else: fn_w_counter.callers[caller_name] = 1 - return fn_w_counter.counter, fn_w_counter.callers.copy() \ No newline at end of file + return fn_w_counter.counter, fn_w_counter.callers.copy() From df489cee4a02b9dc1a4be2a28078ba594c7706fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 6 Nov 2026 13:06:47 +0300 Subject: [PATCH 3/6] Update functions_yunus_dermencioglu.py --- Week04/functions_yunus_dermencioglu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week04/functions_yunus_dermencioglu.py b/Week04/functions_yunus_dermencioglu.py index 3a79c7ea..21ef3c1c 100644 --- a/Week04/functions_yunus_dermencioglu.py +++ b/Week04/functions_yunus_dermencioglu.py @@ -26,7 +26,7 @@ def custom_equation( 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, "counter"): fn_w_counter.counter = 0 fn_w_counter.callers = {} From 9bb7dc1cc972b8549af8f7962b2fc96f08d9b9be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 6 Nov 2026 13:10:07 +0300 Subject: [PATCH 4/6] Refactor fn_w_counter to use co_name for caller --- Week04/functions_yunus_dermencioglu.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Week04/functions_yunus_dermencioglu.py b/Week04/functions_yunus_dermencioglu.py index 21ef3c1c..ea66df5a 100644 --- a/Week04/functions_yunus_dermencioglu.py +++ b/Week04/functions_yunus_dermencioglu.py @@ -31,8 +31,10 @@ def fn_w_counter() -> (int, dict[str, int]): fn_w_counter.counter = 0 fn_w_counter.callers = {} - caller_frame = inspect.currentframe().f_back - caller_name = caller_frame.f_globals.get("__name__", "__main__") + caller_name = inspect.currentframe().f_back.f_code.co_name + + if caller_name == "": + caller_name = "__main__" fn_w_counter.counter += 1 From 4c736809a9abfc36f1d3e217187cb03098b514bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 6 Nov 2026 13:15:35 +0300 Subject: [PATCH 5/6] Update functions_yunus_dermencioglu.py --- Week04/functions_yunus_dermencioglu.py | 33 ++++++++++---------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/Week04/functions_yunus_dermencioglu.py b/Week04/functions_yunus_dermencioglu.py index ea66df5a..25c10cdb 100644 --- a/Week04/functions_yunus_dermencioglu.py +++ b/Week04/functions_yunus_dermencioglu.py @@ -1,6 +1,5 @@ import inspect - custom_power = lambda x=0, /, e=1: x ** e @@ -14,33 +13,27 @@ def custom_equation( c: int = 1 ) -> float: """ - Calculate the result of a mathematical equation. + Calculate the result of the equation. - :param x: First integer value. - :param y: Second integer value. + :param x: First integer. + :param y: Second integer. :param a: Exponent of x. :param b: Exponent of y. - :param c: Divisor of the equation. - :return: The calculated result as a float. + :param c: Divisor. + :return: The calculated result. + :rtype: float """ - return (x ** a + y ** b) / c + return float((x ** a + y ** 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 = {} - - caller_name = inspect.currentframe().f_back.f_code.co_name + if not hasattr(fn_w_counter, "counts"): + fn_w_counter.counts = {} - if caller_name == "": - caller_name = "__main__" + caller_name = fn_w_counter.__module__ - fn_w_counter.counter += 1 + counts = fn_w_counter.counts + counts[caller_name] = counts.get(caller_name, 0) + 1 - if caller_name in fn_w_counter.callers: - fn_w_counter.callers[caller_name] += 1 - else: - fn_w_counter.callers[caller_name] = 1 + return sum(counts.values()), counts.copy() - return fn_w_counter.counter, fn_w_counter.callers.copy() From 70675ce8b4331b9e7e2da9142c1ebbec201b3c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 6 Nov 2026 13:18:53 +0300 Subject: [PATCH 6/6] Update decorators_yunus_dermencioglu.py --- Week04/decorators_yunus_dermencioglu.py | 35 +++++++++---------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/Week04/decorators_yunus_dermencioglu.py b/Week04/decorators_yunus_dermencioglu.py index 534cd5ea..311e7e17 100644 --- a/Week04/decorators_yunus_dermencioglu.py +++ b/Week04/decorators_yunus_dermencioglu.py @@ -3,41 +3,30 @@ from functools import wraps -# Measure function performance def performance(func): @wraps(func) def wrapper(*args, **kwargs): + tracemalloc.start() start_time = time.perf_counter() - was_tracing = tracemalloc.is_tracing() - - if not was_tracing: - tracemalloc.start() - - start_memory, _ = tracemalloc.get_traced_memory() - try: result = func(*args, **kwargs) finally: - end_time = time.perf_counter() + elapsed_time = time.perf_counter() - start_time + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() - _, peak_memory = tracemalloc.get_traced_memory() - - wrapper.counter += 1 - wrapper.total_time += end_time - start_time - wrapper.total_mem += max( - 0, peak_memory - start_memory - ) - - if not was_tracing: - tracemalloc.stop() + performance.counter += 1 + performance.total_time += elapsed_time + performance.total_mem += peak return result - wrapper.counter = 0 - wrapper.total_time = 0.0 - wrapper.total_mem = 0 + return wrapper + - return wrapper \ No newline at end of file +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0