def load(obj: Any, /, *, exclude: Optional[Union[List[str], Tuple[str, ...]]], **kwargs: Any) -> Any
Load a dbzero instance recursively into memory as its equivalent native Python representation.
Useful for exporting application state for APIs or functions expecting standard Python types, like JSON serialization. Intelligently handles both standard Python and dbzero types. The conversion is performed recursively on all elements within collections and attributes.
Parameters
-
objAny
The object to convert. Can be native Python type, dbzero object, or @dbzero.memo class instance. -
excludelist of str or tuple of str, optional
Optional list of attribute names to exclude when loading @dbzero.memo class instances.💡Note: The
excludeparameter only works for classes with default serialization behavior. It will raise anAttributeErrorif the class defines a custom__load__method. The exclusion is not recursive and only applies to the top-level object being loaded. -
**kwargsdict
Additional keyword arguments passed to custom__load__methods.
Returns
Converted object with the following behavior:
- Native types: Returned as-is
- dbzero collections: Converted to built-in counterparts (list, tuple, set, dict)
- @dbzero.enum values: Converted to string representation
- @dbzero.memo instances: Converted to dictionaries (or using custom
__load__method)
Cyclic Graphs: Attempting to load an object that contains a reference to itself (a cyclic graph) will cause a RecursionError. Make sure your data structures doesn't contain cycles if you want to use this functon.
Examples
Basic Conversion of a dbzero List
dbzero.load converts dbzero types within a collection to their Python native equivalents.
# Setup a dbzero enum
Colors = db0.enum("Colors", ["RED", "GREEN", "BLUE"])
# Create a list with mixed types
my_list = db0.list([1, "string", Colors.GREEN])
# Load the list to get native Python types
native_list = db0.load(my_list)
print(native_list)
# Expected Output: [1, 'string', 'GREEN']Loading a memo instance
Instances of @dbzero.memo classes are converted to dictionaries.
@db0.memo
class User:
def __init__(self, name, age):
self.name = name
self.age = age
user_instance = User("Alex", 30)
# Load the instance to get a dictionary
user_dict = db0.load(user_instance)
print(user_dict)
# Expected Output: {'name': 'Alex', 'age': 30}Using the exclude Parameter
You can omit specific attributes from the output dictionary.
@db0.memo
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
book_instance = Book("The Hobbit", "J.R.R. Tolkien", "978-0-345-33968-3")
# Load the instance, excluding the 'isbn' field
book_dict = db0.load(book_instance, exclude=["isbn"])
print(book_dict)
# Expected Output: {'title': 'The Hobbit', 'author': 'J.R.R. Tolkien'}Custom loading with __load__ and **kwargs
Define a __load__ method in your class to control the serialization logic and accept extra parameters.
@db0.memo
class Report:
def __init__(self, data, author):
self.data = data
self.author = author
def __load__(self, include_author=True):
# Custom logic to structure the output
if include_author:
return {"report_data": self.data, "created_by": self.author}
return {"report_data": self.data}
report_instance = Report({"sales": 100}, "Admin")
# Load with the custom parameter
full_report = db0.load(report_instance, include_author=True)
print(full_report)
# Expected Output: {'report_data': {'sales': 100}, 'created_by': 'Admin'}
# Load without the author
simple_report = db0.load(report_instance, include_author=False)
print(simple_report)
# Expected Output: {'report_data': {'sales': 100}}