-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_decorator.py
38 lines (30 loc) · 1.48 KB
/
my_decorator.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
from typing import Callable, List, Tuple, Dict
Decorator = Callable
def get_list_of_kwargs_for_function(
identifiers: str, values: List[Tuple[str, str]]
) -> List[Dict[str, str]]:
print(f"getting list_of_kwargs_for_function,\n{identifiers=}, {values=}")
parsed_identifiers = identifiers.split(",")
list_of_kwargs_for_function = []
for tuple_value in values:
kwargs_for_function = {}
for i, keyword in enumerate(parsed_identifiers):
kwargs_for_function[keyword] = tuple_value[i]
list_of_kwargs_for_function.append(kwargs_for_function)
print(f"{list_of_kwargs_for_function=}")
return list_of_kwargs_for_function
def my_parametrized(identifiers: str, values: List[Tuple[int, int]]) -> Decorator:
def my_parametrized_decorator(function: Callable) -> Callable:
def run_func_parametrized() -> None:
# parse arguments [{"n":0,"expected":0},{"n":1,"expected":1},{"n":2,"expected":1},{"n":20,"expected":6765}]
list_of_kwargs_for_function = get_list_of_kwargs_for_function(
identifiers=identifiers, values=values
)
for kwargs_for_function in list_of_kwargs_for_function:
print(
f"calling function {function.__name__} with {kwargs_for_function}"
)
function(**kwargs_for_function)
return run_func_parametrized
# run function multiple times with parsed arguments
return my_parametrized_decorator