|
| 1 | +import inspect |
| 2 | + |
| 3 | +# 1. custom_power: lambda function with positional-only x |
| 4 | +custom_power = lambda x=0, /, e=1: x**e |
| 5 | + |
| 6 | +# 2. custom_equation: positional-only, positional-or-keyword, and keyword-only params |
| 7 | +def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float: |
| 8 | + """ |
| 9 | + Calculates (x**a + y**b) / c with specific parameter constraints. |
| 10 | +
|
| 11 | + :param x: Base 1, positional-only |
| 12 | + :param y: Base 2, positional-only |
| 13 | + :param a: Exponent 1, positional-or-keyword |
| 14 | + :param b: Exponent 2, positional-or-keyword |
| 15 | + :param c: Divisor, keyword-only |
| 16 | + :returns: Result as float |
| 17 | + """ |
| 18 | + return float((x**a + y**b) / c) |
| 19 | + |
| 20 | +# 3. fn_w_counter: tracks total calls and caller names |
| 21 | +def fn_w_counter() -> tuple[int, dict]: |
| 22 | + """ |
| 23 | + Counts the number of calls and tracks the caller (__name__). |
| 24 | + """ |
| 25 | + if not hasattr(fn_w_counter, "calls"): |
| 26 | + fn_w_counter.calls = 0 |
| 27 | + fn_w_counter.callers = {} |
| 28 | + |
| 29 | + caller_frame = inspect.currentframe().f_back |
| 30 | + caller_name = caller_frame.f_globals.get('__name__', 'unknown') |
| 31 | + |
| 32 | + fn_w_counter.calls += 1 |
| 33 | + fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1 |
| 34 | + |
| 35 | + return (fn_w_counter.calls, fn_w_counter.callers) |
0 commit comments