API Reference
atomic_assign

dbzero.atomic_assign()

def atomic_assign(*objects, **attributes)

The atomic_assign method updates one or more attributes on one or more dbzero-managed objects within a single, atomic transaction. This guarantees that all specified assignments are applied successfully together. If any single assignment fails, the entire operation is rolled back, and none of the objects' attributes are changed, ensuring your data remains in a consistent state.

This is particularly useful when you need to modify several related attributes at once and want to avoid leaving your object in a partially updated, invalid state.


Parameters

  • *objects: A variable number of dbzero-managed objects that you want to modify.
  • **attributes: Keyword arguments where each key is the name of an attribute to update and the corresponding value is the new value to assign.

Returns

This method does not return any value.


Examples

Updating Multiple Attributes on a Single Object

Here, we change value_1, value_2, and value_3 of a single object. The changes are applied as one indivisible operation.

# Create an object with initial values
obj = MyMemoClass(value_1=1, value_2=2, value_3=3)
 
# Atomically assign new values to its attributes
db0.atomic_assign(obj, value_1=10, value_2=20, value_3=30)
 
# The object is now updated
assert obj.value_1 == 10
assert obj.value_2 == 20
assert obj.value_3 == 30

Applying the Same Update to Multiple Objects

You can also apply the same set of changes to several objects at once. Both obj_1 and obj_2 will be updated atomically.

# Create two objects
obj_1 = MyMemoClass(value_1=1, value_2=2, value_3=3)
obj_2 = MyMemoClass(value_1=1, value_2=2, value_3=3)
 
# Atomically assign new values to both objects
db0.atomic_assign(obj_1, obj_2, value_1=15, value_2=25)
 
# Verify the changes on both objects
assert obj_1.value_1 == 15
assert obj_1.value_2 == 25
assert obj_2.value_1 == 15
assert obj_2.value_2 == 25
ℹ️

The key benefit of atomic_assign over setting attributes one by one (e.g., obj.value_1 = 10; obj.value_2 = 20) is the guarantee of atomicity. It ensures that your objects are never left in an intermediate, partially modified state.