dacapo.store.converter
Module Contents
Classes
A converter that stores and retrieves type information for selected |
Attributes
- 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.
- 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.
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.
- dacapo.store.converter.converter