Skip to content

Commit 5bf3e95

Browse files
authored
functions_miray_cengil.py
1 parent 693c05b commit 5bf3e95

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Week04/functions_miray_cengil.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int
2222
call_count = defaultdict(int)
2323

2424
def fn_w_counter() -> (int, dict[str, int]):
25-
"""A function that counts the number of calls."""
26-
caller_name = inspect.currentframe().f_back.f_code.co_name # Get the name of the calling function
27-
28-
29-
# Increment the global counter
30-
call_count[caller_name] += 1
31-
32-
# Calculate the total number of calls
33-
total_calls = sum(call_count.values())
34-
35-
# Return the total call count and the call counts for each function
36-
return total_calls, dict(call_count)
25+
if not hasattr(fn_w_counter, "call_counter"):
26+
fn_w_counter.call_counter = 0
27+
fn_w_counter.caller_count_dict = {}
28+
29+
# Get the name of the caller
30+
caller_name = __name__
31+
fn_w_counter.call_counter += 1
32+
33+
# If the caller is already in the dictionary, increment its value
34+
if caller_name in fn_w_counter.caller_count_dict:
35+
fn_w_counter.caller_count_dict[caller_name] += 1
36+
else:
37+
# Otherwise, add it as a new key
38+
fn_w_counter.caller_count_dict[caller_name] = 1
39+
40+
# Return the total call count and the caller information
41+
return fn_w_counter.call_counter, fn_w_counter.caller_count_dict

0 commit comments

Comments
 (0)