dacapo.store.converter
Attributes
Classes
A converter that stores and retrieves type information for selected |
Module Contents
- class dacapo.store.converter.TypedConverter(dict_factory: Callable[[], Any] = dict, unstruct_strat: UnstructureStrategy = UnstructureStrategy.AS_DICT, omit_if_default: bool = False, forbid_extra_keys: bool = False, type_overrides: cattrs._compat.Mapping[type, cattrs.gen.AttributeOverride] = {}, unstruct_collection_overrides: cattrs._compat.Mapping[type, Callable] = {}, prefer_attrib_converters: bool = False, detailed_validation: bool = True, unstructure_fallback_factory: cattrs.dispatch.HookFactory[cattrs.dispatch.UnstructureHook] = lambda _: ..., structure_fallback_factory: cattrs.dispatch.HookFactory[cattrs.dispatch.StructureHook] = lambda _: ...)
A converter that stores and retrieves type information for selected class hierarchies. Used to reconstruct a concrete class from unstructured data.
- hooks
A dictionary mapping classes to lists of hooks that should be applied to them.
- Type:
Dict[Type, List[Hook]]
- register_hierarchy()
Register a class hierarchy for typed structure/unstructure conversion.
- __typed_unstructure()
Unstructure an object, adding a ‘__type__’ field with the class name.
- __typed_structure()
Structure an object, using the ‘__type__’ field to determine the class.
Note
This class is a subclass of cattr.Converter, and extends it with the ability to store and retrieve type information for selected class hierarchies. This is useful for reconstructing a concrete class from unstructured data.
- register_hierarchy(cls, cls_fn)
Register a class hierarchy for typed structure/unstructure conversion.
For each class in the hierarchy under (including)
cls, this will store an additional__type__attribute (a string) in the object dictionary. This__type__string will be the concrete class of the object, and will be used to structure the dictionary back into an object of the correct class.For this to work, this function needs to know how to convert a
__type__string back into a class, for which it used the providedcls_fn.- Parameters:
cls (class) – The top-level class of the hierarchy to register.
cls_fn (function) – A function mapping type strings to classes. This can be as simple as
lambda typ: eval(typ), if all subclasses ofclsare visible to the module that calls this method.
- Raises:
TypeError – If
clsis not a class.
Example
If class
Ais the base of classB, andconverter.register_hierarchy(A, lambda typ: eval(typ))has been called, the dictionaryy = converter.unstructure(x)will contain a__type__field that is'A'ifx = A()andBifx = B().This
__type__field is then used byx = converter.structure(y, A)to recreate the concrete type ofx.Note
This method is used to register a class hierarchy for typed structure/unstructure conversion. For each class in the hierarchy under (including)
cls, this will store an additional__type__attribute (a string) in the object dictionary. This__type__string will be the concrete class of the object, and will be used to structure the dictionary back into an object of the correct class.For this to work, this function needs to know how to convert a
__type__string back into a class, for which it used the providedcls_fn.
- dacapo.store.converter.converter