-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabomination-decorator.py
More file actions
25 lines (22 loc) · 973 Bytes
/
Copy pathabomination-decorator.py
File metadata and controls
25 lines (22 loc) · 973 Bytes
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
IN = "in"
OUT = "out"
SEP = ", "
INVALID_INPUT = "Invalid input arguments, expected "
INVALID_OUTPUT = "Invalid output value, expected "
def type_check(io):
"""Check types of input and output values for the function."""
def expected_decorator(*types):
def decorator(func):
def inner(*args, **kwargs):
if io == IN:
unexpected_types = set(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values())))
if unexpected_types:
print(INVALID_INPUT + SEP.join([str(s) for s in types]) + "!")
actual_result = func(*args, **kwargs)
if io == OUT:
if not isinstance(actual_result, types):
print(INVALID_OUTPUT + SEP.join([str(s) for s in types]) + "!")
return actual_result
return inner
return decorator
return expected_decorator