Is there a shorter way to add properties to a class and have validation for setting than using the @property method? The below code is a sample. I would like to be able to many properties in a class that will be validated and doing it this way seems redundant. Any advice is greatly appreciated!
def valid_max(max_val=10):
"""
Decorator to check for valid value of a number between min and max
"""
def valid_value_decorator(func):
def func_wrapper(wraps,value):
if value <= max_val:
return func(wraps,value)
else:
raise(Exception('Value above max'))
return func_wrapper
return valid_value_decorator
class Test(object):
def __init__(self):
self._x=0
self._y=0
@property
def x(self):
return self._x
@x.setter
@valid_max(max_val=10)
def x(self,data):
self._x = data
@property
def y(self):
return self._y
@y.setter
@valid_max(max_val=10)
def y(self,data):
self._y = data