1def check_input_arguments(args, types):
2 for arg in args:
3 if not isinstance(arg, types):
4 print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types)))
5 return False
6 return True
7
8
9def type_check(val):
10 """Check the types of the input and output of a function."""
11
12 def wrapper(*types):
13 def decorator(func):
14 def check_types(*args, **kwargs):
15 if isinstance(val, str):
16 if val == 'in':
17 check_input_arguments(args,types)
18
19 res = func(*args, **kwargs)
20
21 if val == 'out':
22 if not isinstance(res, types):
23 print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types)))
24
25 return res
26 return check_types
27 return decorator
28 return wrapper
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK
| t | 1 | def check_input_arguments(args, types): | t | 1 | def check_input_arguments(args, types): |
| 2 | for arg in args: | 2 | for arg in args: | ||
| 3 | if not isinstance(arg, types): | 3 | if not isinstance(arg, types): | ||
| 4 | print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types))) | 4 | print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types))) | ||
| 5 | return False | 5 | return False | ||
| 6 | return True | 6 | return True | ||
| 7 | 7 | ||||
| 8 | 8 | ||||
| 9 | def type_check(val): | 9 | def type_check(val): | ||
| 10 | """Check the types of the input and output of a function.""" | 10 | """Check the types of the input and output of a function.""" | ||
| 11 | 11 | ||||
| 12 | def wrapper(*types): | 12 | def wrapper(*types): | ||
| 13 | def decorator(func): | 13 | def decorator(func): | ||
| 14 | def check_types(*args, **kwargs): | 14 | def check_types(*args, **kwargs): | ||
| 15 | if isinstance(val, str): | 15 | if isinstance(val, str): | ||
| 16 | if val == 'in': | 16 | if val == 'in': | ||
| 17 | check_input_arguments(args,types) | 17 | check_input_arguments(args,types) | ||
| 18 | 18 | ||||
| 19 | res = func(*args, **kwargs) | 19 | res = func(*args, **kwargs) | ||
| 20 | 20 | ||||
| 21 | if val == 'out': | 21 | if val == 'out': | ||
| 22 | if not isinstance(res, types): | 22 | if not isinstance(res, types): | ||
| 23 | print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types))) | 23 | print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types))) | ||
| 24 | 24 | ||||
| 25 | return res | 25 | return res | ||
| 26 | return check_types | 26 | return check_types | ||
| 27 | return decorator | 27 | return decorator | ||
| 28 | return wrapper | 28 | return wrapper |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def check_input_arguments(args, types): | f | 1 | def check_input_arguments(args, types): |
| 2 | for arg in args: | 2 | for arg in args: | ||
| 3 | if not isinstance(arg, types): | 3 | if not isinstance(arg, types): | ||
| 4 | print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types))) | 4 | print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types))) | ||
| 5 | return False | 5 | return False | ||
| 6 | return True | 6 | return True | ||
| 7 | 7 | ||||
| 8 | 8 | ||||
| 9 | def type_check(val): | 9 | def type_check(val): | ||
| n | 10 | """Check the types of the input and output of a fuction.""" | n | 10 | """Check the types of the input and output of a function.""" |
| 11 | 11 | ||||
| 12 | def wrapper(*types): | 12 | def wrapper(*types): | ||
| 13 | def decorator(func): | 13 | def decorator(func): | ||
| 14 | def check_types(*args, **kwargs): | 14 | def check_types(*args, **kwargs): | ||
| 15 | if isinstance(val, str): | 15 | if isinstance(val, str): | ||
| 16 | if val == 'in': | 16 | if val == 'in': | ||
| 17 | check_input_arguments(args,types) | 17 | check_input_arguments(args,types) | ||
| 18 | 18 | ||||
| 19 | res = func(*args, **kwargs) | 19 | res = func(*args, **kwargs) | ||
| 20 | 20 | ||||
| 21 | if val == 'out': | 21 | if val == 'out': | ||
| 22 | if not isinstance(res, types): | 22 | if not isinstance(res, types): | ||
| 23 | print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types))) | 23 | print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types))) | ||
| t | t | 24 | |||
| 24 | return res | 25 | return res | ||
| 25 | return check_types | 26 | return check_types | ||
| 26 | return decorator | 27 | return decorator | ||
| 27 | return wrapper | 28 | return wrapper |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||