-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_2.py
43 lines (34 loc) · 1.28 KB
/
task_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""A task for creating a logging decorator."""
import inspect
def bind_args_kwargs(func, output, *args, **kwargs):
"""Return a string for printing logging."""
sig = inspect.signature(func)
bound = sig.bind(*args, **kwargs)
bound.apply_defaults()
args = [f"{i}={v}" for i, v in bound.arguments.items()]
out = f"{func.__name__}({', '.join(args)}) -> {output}"
return out
def print_input_output(func):
"""
A decorator to print function inputs and outputs.
Examples
--------
>>> @print_input_output
... def my_func(param_1, param_2)
... return param_1 + param_2
>>> my_func(1, param_2=2)
>>> # prints "my_func(param_1=1, param_2=2) -> 3"
"""
# hints: use bind_args_kwargs to format print string.
# also https://realpython.com/python-kwargs-and-args/ if you are new to
# *args and **kwargs
if __name__ == "__main__":
# tests for bind_args_kwargs
def test_func(a, b=2):
"""A test function for bing_args_kwargs."""
return a + b
expected = "test_func(a=1, b=2) -> 3"
assert bind_args_kwargs(test_func, 3, a=1) == expected
assert bind_args_kwargs(test_func, 3, a=1, b=2) == expected
expected_2 = "test_func(a=3, b=3) -> 6"
assert bind_args_kwargs(test_func, 6, a=3, b=3) == expected_2