API Reference
bytearray

dbzero.bytearray()

def bytearray(source: bytes | Iterable[int] = b'')

Creates a mutable sequence of bytes that is managed and persisted by dbzero. This object is designed to be a seamless replacement for Python's built-in bytearray, offering an almost identical API for byte manipulation.

Use dbzero.bytearray when you need to work with mutable binary data that should be part of dbzero's state management and persistence layer. Any modifications to the object are automatically tracked.


Parameters

  • source (optional): An iterable of integers (from 0 to 255) or a bytes-like object used to initialize the byte array. If not provided, an empty byte array is created.

Returns

A new dbzero.bytearray object. This object can be modified in place and assigned as an attribute to other dbzero-managed objects.


Examples

Creating and Modifying a dbzero.bytearray

You can create and manipulate a dbzero.bytearray just like a standard one.

# Create a new db0.bytearray
raw_data = db0.bytearray(b'log_level:info')
 
# Modify it in-place
raw_data.extend(b', user:admin')
raw_data[11:15] = b'warn'
 
# The final data is b'log_level:warn, user:admin'
print(raw_data.decode())

Storing as a Persistent Attribute

The main benefit is that it can be stored and persisted as part of another dbzero object.

# Create a db0.bytearray containing some binary data
config_payload = db0.bytearray(b'\xDE\xAD\xBE\xEF')
 
# Assign it as an attribute to another dbzero-managed object
app_settings = AppSettings()
app_settings.payload = config_payload
 
# The payload is now persisted along with app_settings
💡

Familiar API

A dbzero.bytearray supports the rich API of Python's built-in bytearray, including methods like .decode(), .split(), .strip(), .upper(), .find(), .replace(), and many more. You can work with it just as you would with the standard library type.