cait.serialize
Serialization is the process of saving a Python object in a way such that it can be uniquely reconstructed again at a later point. This is relevant for example when you want to save the EnergyCalibration object to a file or store an EventIterator in a DataHandler as a reference. The reverse is called de-serialization and it happens, e.g., when you do dh.get_event_iterator(): The DataHandler stores a string that uniquely defines the EventIterator and when you request it, the Python object is constructed again through the process of de-serialization.
The process of serializing is called dump, and load is the reverse. Serializing into a string specifically is done using dumps, and the reverse is loads.
- class cait.serialize.SerializingMixin(*args, **kwargs)[source]
Bases:
objectMixin to give serialization properties to an object.
The initializer has to be called from all child classes with all their (keyword) arguments. The mixin then saves those and if ‘to_dict’ is called on the child, a dictionary with the class name and the (keyword) arguments is returned. If more information is required to reconstruct a class, the ‘to_dict’ method may be overridden (see e.g. DataHandler). If reconstructing a class from a dictionary requires more than calling the initializer with the (keyword) arguments, the child class has to provide a ‘from_dict’ class method (see e.g. DataHandler).
Example:
import cait.versatile as vai class SomeClass(vai.serializing.SerializingMixin): def __init__(self, kwarg1=None, kwarg2=None): super().__init__(kwarg1=kwarg1, kwarg2=kwarg2) c = SomeClass() c.to_dict()
- cait.serialize.dump(obj: SerializingMixin)[source]
Return the dictionary representation of a serializable cait object (e.g. iterator or data source).
- cait.serialize.dumps(obj: SerializingMixin)[source]
Return the string representation of a serializable cait object (e.g. iterator or data source).
- Parameters:
obj (SerializingMixin) – The object to be serialized.
Example:
import cait.versatile as vai md = vai.MockData() it = md.get_event_iterator() md_str = dumps(md) it_str = dumps(it) recoverd_md = loads(md_str) recoverd_it = loads(it_str)
- cait.serialize.load(d: dict)[source]
Converts a (valid) dictionary to the cait object that it represents.
- cait.serialize.loads(s: str)[source]
Returns an object constructed from its string representation (e.g. iterator or data source).
- Parameters:
s (str) – The string representation of object to be deserialized.
Example:
import cait.versatile as vai md = vai.MockData() it = md.get_event_iterator() md_str = dumps(md) it_str = dumps(it) recoverd_md = loads(md_str) recoverd_it = loads(it_str)