FloatAttribute
- class dtaianomaly.type_validation.FloatAttribute(minimum: float = None, maximum: float = None, inclusive_minimum: bool = True, inclusive_maximum: bool = True)[source]
Validator for floats.
Check wether a given value is a valid float. By default, any float is valid, but it is also possible to put an upper- and lower-bound on the valid range of floats. Both upper- and lower-bound can be an inclusive or exclusive bound.
- Parameters:
- minimumfloat, default=None
The minimum value a given float can have. If
None, there is no lower-bound.- maximumfloat, default=None
The maximum value a given float can have. If
None, there is no upper-bound.- inclusive_minimumbool, default=True
Whether the lower-bound itself is valid or not.
- inclusive_maximumbool, default=True
Whether the upper-bound itself is valid or not.
Examples
>>> from dtaianomaly.type_validation import FloatAttribute >>> a_float = FloatAttribute(minimum=0.0, maximum=1.0, inclusive_maximum=False) >>> a_float.raise_error_if_invalid(0.0, "my_attribute", "MyClass") # No error >>> a_float.raise_error_if_invalid(0.5, "my_attribute", "MyClass") # No error >>> a_float.raise_error_if_invalid("not-a-float", "my_attribute", "MyClass") Traceback (most recent call last): ... TypeError: Attribute 'my_attribute' in class 'MyClass' must be of type float, but received 'not-a-float' of type <class 'str'>! >>> a_float.raise_error_if_invalid(1.0, "my_attribute", "MyClass") Traceback (most recent call last): ... ValueError: Attribute 'my_attribute' in class 'MyClass' must be in range [0.0, 1.0[, but received '1.0'!
- raise_error_if_invalid(value, name: str, class_name: str) None
Raise an error if the given value is invalid.
Check if the type and the exact value are permitted, according to the rules of this attribute validation. If either the type or the value is invalid, an error is raised accordingly. Otherwise, nothing happens.
- Parameters:
- valueany
The value to verify.
- namestr
The name of the attribute that is being checked. Used for generating clear error messages, if needed.
- class_namestr
The name of the class to which the attribute belongs. Used for generating clear error messages, if needed.
- Raises:
- TypeError
If the type of the given value is invalid according to the rules of this BaseAttributeValidation.
- ValueError
If the value of the given value is invalid according to the rules of this BaseAttributeValidation.