API Reference
select_modified

select_modified(query, pre_snapshot, last_snapshot, compare_with=None)

Finds objects within a query that were modified between two snapshots. This method is perfect for analyzing changes or "diffs" over time, as it specifically ignores newly created objects and focuses only on modifications to objects that existed in the pre_snapshot.


Parameters

  • query: A dbzero.find() query specifying the set of objects you want to check for modifications.
  • pre_snapshot: A dbzero.snapshot() object representing the starting point ("before" state) for the comparison.
  • last_snapshot: A dbzero.snapshot() object representing the ending point ("after" state).
  • compare_with (optional): A callable that takes two arguments: the object from pre_snapshot and the object from last_snapshot. It should return True if the objects are considered unchanged. This lets you define custom logic to ignore irrelevant modifications (e.g., changes to a last_updated timestamp).

Returns

An iterable of tuples, where each tuple contains the "before" and "after" versions of a modified object: (old_version, new_version).

  • old_version is the object's state from pre_snapshot.
  • new_version is the object's state from last_snapshot.

Objects that were merely "touched" using dbzero.touch() without any state change are also considered modified and will be included in the results unless filtered out by a compare_with function.


Examples

Basic Usage: Find all modified districts

This example shows how to get a list of districts whose names have changed between two commits.

# Initial state
district_a = District(name="Oldtown")
db0.commit()
snap_1 = db0.snapshot()
 
# Make a change
district_a.name = "Newtown"
db0.commit()
snap_2 = db0.snapshot()
 
# Find modified objects
modified_districts = db0.select_modified(
    db0.find(District), snap_1, snap_2
)
 
for old_ver, new_ver in modified_districts:
    print(f"District name changed from '{old_ver.name}' to '{new_ver.name}'")
    # Expected Output: District name changed from 'Oldtown' to 'Newtown'
 
    assert old_ver.name == "Oldtown"
    assert new_ver.name == "Newtown"
Advanced Usage: Custom comparison logic

Here, we'll ignore modifications if only the last_updated timestamp has changed.

class Product:
    def __init__(self, name, price, last_updated):
        self.name = name
        self.price = price
        self.last_updated = last_updated
 
# Initial state
product = Product(name="Laptop", price=1200, last_updated=1672531200)
db0.commit()
snap_1 = db0.snapshot()
 
# Modify only the timestamp
product.last_updated = 1672617600
db0.commit()
snap_2 = db0.snapshot()
 
# Custom comparison function
def a_meaningful_change(obj1, obj2):
    # Return True if only the timestamp is different (i.e., they are "equal" for our purposes)
    return obj1.name == obj2.name and obj1.price == obj2.price
 
# This will find no "meaningful" modifications
results = db0.select_modified(
    db0.find(Product), snap_1, snap_2, compare_with=a_meaningful_change
)
 
assert len(results) == 0