[docs]classNumberRangeModel(ValidationModel[T]):""" A ValidationModel that validates numeric values within a specified range. Attributes: gt (Optional[Number]): The minimum value (exclusive). lt (Optional[Number]): The maximum value (exclusive). ge (Optional[Number]): The minimum value (inclusive). le (Optional[Number]): The maximum value (inclusive). """def__init__(self,gt:Optional[Number]=None,lt:Optional[Number]=None,ge:Optional[Number]=None,le:Optional[Number]=None,)->None:""" Initializes the NumberRangeModel with the specified range constraints. """super().__init__(gt=gt,lt=lt,ge=ge,le=le)
[docs]defvalidate_arguments(self)->None:""" Validates the range constraints provided to the NumberRangeModel. """ifself.gtisnotNoneandself.geisnotNone:raiseValueError("gt and ge cannot be used together")ifself.ltisnotNoneandself.leisnotNone:raiseValueError("lt and le cannot be used together")min_value=self.gtorself.gemax_value=self.ltorself.leifmin_valueisnotNoneandmax_valueisnotNoneandmin_value>=max_value:raiseValueError("Range maximum cannot be less than or equal to minimum")
[docs]defvalidate_value(self,value:Number|str)->Number:""" Validates the given value and returns the validated value. Args: value (Number | str): The value to be validated. Returns: Number: The validated value. Raises: TypeValidationError: If the value is not a number or a string. InputConversionError: If the value cannot be converted to the expected numeric type. RangeError: If the value is outside the specified range. """ifisinstance(value,str):value=self.convert(value)elifnotisinstance(value,Number):raiseTypeValidationError("Invalid type for number validation")self.validate_range(value)returnvalue
[docs]@abstractmethoddefconvert(self,value:str)->Number:""" Converts the given string value to the expected numeric type. Args: value (str): The string value to be converted. Returns: Number: The converted numeric value. Raises: InputConversionError: If the value cannot be converted to the expected numeric type. """pass
[docs]defvalidate_range(self,value:Number)->None:""" Validates that the given value is within the specified range. Args: value (Number): The value to be validated. Raises: RangeError: If the value is outside the specified range. """ifself.gtisnotNoneandvalue<=self.gt:raiseRangeError(f"Value must be greater than {self.gt}")ifself.geisnotNoneandvalue<self.ge:raiseRangeError(f"Value must be greater than or equal to {self.ge}")ifself.ltisnotNoneandvalue>=self.lt:raiseRangeError(f"Value must be less than {self.lt}")ifself.leisnotNoneandvalue>self.le:raiseRangeError(f"Value must be less than or equal to {self.le}")
[docs]classConstrainedInteger(NumberRangeModel[int]):""" A NumberRangeModel that validates integer values within a specified range. """
[docs]defconvert(self,value:str)->int:""" Converts the given string value to an integer. Args: value (str): The string value to be converted. Returns: int: The converted integer value. Raises: InputConversionError: If the value cannot be converted to an integer. """try:returnint(value)exceptValueErrorase:raiseInputConversionError("Invalid input for integer conversion")frome
[docs]classConstrainedFloat(NumberRangeModel[float]):""" A NumberRangeModel that validates float values within a specified range. """
[docs]defconvert(self,value:str)->float:""" Converts the given string value to a float. Args: value (str): The string value to be converted. Returns: float: The converted float value. Raises: InputConversionError: If the value cannot be converted to a float. """try:returnfloat(value)exceptValueErrorase:raiseInputConversionError("Invalid input for float conversion")frome