8.4.2.1. pymodaq_utils.serialize.factory module
- class pymodaq_utils.serialize.factory.SerializableBase[source]
Bases:
objectBase 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
- abstractmethod static deserialize(bytes_str)[source]
Implements deserialization into self type from bytes
- Parameters:
bytes_str (
bytes)- Return type:
- Returns:
SerializableBase (
objecttoreconstruct)bytes (
leftover bytestodeserialize)
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:
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:
objectThe 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])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 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:
- 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:
obj (
Union[None,bytes,str,int,float,complex,list,tuple,dict,ndarray[tuple[Any,...],dtype[TypeVar(_ScalarT, bound=generic)]],SerializableBase]) – 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:
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
- 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>}}