scientific-programming-exer.../ex_36.py
2019-01-09 14:38:46 +01:00

22 lines
612 B
Python

import sys
def check_range(f):
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)