medkit.core.dict_conv#

Classes#

DictConvertible

Base protocol to support conversion to and from a data dictionary.

SubclassMapping

Base class for managing subclasses.

Functions#

get_class_name(→ str)

add_class_name_to_data_dict(instance, data_dict)

Add a class name to a data dict returned by a to_dict() method.

get_class_name_from_data_dict(data_dict)

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

Module Contents#

medkit.core.dict_conv.get_class_name(class_: type) str#
medkit.core.dict_conv.add_class_name_to_data_dict(instance: object, data_dict: dict[str, Any])#

Add a class name to a data dict returned by a to_dict() method.

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

medkit.core.dict_conv.get_class_name_from_data_dict(data_dict: dict[str, Any])#

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

Parameters:
data_dict

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

class medkit.core.dict_conv.DictConvertible#

Bases: typing_extensions.Protocol

Base protocol to support conversion to and from a data dictionary.

to_dict() dict[str, Any]#

Convert current instance to a data dictionary.

Returns:
dict of str to Any:

A data dict containing all the information needed to re-instantiate the object

classmethod from_dict(data_dict: dict[str, Any]) typing_extensions.Self#

Instantiate an object from a data dictionary.

Parameters:
data_dict: dict of str to Any

Data dict returned by to_dict()

Returns:
Self:

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

class medkit.core.dict_conv.SubclassMapping#

Base class for managing subclasses.

_subclasses: dict[str, type[typing_extensions.Self]]#
classmethod __init_subclass__()#
classmethod register_subclass(subclass: type[typing_extensions.Self])#
classmethod get_subclass(name: str) type[typing_extensions.Self] | None#
classmethod get_subclass_for_data_dict(data_dict: dict[str, Any]) type[typing_extensions.Self] | None#

Return the subclass corresponding to the class name found in a data dictionary.

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).

Returns:
subclass

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