def open(prefix_name: str, open_mode: str = "rw", **kwargs)
Opens a data partition, known as a prefix, and sets it as the current working context.
This function is the primary way to access a specific dataset within the dbzero environment. After a prefix is opened, all subsequent object creations and lookups by default (that don't specify another prefix) will occur within it.
If you try to access an object from a prefix that isn't currently open, dbzero will automatically attempt to open that prefix in read-only ("r") mode. This ensures data can be accessed safely without an explicit dbzero.open() call.
Parameters
-
prefix_namestr
The unique name for the data partition you want to open. -
open_mode{"rw", "r"}, default "rw" : str
The mode for opening the prefix. Defaults to"rw"."rw": Read-write mode. Allows both reading and modifying objects within the prefix."r": Read-only mode. Prevents any changes to the data. This is useful for preventing accidental modifications or for client applications that only need to consume data.
-
**kwargsdict
Additional keyword arguments to configure the prefix's behavior:autocommit(bool): Set toFalseto disable automatic commits for this specific prefix. By default, it uses the globalautocommitsetting configured duringdbzero.init().slab_size(int): Specifies the size (in bytes) of the memory slab to allocate for this prefix's data. Useful for performance tuning.meta_io_step_size(int): Configures the chunk size for metadata I/O operations, which can impact performance for certain workloads.lock_flags(dict): Configure locking behavior when opening the prefix in read-write mode.
Returns
This method does not return any value.
Examples
Basic Usage
Open a prefix in the default read-write mode. This becomes the current active prefix.
# Initialize dbzero first
db0.init("app-data")
# Open a prefix for read-write access
db0.open("user-profiles")
# Now you can work with objects in the "user-profiles" prefix
user = UserProfile(name="Alex")Opening in Read-Only Mode
Open a prefix for safe, read-only access. This is ideal for analytics or recovery operations.
# Open the same prefix, but only for reading
db0.open("user-profiles", "r")
# This will succeed
alex = next(iter(db0.find(UserProfile, "Alex")))
# This will raise an error because the prefix is read-only
alex.last_login = datetime.now()
db0.commit()Customizing Prefix Settings
You can override global configurations on a per-prefix basis. Here, we disable autocommit for a specific prefix that requires manual transaction control.
# Open a prefix and disable autocommit just for it
db0.open("transaction-logs", autocommit=False)
# Changes made here won't be saved until you manually call db0.commit()
log_entry = Log(message="User action")
# No autocommit happens here
time.sleep(1)
# Manually commit the changes
db0.commit()