qudi.util.descriptors.CheckedAttribute
- class qudi.util.descriptors.CheckedAttribute(static_validators: ~typing.Iterable[~typing.Callable[[~typing.Any], None]] | None = None, valid_types: ~typing.Iterable[~typing.Type] | None = None, default: ~typing.Any | None = <object object>)[source]
Bases:
qudi.util.descriptors.TypedMixin,qudi.util.descriptors.ValidateMixin,qudi.util.descriptors.DefaultAttributeExtension of DefaultAttribute including optional validation via static or bound validator methods as well as optional type checking via “isinstance”. A given default value is not validated. Type checking is performed before validation. Register bound validator methods via the CheckedAttribute.validator decorator. This decorator can be combined with classmethod/staticmethod decorators in any order.
Example usage:
- def my_static_validator(value):
- if not (0 <= value <= 100):
raise ValueError(‘Value must be number between 0 and 100’)
- class Test:
variable_a = CheckedAttribute([my_static_validator], [int, float], 0) variable_b = CheckedAttribute(valid_types=[str]) _valid_strings = [‘A’, ‘B’, ‘C’]
- def __init__(self):
self.variable_a = 66.7 self.variable_b = ‘B’ assert self.variable_a == 66.7 assert self.variable_b == ‘B’ # The following would raise ValueError # self.variable_a = 101 # self.variable_b = ‘D’
@variable_b.validator @classmethod def _validate_variable_b(cls, value):
- if value not in cls._valid_strings:
raise ValueError(f’Invalid string. Valid strings are: {cls._valid_strings}’)
- __init__(static_validators: ~typing.Iterable[~typing.Callable[[~typing.Any], None]] | None = None, valid_types: ~typing.Iterable[~typing.Type] | None = None, default: ~typing.Any | None = <object object>)[source]
Methods
__init__([static_validators, valid_types, ...])check_type(value)validate(value[, instance])validator(func)Decorator to register either a static or bound validator
- check_type(value: Any) None
- validate(value: Any, instance: Any | None = None) None
- validator(func: staticmethod | classmethod | Callable[[Any], None]) staticmethod | classmethod | Callable[[Any], None]
Decorator to register either a static or bound validator