From d474567a43d96edd075a1016f6554135f0b959d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCll=C3=BCler?= <85554705+buraxta@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:01:14 +0300 Subject: [PATCH 1/5] Create functions_burak_gulluler.py --- Week03/functions_burak_gulluler.py | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week03/functions_burak_gulluler.py diff --git a/Week03/functions_burak_gulluler.py b/Week03/functions_burak_gulluler.py new file mode 100644 index 00000000..71ab0314 --- /dev/null +++ b/Week03/functions_burak_gulluler.py @@ -0,0 +1,36 @@ +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 takes 5 arguments, 2 of which are optional, and returns a float + + :param x: (int) first value + :param y: (int) second value + :param a: (int) first exponent + :param b: (int) second exponent + :param c: (int) divisor + :return: (float) result of the equation + """ + return (x**a + y**b) / c + + +def count_calls(): + call_counts = {} # Define a dict to store the call counts + + def decorator(func): + def wrapper(*args, **kwargs): # Define the wrapper function + caller = func.__name__ # Get the name of the function being called + if caller in call_counts: # If the function has been called before + call_counts[caller] += 1 # Increment the call count + else: + call_counts[caller] = 1 # Else, set the call count to 1 + total_calls = sum(call_counts.values()) # Sum the call counts + return total_calls, call_counts # Return the total calls and call counts + return wrapper # Return the wrapper function + return decorator # Return the decorator function + + +@count_calls() +def fn_w_counter(): + pass From cc56f70e0c204822b25cf0b9d6744cc318d01758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCll=C3=BCler?= <85554705+buraxta@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:51:48 +0300 Subject: [PATCH 2/5] Update functions_burak_gulluler.py --- Week03/functions_burak_gulluler.py | 32 +++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Week03/functions_burak_gulluler.py b/Week03/functions_burak_gulluler.py index 71ab0314..9f70bd76 100644 --- a/Week03/functions_burak_gulluler.py +++ b/Week03/functions_burak_gulluler.py @@ -15,22 +15,18 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int return (x**a + y**b) / c -def count_calls(): - call_counts = {} # Define a dict to store the call counts - - def decorator(func): - def wrapper(*args, **kwargs): # Define the wrapper function - caller = func.__name__ # Get the name of the function being called - if caller in call_counts: # If the function has been called before - call_counts[caller] += 1 # Increment the call count - else: - call_counts[caller] = 1 # Else, set the call count to 1 - total_calls = sum(call_counts.values()) # Sum the call counts - return total_calls, call_counts # Return the total calls and call counts - return wrapper # Return the wrapper function - return decorator # Return the decorator function - - -@count_calls() +call_counter = 0 +call_counts = {} + def fn_w_counter(): - pass + global call_counter + call_counter += 1 + + caller = fn_w_counter.__name__ + + if caller in call_counts: + call_counts[caller] += 1 + else: + call_counts[caller] = 1 + + return call_counter, call_counts From 0a131fd7e41d698891b7cf31a9b8731f585651a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCll=C3=BCler?= <85554705+buraxta@users.noreply.github.com> Date: Fri, 20 Oct 2023 15:06:11 +0300 Subject: [PATCH 3/5] Update functions_burak_gulluler.py add return type explicitly --- Week03/functions_burak_gulluler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week03/functions_burak_gulluler.py b/Week03/functions_burak_gulluler.py index 9f70bd76..14eb9dfa 100644 --- a/Week03/functions_burak_gulluler.py +++ b/Week03/functions_burak_gulluler.py @@ -18,7 +18,7 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int call_counter = 0 call_counts = {} -def fn_w_counter(): +def fn_w_counter() -> (int, dict[str, int]): global call_counter call_counter += 1 From 06da3f11bcf62ebc95751a7a5f80e615c17d3acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCll=C3=BCler?= <85554705+buraxta@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:05:31 +0300 Subject: [PATCH 4/5] function attributes added --- Week03/functions_burak_gulluler.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Week03/functions_burak_gulluler.py b/Week03/functions_burak_gulluler.py index 14eb9dfa..e46124a5 100644 --- a/Week03/functions_burak_gulluler.py +++ b/Week03/functions_burak_gulluler.py @@ -4,7 +4,6 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float: """ a function that takes 5 arguments, 2 of which are optional, and returns a float - :param x: (int) first value :param y: (int) second value :param a: (int) first exponent @@ -15,18 +14,25 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int return (x**a + y**b) / c -call_counter = 0 -call_counts = {} -def fn_w_counter() -> (int, dict[str, int]): - global call_counter - call_counter += 1 +def fn_w_counter() -> (int, dict[str, int]): + # Get the caller's name caller = fn_w_counter.__name__ - if caller in call_counts: - call_counts[caller] += 1 + # Initialize counters if they don't exist + if not hasattr(fn_w_counter, 'call_counter'): + fn_w_counter.call_counter = 0 + fn_w_counter.caller_counts = {} + + # Increment the call counter + fn_w_counter.call_counter += 1 + + # Increment the caller-specific counter + if caller in fn_w_counter.caller_counts: + fn_w_counter.caller_counts[caller] += 1 else: - call_counts[caller] = 1 + fn_w_counter.caller_counts[caller] = 1 - return call_counter, call_counts + # Return the results + return fn_w_counter.call_counter, fn_w_counter.caller_counts From 0ffed9b6862fadc16dbc73d7a1a0833453286109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCll=C3=BCler?= <85554705+buraxta@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:14:49 +0300 Subject: [PATCH 5/5] Update functions_burak_gulluler.py --- Week03/functions_burak_gulluler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week03/functions_burak_gulluler.py b/Week03/functions_burak_gulluler.py index e46124a5..c2870199 100644 --- a/Week03/functions_burak_gulluler.py +++ b/Week03/functions_burak_gulluler.py @@ -18,7 +18,7 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int def fn_w_counter() -> (int, dict[str, int]): # Get the caller's name - caller = fn_w_counter.__name__ + caller = globals()['__name__'] # Initialize counters if they don't exist if not hasattr(fn_w_counter, 'call_counter'):