2.7. fancytypes.Updatable
- class Updatable(skip_validation_and_conversion=False, skip_cls_tests=False, **kwargs)[source]
Bases:
Checkable
A type that can perform user-defined validations and conversions of a set of parameters upon construction, and that has an updatable subset of attributes.
One cannot construct an instance of the class
fancytypes.Updatable
without raising an exception. In order to make use of this class, one must create a subclass that inherits fromfancytypes.Updatable
and then override the class methodget_validation_and_conversion_funcs
in a way that is consistent with the method’s description.- Parameters:
skip_validation_and_conversion (bool, optional) –
Let
validation_and_conversion_funcs
andcore_attrs
denote the attributesvalidation_and_conversion_funcs
andcore_attrs
respectively, both of which being dict objects.Let
params_to_be_mapped_to_core_attrs
denote the dict representation of the constructor parameters excluding the parametersskip_validation_and_conversion
andskip_cls_tests
, where each dict keykey
is 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_conversion
is set toFalse
, then for each keykey
inparams_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_conversion
is set toTrue
, thencore_attrs
is 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_test
is 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_test
is 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.
Update a subset of the core attributes.
Attributes
The "core attributes".
The validation and conversion functions.
- property core_attrs
The “core attributes”.
The keys of
core_attrs
are the same as the attributevalidation_and_conversion_funcs
, which is also a dict object.Note that
core_attrs
should be considered read-only.- Type:
dict
- get_core_attrs(deep_copy=True)
Return the core attributes.
- Parameters:
deep_copy (bool, optional) –
Let
core_attrs
denote the attributecore_attrs
, which is a dict object.If
deep_copy
is set toTrue
, then a deep copy ofcore_attrs
is returned. Otherwise, a shallow copy ofcore_attrs
is returned.- Returns:
core_attrs – The attribute
core_attrs
.- Return type:
dict
- classmethod get_validation_and_conversion_funcs()
Return the validation and conversion functions.
- Returns:
validation_and_conversion_funcs – The attribute
validation_and_conversion_funcs
.- Return type:
dict
- update(new_core_attr_subset_candidate={}, skip_validation_and_conversion=False)[source]
Update a subset of the core attributes.
- Parameters:
new_core_attr_subset_candidate (dict, optional) – A dict object.
skip_validation_and_conversion (bool, optional) –
Let
validation_and_conversion_funcs
andcore_attrs
denote the attributesvalidation_and_conversion_funcs
andcore_attrs
respectively, both of which being dict objects.If
skip_validation_and_conversion
is set toFalse
, then for each keykey
incore_attrs
that is also innew_core_attr_subset_candidate
,core_attrs[key]
is set tovalidation_and_conversion_funcs[key] (new_core_attr_subset_candidate)
.Otherwise, if
skip_validation_and_conversion
is set toTrue
, then for each keykey
incore_attrs
that is also innew_core_attr_subset_candidate
,core_attrs[key]
is set tonew_core_attr_subset_candidate[key]
. This option is desired primarily when the user wants to avoid potentially expensive deep copies and/or conversions of the dict values ofnew_core_attr_subset_candidate
, as it is guaranteed that no copies or conversions are made in this case.
- property validation_and_conversion_funcs
The validation and conversion functions.
The keys of
validation_and_conversion_funcs
are the names of the constructor parameters, excludingskip_validation_and_conversion
if it exists as a construction parameter.Let
core_attrs
denote the attributecore_attrs
, which is also a dict object.For each dict key
key
incore_attrs
,validation_and_conversion_funcs[key](core_attrs)
is expected to not raise an exception.Note that
validation_and_conversion_funcs
should be considered read-only.- Type:
dict