1+ import inspect
2+
3+ # x is positional-only (default 0), e is positional-or-keyword (default 1)
4+ custom_power = lambda x = 0 , / , e = 1 : x ** e
5+
6+
7+ def custom_equation (
8+ x : int = 0 , y : int = 0 , / , a : int = 1 , b : int = 1 , * , c : int = 1
9+ ) -> float :
10+ """
11+ Calculate (x**a + y**b) / c.
12+
13+ :param x: Positional-only base of the first term.
14+ :type x: int
15+ :param y: Positional-only base of the second term.
16+ :type y: int
17+ :param a: Exponent applied to x.
18+ :type a: int
19+ :param b: Exponent applied to y.
20+ :type b: int
21+ :param c: Keyword-only divisor.
22+ :type c: int
23+ :return: The value of (x**a + y**b) / c.
24+ :rtype: float
25+ """
26+ return (x ** a + y ** b ) / c
27+
28+
29+ _total_calls : int = 0
30+ _calls_by_caller : dict [str , int ] = {}
31+
32+
33+ def fn_w_counter () -> tuple [int , dict [str , int ]]:
34+ """
35+ Count how many times this function has been called, and by whom.
36+
37+ :return: A tuple of the total number of calls and a dictionary mapping
38+ each caller's ``__name__`` to the number of calls it made.
39+ :rtype: tuple[int, dict[str, int]]
40+ """
41+ global _total_calls
42+ caller = inspect .currentframe ().f_back .f_globals ["__name__" ]
43+ _total_calls += 1
44+ _calls_by_caller [caller ] = _calls_by_caller .get (caller , 0 ) + 1
45+ return _total_calls , dict (_calls_by_caller )
46+
47+
48+ if __name__ == "__main__" :
49+ assert custom_power (2 ) == 2
50+ assert custom_power (2 , 3 ) == 8
51+ assert custom_power (2 , e = 2 ) == 4
52+
53+ assert custom_equation (2 , 3 ) == 5.0
54+ assert custom_equation (2 , 3 , 2 ) == 7.0
55+ assert custom_equation (2 , 3 , 2 , 3 ) == 31.0
56+ assert custom_equation (3 , 5 , a = 2 , b = 3 , c = 4 ) == 33.5
57+ assert custom_equation (3 , 5 , 2 , b = 3 , c = 4 ) == 33.5
58+ assert custom_equation (3 , 5 , 2 , 3 , c = 4 ) == 33.5
59+
60+ for i in range (10 ):
61+ fn_w_counter ()
62+ assert fn_w_counter () == (11 , {"__main__" : 11 })
63+ print ("All examples pass." )
0 commit comments