API Reference
get_current_prefix

def get_current_prefix() -> PrefixMetaData:

Retrieves the currently active database prefix.

In dbzero, a "prefix" acts as a distinct data partition. Most operations, such as creating new objects, are implicitly performed on the current prefix unless a different one is explicitely specified. The current prefix is managed as a stack; dbzero.open() pushes a new prefix onto the stack, making it the current one, and dbzero.close() pops it, restoring the previous one.

Returns

A PrefixMetaData object that represents the current database context. It has a .name attribute holding the string identifier of the prefix and an .uuid.


Examples

Getting the current prefix name

You can easily access the name of the currently active prefix.

# Assuming a connection is open with a default prefix like 'main'
current_px = db0.get_current_prefix()
print(current_px.name)
# Expected output: 'main'

How dbzero.open() and dbzero.close() affect the current prefix

The dbzero.open() function sets a new current prefix, and dbzero.close() reverts to the previous one.

# Get the initial prefix
initial_prefix = db0.get_current_prefix()
print(f"Initial prefix: {initial_prefix.name}")
 
# Open a new prefix, which becomes the current one
db0.open("secondary-prefix")
print(f"New current prefix: {db0.get_current_prefix().name}")
 
# Close the new prefix
db0.close("secondary-prefix")
 
# The current prefix is restored to the initial one
print(f"Restored prefix: {db0.get_current_prefix().name}")
 
# Expected output:
# Initial prefix: main
# New current prefix: secondary-prefix
# Restored prefix: main

Default object creation

Objects created without a specific scope are automatically assigned to the current prefix.

# The current prefix is 'main'
obj_1 = MemoTestClass(100)
assert db0.get_prefix_of(obj_1) == db0.get_current_prefix()
 
# Switch to a different prefix
db0.open("other-prefix")
obj_2 = MemoTestClass(200)
 
# obj_2 is on the new current prefix, different from obj_1
assert db0.get_prefix_of(obj_2) == db0.get_current_prefix()
assert db0.get_prefix_of(obj_1) != db0.get_prefix_of(obj_2)