2.4. fancytypes.Checkable
- class Checkable(skip_validation_and_conversion=False, skip_cls_tests=False, **kwargs)[source]
Bases:
objectA type that can perform user-defined validations and conversions of a set of parameters upon construction.
One cannot construct an instance of the class
fancytypes.Checkablewithout raising an exception. In order to make use of this class, one must create a subclass that inherits fromfancytypes.Checkableand then override the class methodget_validation_and_conversion_funcsin a way that is consistent with the method’s description.- Parameters:
skip_validation_and_conversion (bool, optional) –
Let
validation_and_conversion_funcsandcore_attrsdenote the attributesvalidation_and_conversion_funcsandcore_attrsrespectively, both of which being dict objects.Let
params_to_be_mapped_to_core_attrsdenote the dict representation of the constructor parameters excluding the parametersskip_validation_and_conversionandskip_cls_tests, where each dict keykeyis a different constructor parameter name, excluding the names"skip_validation_and_conversion"and"skip_cls_tests", andparams_to_be_mapped_to_core_attrs[key]would yield the value of the constructor parameter with the name given bykey.If
skip_validation_and_conversionis set toFalse, then for each keykeyinparams_to_be_mapped_to_core_attrs,core_attrs[key]is set tovalidation_and_conversion_funcs[key] (params_to_be_mapped_to_core_attrs).Otherwise, if
skip_validation_and_conversionis set toTrue, thencore_attrsis set toparams_to_be_mapped_to_core_attrs.copy(). This option is desired primarily when the user wants to avoid potentially expensive deep copies and/or conversions of the dict values ofparams_to_be_mapped_to_core_attrs, as it is guaranteed that no copies or conversions are made in this case.skip_cls_test (bool, optional) –
If
skip_cls_testis set toFalse, then upon construction, tests will be performed to check whether the class was properly defined. If any of the tests fail, an exception will be raised.Otherwise, if
skip_cls_testis set toTrue, these tests will be skipped.One should only skip the tests if they are sure that the class is properly defined. Skipping the tests will yield some improvement in performance.
**kwargs – The remaining constructor parameters.
Methods
Return the core attributes.
Return the validation and conversion functions.
Attributes
The "core attributes".
The validation and conversion functions.
- property core_attrs
The “core attributes”.
The keys of
core_attrsare the same as the attributevalidation_and_conversion_funcs, which is also a dict object.Note that
core_attrsshould be considered read-only.- Type:
dict
- get_core_attrs(deep_copy=True)[source]
Return the core attributes.
- Parameters:
deep_copy (bool, optional) –
Let
core_attrsdenote the attributecore_attrs, which is a dict object.If
deep_copyis set toTrue, then a deep copy ofcore_attrsis returned. Otherwise, a shallow copy ofcore_attrsis returned.- Returns:
core_attrs – The attribute
core_attrs.- Return type:
dict
- classmethod get_validation_and_conversion_funcs()[source]
Return the validation and conversion functions.
- Returns:
validation_and_conversion_funcs – The attribute
validation_and_conversion_funcs.- Return type:
dict
- property validation_and_conversion_funcs
The validation and conversion functions.
The keys of
validation_and_conversion_funcsare the names of the constructor parameters, excludingskip_validation_and_conversionif it exists as a construction parameter.Let
core_attrsdenote the attributecore_attrs, which is also a dict object.For each dict key
keyincore_attrs,validation_and_conversion_funcs[key](core_attrs)is expected to not raise an exception.Note that
validation_and_conversion_funcsshould be considered read-only.- Type:
dict