1def type_check(mode):
2 def get_types(*types):
3 def validate_corresponding_types(func):
4 def wrapper(*args, **kwargs):
5 output_value = 0
6
7 if mode == "in":
8 arg_list = (list(map(lambda x: ('', x), args))
9 + list(kwargs.items()))
10
11 for _, value in arg_list:
12 if not isinstance(value, types):
13 formatted_types = ", ".join([str(val_type)
14 for val_type in types])
15
16 print(f"Invalid input arguments, expected "
17 f"{formatted_types}!")
18 break
19
20 output_value = func(*args, **kwargs)
21
22 elif mode == "out":
23 output_value = func(*args, **kwargs)
24
25 if not isinstance(output_value, types):
26 formatted_types = ", ".join([str(val_type)
27 for val_type in types])
28
29 print(f"Invalid output value, expected "
30 f"{formatted_types}!")
31
32 return output_value
33 return wrapper
34 return validate_corresponding_types
35 return get_types
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK
| f | 1 | def type_check(mode): | f | 1 | def type_check(mode): |
| 2 | def get_types(*types): | 2 | def get_types(*types): | ||
| 3 | def validate_corresponding_types(func): | 3 | def validate_corresponding_types(func): | ||
| 4 | def wrapper(*args, **kwargs): | 4 | def wrapper(*args, **kwargs): | ||
| 5 | output_value = 0 | 5 | output_value = 0 | ||
| 6 | 6 | ||||
| 7 | if mode == "in": | 7 | if mode == "in": | ||
| 8 | arg_list = (list(map(lambda x: ('', x), args)) | 8 | arg_list = (list(map(lambda x: ('', x), args)) | ||
| 9 | + list(kwargs.items())) | 9 | + list(kwargs.items())) | ||
| 10 | 10 | ||||
| t | 11 | for key, value in arg_list: | t | 11 | for _, value in arg_list: |
| 12 | if not isinstance(value, types): | 12 | if not isinstance(value, types): | ||
| 13 | formatted_types = ", ".join([str(val_type) | 13 | formatted_types = ", ".join([str(val_type) | ||
| 14 | for val_type in types]) | 14 | for val_type in types]) | ||
| 15 | 15 | ||||
| 16 | print(f"Invalid input arguments, expected " | 16 | print(f"Invalid input arguments, expected " | ||
| 17 | f"{formatted_types}!") | 17 | f"{formatted_types}!") | ||
| 18 | break | 18 | break | ||
| 19 | 19 | ||||
| 20 | output_value = func(*args, **kwargs) | 20 | output_value = func(*args, **kwargs) | ||
| 21 | 21 | ||||
| 22 | elif mode == "out": | 22 | elif mode == "out": | ||
| 23 | output_value = func(*args, **kwargs) | 23 | output_value = func(*args, **kwargs) | ||
| 24 | 24 | ||||
| 25 | if not isinstance(output_value, types): | 25 | if not isinstance(output_value, types): | ||
| 26 | formatted_types = ", ".join([str(val_type) | 26 | formatted_types = ", ".join([str(val_type) | ||
| 27 | for val_type in types]) | 27 | for val_type in types]) | ||
| 28 | 28 | ||||
| 29 | print(f"Invalid output value, expected " | 29 | print(f"Invalid output value, expected " | ||
| 30 | f"{formatted_types}!") | 30 | f"{formatted_types}!") | ||
| 31 | 31 | ||||
| 32 | return output_value | 32 | return output_value | ||
| 33 | return wrapper | 33 | return wrapper | ||
| 34 | return validate_corresponding_types | 34 | return validate_corresponding_types | ||
| 35 | return get_types | 35 | return get_types |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||