def get_prefix_of(obj: Any) -> PrefixMetaData
Returns the prefix where a given dbzero-managed object resides.
Every object managed by dbzero belongs to a specific prefix, which acts as its scope. This method allows to identify that prefix, which is useful for persistence, querying, and understanding where your data lives.
Parameters
objAny
The dbzero item whose prefix you want to find. This can be an object instance, a class decorated with@dbzero.memoor an enum.
Returns
A PrefixMetaData object representing the prefix where the item is stored or defined. This object has a .name attribute holding the string identifier of the prefix and an .uuid.
Examples
Getting the prefix of an object instance
# Create an object on the current prefix
db0.open("main")
obj_1 = MemoTestClass(100)
print(f"obj_1 lives on prefix: {db0.get_prefix_of(obj_1).name}")
# Open a new prefix, making it the new current one
db0.open("secondary-db")
obj_2 = MemoTestClass(200)
print(f"obj_2 lives on prefix: {db0.get_prefix_of(obj_2).name}")
# Expected output:
# obj_1 lives on prefix: main
# obj_2 lives on prefix: secondary-dbGetting the prefix of a class type
If a class is defined with a static prefix scope, you can get its prefix directly from the class type itself, without needing an instance.
@db0.memo(prefix="scoped-class-prefix")
class ScopedDataClass:
def __init__(self, value):
self.value = value
# Get the prefix directly from the type
class_prefix = db0.get_prefix_of(ScopedDataClass)
print(f"ScopedDataClass belongs to: {class_prefix.name}")
# An instance of the class will belong to the same prefix
instance = ScopedDataClass(42)
instance_prefix = db0.get_prefix_of(instance)
print(f"An instance belongs to: {instance_prefix.name}")
# Expected output:
# ScopedDataClass belongs to: scoped-class-prefix
# An instance belongs to: scoped-class-prefix