From ad9a5aedcf4e01f2b4040b359d790744b56312e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 18:14:57 +0300 Subject: [PATCH 01/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Week04/functions_miray_cengil.py diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py new file mode 100644 index 00000000..1a89ad63 --- /dev/null +++ b/Week04/functions_miray_cengil.py @@ -0,0 +1,34 @@ +def custom_power(x=0, /, e=1): + """ + A lambda function that takes two parameters, x and e, and returns x to the power of e. + + :param x: positional-only parameter (default 0) + :param e: positional-or-keyword parameter (default 1) + :return: x raised to the power of e + """ + return lambda e: x ** e + +def custom_equation(x: int = 0, /, y: int = 0, a: int = 1, b: int = 1, *, c: int = 1) -> float: + return (x**a + y**b) / c + +from collections import defaultdict + +call_count = defaultdict(int) + +def fn_w_counter(): + """ + A function that counts the number of calls and returns a tuple. + + The first element is the total number of calls. + The second element is a dictionary with caller information (caller __name__ and count). + + :return: A tuple of an integer and a dictionary + """ + def wrapper(): + import inspect + caller_name = inspect.stack()[1].function + call_count[caller_name] += 1 + total_calls = sum(call_count.values()) + return total_calls, dict(call_count) + + return wrapper From e7907ad47d78e6fca44bd8ec1d14c18f1d76e22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 18:18:55 +0300 Subject: [PATCH 02/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index 1a89ad63..a900c6a2 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -1,14 +1,19 @@ def custom_power(x=0, /, e=1): + return e: x ** e # Direct calculation + +def custom_equation(x: int = 0, /, y: int = 0, a: int = 1, b: int = 1, *, c: int = 1) -> float: """ - A lambda function that takes two parameters, x and e, and returns x to the power of e. + A function that calculates the custom equation: + + (x**a + y**b) / c - :param x: positional-only parameter (default 0) - :param e: positional-or-keyword parameter (default 1) - :return: x raised to the power of e + :param x: positional-only integer, default 0 + :param y: positional-only integer, default 0 + :param a: positional-or-keyword integer, default 1 + :param b: positional-or-keyword integer, default 1 + :param c: keyword-only integer, default 1 + :return: Result of the equation as a float """ - return lambda e: x ** e - -def custom_equation(x: int = 0, /, y: int = 0, a: int = 1, b: int = 1, *, c: int = 1) -> float: return (x**a + y**b) / c from collections import defaultdict @@ -31,4 +36,4 @@ def wrapper(): total_calls = sum(call_count.values()) return total_calls, dict(call_count) - return wrapper + return wrapper # Return the function From 957b383b8fc89d632c59dc43d9187534b93fc383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 18:27:08 +0300 Subject: [PATCH 03/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index a900c6a2..d7b1bdff 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -1,5 +1,6 @@ def custom_power(x=0, /, e=1): - return e: x ** e # Direct calculation + """Calculate x raised to the power of e.""" + return x ** e # Direct calculation def custom_equation(x: int = 0, /, y: int = 0, a: int = 1, b: int = 1, *, c: int = 1) -> float: """ @@ -20,15 +21,8 @@ def custom_equation(x: int = 0, /, y: int = 0, a: int = 1, b: int = 1, *, c: in call_count = defaultdict(int) -def fn_w_counter(): - """ - A function that counts the number of calls and returns a tuple. - - The first element is the total number of calls. - The second element is a dictionary with caller information (caller __name__ and count). - - :return: A tuple of an integer and a dictionary - """ +def fn_w_counter() -> tuple[int, dict[str, int]]: + """A function that counts the number of calls.""" def wrapper(): import inspect caller_name = inspect.stack()[1].function From 64ecf863a7f96c0ab86d58b1a2eabbc54ebe9a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 18:38:40 +0300 Subject: [PATCH 04/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index d7b1bdff..20096282 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -1,8 +1,8 @@ def custom_power(x=0, /, e=1): """Calculate x raised to the power of e.""" - return x ** e # Direct calculation + return (lambda x, e: x ** e)(x, e) #Direct Calculation -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: """ A function that calculates the custom equation: @@ -21,13 +21,18 @@ def custom_equation(x: int = 0, /, y: int = 0, a: int = 1, b: int = 1, *, c: in call_count = defaultdict(int) -def fn_w_counter() -> tuple[int, dict[str, int]]: +def fn_w_counter()-> tuple[int, dict[str, int]]: """A function that counts the number of calls.""" + call_count = {} def wrapper(): import inspect caller_name = inspect.stack()[1].function + + if caller_name not in call_count: + call_count[caller_name] = 0 + call_count[caller_name] += 1 total_calls = sum(call_count.values()) return total_calls, dict(call_count) - return wrapper # Return the function + return wrapper From a0793a8f44d69dfb672eedc1d899b2b7246688be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 18:42:54 +0300 Subject: [PATCH 05/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index 20096282..da11c0ff 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -2,7 +2,7 @@ def custom_power(x=0, /, e=1): """Calculate x raised to the power of e.""" return (lambda x, e: x ** e)(x, e) #Direct Calculation -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: """ A function that calculates the custom equation: From 95dfed14bbc6eea9a1a52e716fcb5b27b7da5bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 18:49:31 +0300 Subject: [PATCH 06/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index da11c0ff..bec9f846 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -1,8 +1,9 @@ def custom_power(x=0, /, e=1): - """Calculate x raised to the power of e.""" + """A lambda function that calculates x raised to the power of e.""" return (lambda x, e: x ** e)(x, e) #Direct Calculation -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: + """ A function that calculates the custom equation: @@ -21,16 +22,11 @@ def custom_equation(x: int = 0, y: int = 0, a: int = 1, b: int = 1, *, c: int = call_count = defaultdict(int) -def fn_w_counter()-> tuple[int, dict[str, int]]: +def fn_w_counter()-> tuple: """A function that counts the number of calls.""" - call_count = {} def wrapper(): import inspect caller_name = inspect.stack()[1].function - - if caller_name not in call_count: - call_count[caller_name] = 0 - call_count[caller_name] += 1 total_calls = sum(call_count.values()) return total_calls, dict(call_count) From a59ad33e0024025b56ffb1647e27b42f5597f666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 18:52:26 +0300 Subject: [PATCH 07/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index bec9f846..943b9f51 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -22,9 +22,11 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int call_count = defaultdict(int) -def fn_w_counter()-> tuple: +def fn_w_counter()-> tuple[int, dict[str, int]]: """A function that counts the number of calls.""" - def wrapper(): + call_count = defaultdict(int) + + def wrapper() -> tuple[int, dict[str, int]]: import inspect caller_name = inspect.stack()[1].function call_count[caller_name] += 1 From 7724b61f542f1d6778d65ce323bda08952d02bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 19:14:28 +0300 Subject: [PATCH 08/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index 943b9f51..3b2432df 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -1,6 +1,4 @@ -def custom_power(x=0, /, e=1): - """A lambda function that calculates x raised to the power of e.""" - return (lambda x, e: x ** e)(x, e) #Direct Calculation +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: @@ -19,18 +17,19 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int return (x**a + y**b) / c from collections import defaultdict +import inspect call_count = defaultdict(int) -def fn_w_counter()-> tuple[int, dict[str, int]]: +def fn_w_counter()-> (int, dict[str, int]): """A function that counts the number of calls.""" - call_count = defaultdict(int) - - def wrapper() -> tuple[int, dict[str, int]]: - import inspect - caller_name = inspect.stack()[1].function - call_count[caller_name] += 1 - total_calls = sum(call_count.values()) - return total_calls, dict(call_count) - - return wrapper + caller_name = inspect.stack()[1].function # Get the name of the calling function + + # Increment the global counter + call_count[caller_name] += 1 + + # Calculate the total number of calls + total_calls = sum(call_count.values()) + + # Return the total call count and the call counts for each function + return total_calls, dict(call_count) From 693c05bfdae8d06f1a42b4c5da3489fd237653eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 19:28:03 +0300 Subject: [PATCH 09/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index 3b2432df..bdb657bc 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -1,4 +1,4 @@ -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: @@ -21,9 +21,10 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int call_count = defaultdict(int) -def fn_w_counter()-> (int, dict[str, int]): +def fn_w_counter() -> (int, dict[str, int]): """A function that counts the number of calls.""" - caller_name = inspect.stack()[1].function # Get the name of the calling function + caller_name = inspect.currentframe().f_back.f_code.co_name # Get the name of the calling function + # Increment the global counter call_count[caller_name] += 1 From 5bf3e95b5e12586e825f9d767838c490fd4c423a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 19:34:51 +0300 Subject: [PATCH 10/11] functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index bdb657bc..18579bd4 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -22,15 +22,20 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int call_count = defaultdict(int) def fn_w_counter() -> (int, dict[str, int]): - """A function that counts the number of calls.""" - caller_name = inspect.currentframe().f_back.f_code.co_name # Get the name of the calling function - - - # Increment the global counter - call_count[caller_name] += 1 - - # Calculate the total number of calls - total_calls = sum(call_count.values()) - - # Return the total call count and the call counts for each function - return total_calls, dict(call_count) + if not hasattr(fn_w_counter, "call_counter"): + fn_w_counter.call_counter = 0 + fn_w_counter.caller_count_dict = {} + + # Get the name of the caller + caller_name = __name__ + fn_w_counter.call_counter += 1 + + # If the caller is already in the dictionary, increment its value + if caller_name in fn_w_counter.caller_count_dict: + fn_w_counter.caller_count_dict[caller_name] += 1 + else: + # Otherwise, add it as a new key + fn_w_counter.caller_count_dict[caller_name] = 1 + + # Return the total call count and the caller information + return fn_w_counter.call_counter, fn_w_counter.caller_count_dict From 8c6ae782e8a205f7e176eb9e32aae4601b52828b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Mon, 4 Nov 2024 02:26:29 +0300 Subject: [PATCH 11/11] Update functions_miray_cengil.py --- Week04/functions_miray_cengil.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Week04/functions_miray_cengil.py b/Week04/functions_miray_cengil.py index 18579bd4..ddc31cce 100644 --- a/Week04/functions_miray_cengil.py +++ b/Week04/functions_miray_cengil.py @@ -1,7 +1,6 @@ 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: - """ A function that calculates the custom equation: @@ -14,14 +13,12 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int :param c: keyword-only integer, default 1 :return: Result of the equation as a float """ + if c == 0: + raise ValueError("Division by Zero Exception") return (x**a + y**b) / c -from collections import defaultdict -import inspect - -call_count = defaultdict(int) - def fn_w_counter() -> (int, dict[str, int]): + # Keep track of the call count and caller info without using modules if not hasattr(fn_w_counter, "call_counter"): fn_w_counter.call_counter = 0 fn_w_counter.caller_count_dict = {} @@ -30,11 +27,10 @@ def fn_w_counter() -> (int, dict[str, int]): caller_name = __name__ fn_w_counter.call_counter += 1 - # If the caller is already in the dictionary, increment its value + # Increment the call count for this caller if caller_name in fn_w_counter.caller_count_dict: fn_w_counter.caller_count_dict[caller_name] += 1 else: - # Otherwise, add it as a new key fn_w_counter.caller_count_dict[caller_name] = 1 # Return the total call count and the caller information