Sometimes, you want to serialize and deserialize a Python dictionary easily.
This is the perfect job for the serialization API.
In the following example, we rely on the Bunch
class as a dictionary container for a few fields.
Serializing a bunch of things
| import numpy as np
from mltraq import Bunch
from mltraq.storage.serialization import deserialize, serialize
# Build a dictionary whose elements can be accessed as object attributes.
bunch = Bunch(a=np.array([1, 2, 3]), b="something")
bunch.c = 456
# Print its serialized binary blob (`bytes`)
print("Serialized", serialize(bunch)[:30])
# Demonstrate its deserialization
print("Deserialized", deserialize(serialize(bunch)))
|
OutputSerialized b'\x80\x05\x95\xfe\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\tDATAPAK-0\x94\x8c\x0eml'
Deserialized {'a': array([1, 2, 3]), 'b': 'something', 'c': 456}