|
1 | | -import inspect |
| 1 | +"""Week04 - Introduction to Python: functions.""" |
2 | 2 |
|
3 | | -custom_power = lambda x=0, /, e=1: x**e # noqa: E731 |
| 3 | +# x positional-only (default 0), e positional-or-keyword (default 1) |
| 4 | +custom_power = lambda x=0, /, e=1: x ** e |
4 | 5 |
|
5 | 6 |
|
6 | | -def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float: |
| 7 | +def custom_equation( |
| 8 | + x: int = 0, |
| 9 | + y: int = 0, |
| 10 | + /, |
| 11 | + a: int = 1, |
| 12 | + b: int = 1, |
| 13 | + *, |
| 14 | + c: int = 1, |
| 15 | +) -> float: |
7 | 16 | """ |
8 | | - Calculate custom equation. |
| 17 | + Calculate (x**a + y**b) / c. |
9 | 18 |
|
10 | | - :param x: Positional-only base for first term, default 0 |
| 19 | + :param x: first base (positional-only) |
11 | 20 | :type x: int |
12 | | - :param y: Positional-only base for second term, default 0 |
| 21 | + :param y: second base (positional-only) |
13 | 22 | :type y: int |
14 | | - :param a: Exponent for first term, default 1 |
| 23 | + :param a: exponent of x (positional-or-keyword) |
15 | 24 | :type a: int |
16 | | - :param b: Exponent for second term, default 1 |
| 25 | + :param b: exponent of y (positional-or-keyword) |
17 | 26 | :type b: int |
18 | | - :param c: Keyword-only divisor, default 1 |
| 27 | + :param c: divisor (keyword-only) |
19 | 28 | :type c: int |
20 | | - :return: Computed float result of (x**a + y**b) / c |
| 29 | + :return: the value of (x**a + y**b) / c |
21 | 30 | :rtype: float |
22 | 31 | """ |
23 | | - return float((x**a + y**b) / c) |
| 32 | + return (x ** a + y ** b) / c |
| 33 | + |
| 34 | + |
| 35 | +_call_counts: dict[str, int] = {} |
| 36 | +_total_calls: int = 0 |
24 | 37 |
|
25 | 38 |
|
26 | 39 | def fn_w_counter() -> (int, dict[str, int]): |
27 | | - if not hasattr(fn_w_counter, "total_calls"): |
28 | | - fn_w_counter.total_calls = 0 |
29 | | - fn_w_counter.callers = {} |
| 40 | + """ |
| 41 | + Count the calls made to this function together with caller information. |
| 42 | +
|
| 43 | + :return: total number of calls and a dictionary mapping each caller's |
| 44 | + ``__name__`` to the number of calls made from that caller |
| 45 | + :rtype: tuple[int, dict[str, int]] |
| 46 | + """ |
| 47 | + global _total_calls |
| 48 | + caller = __name__ |
| 49 | + _total_calls += 1 |
| 50 | + _call_counts[caller] = _call_counts.get(caller, 0) + 1 |
| 51 | + return _total_calls, dict(_call_counts) |
30 | 52 |
|
31 | | - fn_w_counter.total_calls += 1 |
32 | 53 |
|
33 | | - caller_frame = inspect.currentframe().f_back |
34 | | - caller_name = caller_frame.f_globals.get("__name__", "__main__") |
| 54 | +if __name__ == "__main__": |
| 55 | + assert custom_power(2) == 2 |
| 56 | + assert custom_power(2, 3) == 8 |
| 57 | + assert custom_power(2, e=2) == 4 |
35 | 58 |
|
36 | | - fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1 |
| 59 | + assert custom_equation(2, 3) == 5.0 |
| 60 | + assert custom_equation(2, 3, 2) == 7.0 |
| 61 | + assert custom_equation(2, 3, 2, 3) == 31.0 |
| 62 | + assert custom_equation(3, 5, a=2, b=3, c=4) == 33.5 |
| 63 | + assert custom_equation(3, 5, 2, b=3, c=4) == 33.5 |
| 64 | + assert custom_equation(3, 5, 2, 3, c=4) == 33.5 |
37 | 65 |
|
38 | | - return fn_w_counter.total_calls, fn_w_counter.callers |
| 66 | + for i in range(10): |
| 67 | + fn_w_counter() |
| 68 | + assert fn_w_counter() == (11, {'__main__': 11}) |
| 69 | + print("All examples passed.") |
0 commit comments