dbzero.serialize(obj)
The dbzero.serialize() method converts a dbzero query iterable or enum value into a platform-independent bytes representation.
This is primarily used to "save" a query's definition. You can then store these bytes and use dbzero.deserialize() or snapshot.deserialize() to re-execute the exact same query at a later time or against a different data state (a snapshot). This is the foundation for state-comparison operations like finding new, deleted, or modified objects between two points in time.
Parameters
| Name | Type | Description |
|---|---|---|
obj | Query Iterable or Enum Value | The dbzero object to serialize. This can be the result of a query (e.g., dbzero.find(...)) or a dbzero enum value (e.g., Colors.RED). |
Returns
Returns a bytes object containing the serialized representation of the input object. This byte string can be stored, transmitted, or used to reconstruct the object later.
Examples
You can serialize any query, including complex ones involving tags, negation, ranges, and sorting. The result is a bytes object that captures the query's logic.
# Create and tag some objects
for i in range(10):
obj = MemoTestClass(i)
db0.tags(obj).add("group_a")
# Define a query
query = db0.find("group_a")
# Serialize the query into bytes
serialized_query_bytes = db0.serialize(query)
print(type(serialized_query_bytes))
# Expected output: <class 'bytes'>
# You can now deserialize it to re-run the query
reconstituted_query = db0.deserialize(serialized_query_bytes)
assert len(list(reconstituted_query)) == 10