def exists(identifier: Union[str, type], expected_type: Optional[type], prefix: Optional[str]) -> bool
Check if an identifier points to a valid dbzero object or an existing singleton instance.
Can check by UUID or by singleton type. Allows to verify if an object is still available before trying to retrieve it, especially since objects can be automatically garbage-collected after their last reference or tag is removed.
Parameters
-
identifierstr or type
The identifier for object to check for.- str: Check for object with its unique identifier (UUID).
- type: Check for instance of this singleton type.
-
expected_typetype, optional
Optional expected type when checking by UUID. Verifies the found object is an instance of this type. -
prefixstr, optional
Optional prefix name to search within. Defaults to currently active prefix. Only used when checking singleton types.
Returns
True if the object exists (and matches type if specified), False otherwise.
Examples
Check by UUID
# Create an object
obj = MyDataObject(value="hello")
uuid = db0.uuid(obj)
# Check if it exists
assert db0.exists(uuid) # TrueCheck with type validation
# Validate its type
assert db0.exists(uuid, MyDataObject) # True
assert not db0.exists(uuid, SomeOtherClass) # FalseCheck singleton
# Create an instance in the default prefix
_ = MySingleton(data="...")
assert db0.exists(MySingleton) # TrueCheck singleton in specific prefix
# Create another instance in a different prefix
db0.open("other-scope", "rw")
_ = MySingleton(data="new data")
# Check for existence in specific prefixes
assert db0.exists(MySingleton, prefix="default") # True
assert db0.exists(MySingleton, prefix="other-scope") # True
assert not db0.exists(MySingleton, prefix="non-existent-scope") # False