From 6e71e52a485fe566b5afd6d45490f8b8758aad65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Recep=20Kadir=20Alt=C4=B1nta=C5=9F?= <126012220+kdraltntas@users.noreply.github.com> Date: Tue, 24 Mar 2026 01:35:47 +0300 Subject: [PATCH 1/4] Add custom mathematical functions and call counter Defines custom_power lambda and custom_equation function for mathematical calculations. Implements fn_w_counter to track function call counts and caller information. --- Week04/functions_recepkadir_altintas.py | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Week04/functions_recepkadir_altintas.py diff --git a/Week04/functions_recepkadir_altintas.py b/Week04/functions_recepkadir_altintas.py new file mode 100644 index 00000000..87ef6d79 --- /dev/null +++ b/Week04/functions_recepkadir_altintas.py @@ -0,0 +1,39 @@ +import inspect + +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 (x**a + y**b) / c. + + :param x: The first base value (positional-only). + :type x: int + :param y: The second base value (positional-only). + :type y: int + :param a: The exponent for x (positional-or-keyword). + :type a: int + :param b: The exponent for y (positional-or-keyword). + :type b: int + :param c: The divisor (keyword-only). + :type c: int + :return: The result of the equation. + :rtype: float + """ + return float((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.caller_info = {} + + fn_w_counter.total_calls += 1 + + caller_frame = inspect.currentframe().f_back + caller_name = caller_frame.f_globals.get('__name__', 'unknown') + + 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() From 1f231d4c3ea34ca1dc74e98eb684db87e0eb893a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Recep=20Kadir=20Alt=C4=B1nta=C5=9F?= <126012220+kdraltntas@users.noreply.github.com> Date: Tue, 24 Mar 2026 01:40:02 +0300 Subject: [PATCH 2/4] Fix indentation and formatting in fn_w_counter --- Week04/functions_recepkadir_altintas.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Week04/functions_recepkadir_altintas.py b/Week04/functions_recepkadir_altintas.py index 87ef6d79..a7322ff6 100644 --- a/Week04/functions_recepkadir_altintas.py +++ b/Week04/functions_recepkadir_altintas.py @@ -24,7 +24,6 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: 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.caller_info = {} From 66480cff991519a98870f5753b28cbe0e1ea2bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Recep=20Kadir=20Alt=C4=B1nta=C5=9F?= <126012220+kdraltntas@users.noreply.github.com> Date: Tue, 24 Mar 2026 01:45:16 +0300 Subject: [PATCH 3/4] Refactor custom_equation and fn_w_counter functions Updated docstring for custom_equation and modified type hints. --- Week04/functions_recepkadir_altintas.py | 30 +++++++++++-------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Week04/functions_recepkadir_altintas.py b/Week04/functions_recepkadir_altintas.py index a7322ff6..0c90c5c7 100644 --- a/Week04/functions_recepkadir_altintas.py +++ b/Week04/functions_recepkadir_altintas.py @@ -1,37 +1,33 @@ -import inspect 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 (x**a + y**b) / c. - - :param x: The first base value (positional-only). - :type x: int - :param y: The second base value (positional-only). - :type y: int - :param a: The exponent for x (positional-or-keyword). - :type a: int - :param b: The exponent for y (positional-or-keyword). - :type b: int - :param c: The divisor (keyword-only). - :type c: int + 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. - :rtype: float """ + + 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() -> 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.caller_info = {} fn_w_counter.total_calls += 1 - caller_frame = inspect.currentframe().f_back - caller_name = caller_frame.f_globals.get('__name__', 'unknown') + caller_name = __name__ fn_w_counter.caller_info[caller_name] = fn_w_counter.caller_info.get(caller_name, 0) + 1 From 6e21b2c533608bf77612a324b5cd31e61f67231b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Recep=20Kadir=20Alt=C4=B1nta=C5=9F?= <126012220+kdraltntas@users.noreply.github.com> Date: Tue, 24 Mar 2026 01:49:39 +0300 Subject: [PATCH 4/4] Add performance decorator for function profiling --- Week04/decorators_recepkadir_altintas.py | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Week04/decorators_recepkadir_altintas.py diff --git a/Week04/decorators_recepkadir_altintas.py b/Week04/decorators_recepkadir_altintas.py new file mode 100644 index 00000000..496b29fb --- /dev/null +++ b/Week04/decorators_recepkadir_altintas.py @@ -0,0 +1,29 @@ +import time +import tracemalloc +from functools import wraps + +def performance(func): + @wraps(func) + 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 + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0