Skip to content

Commit 2a1d2b8

Browse files
authored
Add function to count calls with caller information
Implements a function to count calls and track callers.
1 parent 71f5b39 commit 2a1d2b8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def fn_w_counter() -> tuple[int, dict[str, int]]:
2+
"""
3+
Counts function calls with caller information.
4+
5+
:return: A tuple containing total call count and a dictionary
6+
mapping caller names to their call counts.
7+
"""
8+
if not hasattr(fn_w_counter, "_total_calls"):
9+
fn_w_counter._total_calls = 0
10+
fn_w_counter._callers = {}
11+
12+
caller = __name__
13+
14+
fn_w_counter._total_calls += 1
15+
fn_w_counter._callers[caller] = fn_w_counter._callers.get(caller, 0) + 1
16+
17+
return fn_w_counter._total_calls, fn_w_counter._callers

0 commit comments

Comments
 (0)