dacapo.store.converter

Module Contents

Classes

TypedConverter

A converter that stores and retrieves type information for selected

Attributes

converter

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 provided cls_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 of cls are visible to the module that calls this method.

Example

If class A is the base of class B, and converter.register_hierarchy(A, lambda typ: eval(typ)) has been called, the dictionary y = converter.unstructure(x) will contain a __type__ field that is 'A' if x = A() and B if x = B().

This __type__ field is then used by x = converter.structure(y, A) to recreate the concrete type of x.

dacapo.store.converter.converter