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: bytes) Tuple[SerializableBase, bytes][source]

Implements deserialization into self type from bytes

Parameters:

bytes_str (bytes)

Returns:

  • SerializableBase (object to reconstruct)

  • bytes (leftover bytes to deserialize)

Notes

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

abstractmethod static serialize(obj: SerializableBase) bytes[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: Callable[[Serializable], bytes]) Callable[[Serializable], bytes][source]
classmethod register_decorator() Callable[[type[_SerializableClass]], type[_SerializableClass]][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

classmethod register_from_obj(obj: Serializable, serialize_method: Callable[[Serializable], bytes], deserialize_method: Callable[[bytes], Tuple[Serializable, bytes]] | None = None)[source]

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

classmethod register_from_type(obj_type: type[Serializable], serialize_method: Callable[[Serializable], bytes], deserialize_method: Callable[[bytes], Tuple[Serializable, bytes]])[source]

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

get_apply_deserializer(bytes_str: bytes, only_object: bool = True) None | bytes | str | int | float | complex | list | tuple | dict | ndarray[Any, dtype[_ScalarType_co]] | SerializableBase | Tuple[None | bytes | str | int | float | complex | list | tuple | dict | ndarray[Any, dtype[_ScalarType_co]] | SerializableBase, bytes][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 (default False)) – if False, return the object and the remaining bytes if any if True return only the object

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: None | bytes | str | int | float | complex | list | tuple | dict | ndarray[Any, dtype[_ScalarType_co]] | SerializableBase, append_length=False) bytes[source]
Parameters:
  • obj (object) – should be a serializable object (see get_serializables)

  • append_length (bool) – if True will append the length of the bytes string in the beginning of the returned bytes

Returns:

bytes

Return type:

the encoded object

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: type[Serializable]) Callable[[bytes], Tuple[Serializable, bytes]][source]
get_serializables() List[type][source]
get_serializer(obj_type: type) Callable[[Serializable], bytes][source]
get_type_from_str(obj_type_str: str) type[source]
serializable_registry: dict[type[None | bytes | str | int | float | complex | list | tuple | dict | ndarray[Any, dtype[_ScalarType_co]] | SerializableBase], dict[str, Callable[[Serializable], bytes] | Callable[[bytes], Tuple[Serializable, bytes]]]] = {<class 'NoneType'>: {'deserializer': <function NoneSerializeDesieralize.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>}}