medkit.core.dict_conv#

Classes:

DictConvertible(*args, **kwargs)

Base protocol that must be implemented for all classes supporting conversion to a data dict and re-instantiation from a data dict.

SubclassMapping()

Base class for managing subclasses

Functions:

add_class_name_to_data_dict(instance, data_dict)

Add a class name to a data dict returned by a to_dict() method, so we later know upon which class to call from_dict() when re-instantiating the corresponding object.

get_class_name_from_data_dict(data_dict)

Get the class name written in the data_dict by the to_dict method.

class DictConvertible(*args, **kwargs)[source]#

Base protocol that must be implemented for all classes supporting conversion to a data dict and re-instantiation from a data dict.

Methods:

from_dict(data_dict)

Re-instantiate an object from a datadict obtained via to_dict()

to_dict()

Convert current instance into a data dict that can later be used to rebuild the exact same instance

to_dict()[source]#

Convert current instance into a data dict that can later be used to rebuild the exact same instance

Return type:

dict[str, Any]

Returns:

dict of str to Any – A data dict containing all the information needed to re-instantiate the object

classmethod from_dict(data_dict)[source]#

Re-instantiate an object from a datadict obtained via to_dict()

Parameters:

data_dict (dict of str to Any) – Data dict returned by to_dict()

Return type:

Self

Returns:

Self – An instance of the class to_dict() was called on.

class SubclassMapping[source]#

Base class for managing subclasses

Methods:

get_subclass_for_data_dict(data_dict)

Return the subclass that corresponds to the class name found in a data dict

classmethod get_subclass_for_data_dict(data_dict)[source]#

Return the subclass that corresponds to the class name found in a data dict

Parameters:

data_dict (dict of str to Any) – Data dict returned by the to_dict() method of a subclass (or of the base class itself)

Return type:

Optional[type[Self]]

Returns:

subclass – Subclass that generated data_dict, or None if data_dict correspond to the base class itself.

add_class_name_to_data_dict(instance, data_dict)[source]#

Add a class name to a data dict returned by a to_dict() method, so we later know upon which class to call from_dict() when re-instantiating the corresponding object.

Parameters:
  • instance (object) – The instance of class to which data_dict corresponds

  • data_dict (dict of str to Any) – The data dict on which to add the class name

get_class_name_from_data_dict(data_dict)[source]#

Get the class name written in the data_dict by the to_dict method.

Parameters:

data_dict (dict[str, Any]) – The data dict returned by to_dict of the class to extract

Returns:

class_name – The name of the class which has generated the data_dict