1def identity_function(function):
2 return function
3
4def type_check(arg_type):
5 if type(arg_type) is not str or (arg_type != "in" and arg_type != "out"):
6 print("Error!!!")
7 return identity_function
8
9 def type_decorator(*expected_types):
10
11 def type_wrapper(function):
12
13 def type_validator(*args, **kwargs):
14
15 def print_error():
16 expected_types_str = ', '.join(map(str, expected_types))
17 if arg_type == "in":
18 print(f"Invalid input arguments, expected {expected_types_str}!")
19 elif arg_type == "out":
20 print(f"Invalid output value, expected {expected_types_str}!")
21
22 if arg_type == "in":
23 invalid_args = [
24 current_arg for current_arg in args
25 if type(current_arg) not in expected_types
26 ]
27 invalid_kwargs = [
28 current_value for current_value in kwargs.values()
29 if type(current_value) not in expected_types
30 ]
31 if invalid_args or invalid_kwargs:
32 print_error()
33
34 result = function(*args, **kwargs)
35
36 if arg_type == "out" and type(result) not in expected_types:
37 print_error()
38
39 return result
40
41 return type_validator
42
43 return type_wrapper
44
45 return type_decorator
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK
n | n | 1 | def identity_function(function): | ||
2 | return function | ||||
3 | |||||
1 | def type_check(arg_type): | 4 | def type_check(arg_type): | ||
n | n | 5 | if type(arg_type) is not str or (arg_type != "in" and arg_type != "out"): | ||
6 | print("Error!!!") | ||||
7 | return identity_function | ||||
2 | 8 | ||||
3 | def type_decorator(*expected_types): | 9 | def type_decorator(*expected_types): | ||
4 | 10 | ||||
5 | def type_wrapper(function): | 11 | def type_wrapper(function): | ||
6 | 12 | ||||
7 | def type_validator(*args, **kwargs): | 13 | def type_validator(*args, **kwargs): | ||
8 | 14 | ||||
9 | def print_error(): | 15 | def print_error(): | ||
10 | expected_types_str = ', '.join(map(str, expected_types)) | 16 | expected_types_str = ', '.join(map(str, expected_types)) | ||
11 | if arg_type == "in": | 17 | if arg_type == "in": | ||
12 | print(f"Invalid input arguments, expected {expected_types_str}!") | 18 | print(f"Invalid input arguments, expected {expected_types_str}!") | ||
13 | elif arg_type == "out": | 19 | elif arg_type == "out": | ||
14 | print(f"Invalid output value, expected {expected_types_str}!") | 20 | print(f"Invalid output value, expected {expected_types_str}!") | ||
15 | 21 | ||||
16 | if arg_type == "in": | 22 | if arg_type == "in": | ||
t | 17 | invalid_args = [current_arg for current_arg in args if type(current_arg) not in expected_types] | t | 23 | invalid_args = [ |
18 | invalid_kwargs = [current_value for current_value in kwargs.values() if type(current_value) not in expected_types] | 24 | current_arg for current_arg in args | ||
25 | if type(current_arg) not in expected_types | ||||
26 | ] | ||||
27 | invalid_kwargs = [ | ||||
28 | current_value for current_value in kwargs.values() | ||||
29 | if type(current_value) not in expected_types | ||||
30 | ] | ||||
19 | if invalid_args or invalid_kwargs: | 31 | if invalid_args or invalid_kwargs: | ||
20 | print_error() | 32 | print_error() | ||
21 | 33 | ||||
22 | result = function(*args, **kwargs) | 34 | result = function(*args, **kwargs) | ||
23 | 35 | ||||
24 | if arg_type == "out" and type(result) not in expected_types: | 36 | if arg_type == "out" and type(result) not in expected_types: | ||
25 | print_error() | 37 | print_error() | ||
26 | 38 | ||||
27 | return result | 39 | return result | ||
28 | 40 | ||||
29 | return type_validator | 41 | return type_validator | ||
30 | 42 | ||||
31 | return type_wrapper | 43 | return type_wrapper | ||
32 | 44 | ||||
33 | return type_decorator | 45 | return type_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|