8.4.2.1. pymodaq_utils.serialize.factory module

class pymodaq_utils.serialize.factory.SerializableBase[source]

Bases: object

Base class for a Serializer.

Methods

deserialize(bytes_str)

Implements deserialization into self type from bytes

name()

str: the object class name

serialize(obj)

Implements self serialization into bytes

type()

object: the type of the object

classmethod name()[source]

str: the object class name

classmethod type()[source]

object: the type of the object

abstractmethod static deserialize(bytes_str)[source]

Implements deserialization into self type from bytes

Parameters:

bytes_str (bytes)

Return type:

Tuple[SerializableBase, bytes]

Returns:

Notes

The actual deserialization should be done using the SerializableFactory and its method :meth:SerializableFactory.get_apply_deserializer

abstractmethod static serialize(obj)[source]

Implements self serialization into bytes

Parameters:

obj (SerializableBase)

Return type:

bytes

Notes

The actual serialization should be done using the SerializableFactory and its method :meth:SerializableFactory.get_apply_serializer

class pymodaq_utils.serialize.factory.SerializableFactory[source]

Bases: object

The factory class for creating executors

Methods

get_apply_deserializer(bytes_str[, only_object])

Infer which object is to be deserialized from the first bytes

get_apply_serializer(obj[, append_length])

register_decorator()

Class decorator method to register exporter class to the internal registry.

register_from_obj(obj, serialize_method[, ...])

Method to register a serializable object class to the internal registry.

register_from_type(obj_type, ...)

Method to register a serializable object class to the internal registry.

add_type_to_serialize

get_deserializer

get_serializables

get_serializer

get_type_from_str

classmethod add_type_to_serialize(serialize_method)[source]
Return type:

Callable[[TypeVar(Serializable, bound= Union[None, bytes, str, int, float, complex, list, tuple, dict, ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]], SerializableBase])], bytes]

classmethod register_decorator()[source]

Class decorator method to register exporter class to the internal registry. Must be used as decorator above the definition of a SerializableBase inherited class.

This class must implement specific class methods in particular: serialize and deserialize

Return type:

Callable[[type[TypeVar(_SerializableClass, bound= SerializableBase)]], type[TypeVar(_SerializableClass, bound= SerializableBase)]]

classmethod register_from_obj(obj, serialize_method, deserialize_method=None)[source]

Method to register a serializable object class to the internal registry.

classmethod register_from_type(obj_type, serialize_method, deserialize_method)[source]

Method to register a serializable object class to the internal registry.

get_apply_deserializer(bytes_str, only_object=True)[source]

Infer which object is to be deserialized from the first bytes

The type has been encoded by the get_apply_serializer method

Parameters:
  • bytes_str (bytes) – The bytes to convert back to an object

  • only_object (bool) – if False, return the object and the remaining bytes if any if True return only the object

Return type:

Union[None, bytes, str, int, float, complex, list, tuple, dict, ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]], SerializableBase, Tuple[Union[None, bytes, str, int, float, complex, list, tuple, dict, ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]], SerializableBase], bytes]]

Returns:

  • object (the reconstructed object)

  • optional bytes (only if only_object parameter is False, will be the leftover bytes)

Notes

Symmetric method of :meth:SerializableFactory.get_apply_serializer

Examples

>>> ser_factory = SerializableFactory()
>>> s = [23, 'a']
>>>> ser_factory.get_apply_deserializer(ser_factory.get_apply_serializer(s) == s
get_apply_serializer(obj, append_length=False)[source]
Parameters:
Returns:

bytes

Return type:

bytes

Notes

Symmetric method of :meth:SerializableFactory.get_apply_deserializer

Examples

>>> ser_factory = SerializableFactory()
>>> s = [23, 'a']
>>>> ser_factory.get_apply_deserializer(ser_factory.get_apply_serializer(s) == s
get_deserializer(obj_type)[source]
Return type:

Callable[[bytes], Tuple[TypeVar(Serializable, bound= Union[None, bytes, str, int, float, complex, list, tuple, dict, ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]], SerializableBase]), bytes]]

get_serializables()[source]
Return type:

List[type]

get_serializer(obj_type)[source]
Return type:

Callable[[TypeVar(Serializable, bound= Union[None, bytes, str, int, float, complex, list, tuple, dict, ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]], SerializableBase])], bytes]

get_type_from_str(obj_type_str)[source]
Return type:

type

serializable_registry: dict[type[None | bytes | str | int | float | complex | list | tuple | dict | ndarray[tuple[Any, ...], dtype[_ScalarT]] | SerializableBase], dict[str, Callable[[Serializable], bytes] | Callable[[bytes], Tuple[Serializable, bytes]]]] = {<class 'NoneType'>: {'deserializer': <function NoneSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'bool'>: {'deserializer': <function ScalarSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'bytes'>: {'deserializer': <function BytesSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'complex'>: {'deserializer': <function ScalarSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'dict'>: {'deserializer': <function DictSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'float'>: {'deserializer': <function ScalarSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'int'>: {'deserializer': <function ScalarSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'list'>: {'deserializer': <function ListSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'numpy.ndarray'>: {'deserializer': <function NdArraySerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq.utils.data.DataActuator'>: {'deserializer': <bound method DataWithAxes.deserialize of <class 'pymodaq.utils.data.DataActuator'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq.utils.data.DataFromPlugins'>: {'deserializer': <bound method DataWithAxes.deserialize of <class 'pymodaq.utils.data.DataFromPlugins'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq.utils.data.DataScan'>: {'deserializer': <bound method DataToExport.deserialize of <class 'pymodaq.utils.data.DataScan'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq.utils.data.DataToActuators'>: {'deserializer': <bound method DataToExport.deserialize of <class 'pymodaq.utils.data.DataToActuators'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_data.data.Axis'>: {'deserializer': <function Axis.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_data.data.DataCalculated'>: {'deserializer': <bound method DataWithAxes.deserialize of <class 'pymodaq_data.data.DataCalculated'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_data.data.DataFromRoi'>: {'deserializer': <bound method DataWithAxes.deserialize of <class 'pymodaq_data.data.DataFromRoi'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_data.data.DataRaw'>: {'deserializer': <bound method DataWithAxes.deserialize of <class 'pymodaq_data.data.DataRaw'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_data.data.DataToExport'>: {'deserializer': <bound method DataToExport.deserialize of <class 'pymodaq_data.data.DataToExport'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_data.data.DataWithAxes'>: {'deserializer': <bound method DataWithAxes.deserialize of <class 'pymodaq_data.data.DataWithAxes'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_data.data.NavAxis'>: {'deserializer': <function Axis.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_gui.parameter.utils.ParameterWithPath'>: {'deserializer': <bound method ParameterWithPath.deserialize of <class 'pymodaq_gui.parameter.utils.ParameterWithPath'>>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'pymodaq_utils.utils.ThreadCommand'>: {'deserializer': <function ThreadCommand.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'str'>: {'deserializer': <function StringSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}, <class 'tuple'>: {'deserializer': <function TupleSerializeDeserialize.deserialize>, 'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>}}