def integer(candidate, param_name: str = None, minimum: float = None, maximum: float = None): """ integer (between minimum and maximum) :param candidate: the object to be tested :param param_name: name of the parameter (to be included in the error message) :param minimum: minimum :param maximum: maximum """ if param_name: debug_str = "Parameter \"{name}\" must be an integer".format( name=param_name) else: debug_str = "Parameter must be an integer" if minimum is not None and maximum is not None: debug_str += " between {min} and {max}".format(min=minimum, max=maximum) elif minimum is not None: debug_str += " >= {}".format(minimum) elif maximum is not None: debug_str += " <= {}".format(maximum) debug_str += "! (got: {})".format(candidate) if type(candidate) is not int: raise InvalidParameters(debug_str) if minimum is not None and candidate < minimum: raise InvalidParameters(debug_str) if maximum is not None and candidate > maximum: raise InvalidParameters(debug_str)
def set_parameter(self, param_name: str, value): if param_name == "merry_go_round": verify.not_negative_integer(value, "merry_go_round") self.num_cycles["merry_go_round"] = value elif param_name == "chunk_blendover": verify.not_negative_integer(value, "chunk_blendover") self.num_cycles["chunk_blendover"] = value elif param_name == "whole_blendover": verify.not_negative_integer(value, "whole_blendover") elif param_name == "velocity": verify.integer(value, "velocity", minimum=1, maximum=10) self.velocity = value else: raise InvalidParameters.unknown(param_name)
def positive_numeric(candidate, param_name: str = None): """ a positive number => greater than 0 :param candidate: the object to be tested :param param_name: name of the parameter (to be included in the error message) """ if type(candidate) not in (float, int) or candidate <= 0: if param_name: debug_str = "Parameter \"{name}\" must be a positive number!".format( name=param_name) else: debug_str = "Parameter must be a positive number!" raise InvalidParameters(debug_str)
def boolean(candidate, param_name: str = None): """ a boolean value: True or False :param candidate: the object to be tested :param param_name: name of the parameter (to be included in the error message) """ if type(candidate) is not bool: if param_name: debug_str = "Parameter \"{name}\" must be a boolean value!".format( name=param_name) else: debug_str = "Parameter must be a boolean value!" raise InvalidParameters(debug_str)
def positive_integer(candidate, param_name: str = None): """ a positive integer => greater than 0 => 1 or above :param candidate: the object to be tested :param param_name: name of the parameter (to be included in the error message) """ if type(candidate) is not int or candidate <= 0: if param_name: debug_str = "Parameter \"{name}\" must be a positive integer!".format( name=param_name) else: debug_str = "Parameter must be a positive integer!" raise InvalidParameters(debug_str)
def not_negative_integer(candidate, param_name: str = None): """ a not-negative integer => 0,1,2,3,... :param candidate: the object to be tested :param param_name: name of the parameter (to be included in the error message) """ if type(candidate) is not int or candidate < 0: if param_name: debug_str = "Parameter \"{name}\" must be a non-negative integer!".format( name=param_name) else: debug_str = "Parameter must be a not-negative integer!" raise InvalidParameters(debug_str)
def rgb_color_tuple(candidate, param_name: str = None): """ An RGB color tuple. It must contain three integer components between 0 and 255. :param candidate: the object to be tested :param param_name: name of the parameter (to be included in the error message) """ if param_name: debug_str = "Parameter \"{name}\" must be an RGB color tuple!".format( name=param_name) else: debug_str = "Parameter must be an RGB color tuple!" if type(candidate) is not tuple: raise InvalidParameters(debug_str) if len(candidate) is not 3: # an rgb tuple has three components raise InvalidParameters(debug_str) for component in candidate: try: numeric(component, minimum=0, maximum=255) except InvalidParameters: raise InvalidParameters(debug_str)