2019-01-09 13:38:46 +00:00
|
|
|
import sys
|
2019-01-09 13:19:33 +00:00
|
|
|
|
|
|
|
def check_range(f):
|
2019-01-09 13:38:46 +00:00
|
|
|
if sys.version_info.major < 3 or sys.version_info.minor < 6:
|
|
|
|
raise RuntimeError("check_range requires python >= 3.6")
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
total_args = list(args) + list(kwargs.values())
|
|
|
|
for argv, (argname, argrange) in zip(total_args, f.__annotations__.items()):
|
|
|
|
if(argv < argrange[0] or argv > argrange[1]):
|
|
|
|
raise ValueError("Argument {} ({}) out or range {}".format(argname, argv, argrange))
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
@check_range
|
|
|
|
def f(a: (0, 100), b: (100, 300)):
|
|
|
|
return a + b
|
|
|
|
|
|
|
|
if( __name__ == "__main__"):
|
|
|
|
f(1, 101)
|
|
|
|
f(-2, 400)
|
|
|
|
|