amisc.serialize
Provides serialization protocols for objects in the package. Serialization in the context of amisc
means converting an object to a built-in Python object (e.g. string, dictionary, float, etc.). The serialized objects
are then easy to convert to binary or text forms for storage or transmission using various protocols (i.e. pickle,
json, yaml, etc.).
Includes:
Serializable
— mixin interface for serializing and deserializing objectsBase64Serializable
— mixin class for serializing objects using base64 encodingStringSerializable
— mixin class for serializing objects using string representationPickleSerializable
— mixin class for serializing objects using pickle filesYamlSerializable
— metaclass for serializing an object using Yaml load/dump from string
Base64Serializable
PickleSerializable
Serializable
Bases: ABC
Mixin interface for serializing and deserializing objects.
deserialize(serialized_data)
abstractmethod
classmethod
Construct a Serializable
object from serialized data.
Passing arguments to deserialize
Subclasses should generally not take arguments for deserialization. The serialized object should contain all the information it needs to reconstruct itself. If you need arguments for deserialization, then serialize them along with the object itself and unpack them during the call to deserialize.
Source code in src/amisc/serialize.py
StringSerializable
Bases: Serializable
Mixin class for serializing objects using string representation.
deserialize(serialized_data, trust=False)
classmethod
Deserialize a string representation of the object.
Security Risk
Only use trust=True
if you trust the source of the serialized data. This provides a more flexible
option for eval
-ing the serialized data from string. By default, this will instead try to parse the
string as a class signature like MyClass(*args, **kwargs)
.
PARAMETER | DESCRIPTION |
---|---|
serialized_data |
the string representation of the object
TYPE:
|
trust |
whether to trust the source of the serialized data (i.e. for
TYPE:
|