7.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
- abstract 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
- abstract static serialize(obj: SerializableBase) bytes[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])- param obj:
should be a serializable object (see get_serializables)
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]
- get_apply_deserializer(bytes_str: bytes, only_object: bool = True) bytes | str | int | float | complex | list | ndarray[Any, dtype[_ScalarType_co]] | SerializableBase | Tuple[bytes | str | int | float | complex | list | 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:
- 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: Any, append_length=False) bytes[source]
- Parameters:
- 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]
- 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 an H5Exporter class. H5Exporter must implement specific class attributes and methods, see definition: h5node_exporter.H5Exporter See h5node_exporter.H5txtExporter and h5node_exporter.H5txtExporter for usage examples. :returns: the exporter class
- 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.
- serializable_registry: dict[type[typing.Union[bytes, str, int, float, complex, list, numpy.ndarray[typing.Any, numpy.dtype[+_ScalarType_co]], pymodaq_utils.serialize.factory.SerializableBase]], dict[str, typing.Union[typing.Callable[[~Serializable], bytes], typing.Callable[[bytes], typing.Tuple[~Serializable, bytes]]]]] = {<class 'bytes'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function BytesSerializeDeserialize.deserialize>}, <class 'str'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function StringSerializeDeserialize.deserialize>}, <class 'int'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function ScalarSerializeDeserialize.deserialize>}, <class 'float'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function ScalarSerializeDeserialize.deserialize>}, <class 'complex'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function ScalarSerializeDeserialize.deserialize>}, <class 'bool'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function ScalarSerializeDeserialize.deserialize>}, <class 'numpy.ndarray'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function NdArraySerializeDeserialize.deserialize>}, <class 'list'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <function ListSerializeDeserialize.deserialize>}, <class 'pymodaq_gui.parameter.utils.ParameterWithPath'>: {'serializer': <function SerializableFactory.add_type_to_serialize.<locals>.wrap>, 'deserializer': <bound method ParameterWithPath.deserialize of <class 'pymodaq_gui.parameter.utils.ParameterWithPath'>>}}