dbzero.compare(obj_1, obj_2, tags=None)
The compare method performs a deep, content-based comparison of two memoized objects to check if they are identical. 👯
By default, it only compares the objects' data fields. However, you can use the optional tags parameter to include their tag assignments in the comparison.
Parameters
obj_1(required): The first memoized object for comparison.obj_2(required): The second memoized object for comparison.tags(optional,list[str]): A list of tag names to include in the comparison. If provided, the method will check if both objects have an identical presence (or absence) of each tag in the list.
Returns
Returns a bool indicating whether the objects are identical based on the comparison criteria.
True: If the objects are of the same type and their data content (and specified tags, if checked) are identical.False: If the objects are of different types, their data differs, or their specified tag assignments differ.
Side Effects
This is a read-only method and has no side effects.
Examples
Basic Content Comparison
This example compares two objects based on their field values, ignoring any differences in their tags.
# Create two objects with identical content
obj_1 = MemoTestClass(100)
obj_2 = MemoTestClass(100)
db0.compare(obj_1, obj_2) # Returns True
# Change the content of one object
obj_2.value = 200
db0.compare(obj_1, obj_2) # Returns FalseComparing with Tags
This example shows how to include tag assignments in the comparison.
obj_A = MemoTestClass(100)
obj_B = MemoTestClass(100)
db0.tags(obj_B).add("featured")
db0.commit()
# Default comparison ignores tags and returns True
# because their content is the same.
assert db0.compare(obj_A, obj_B) is True
# Including the 'featured' tag in the comparison
# returns False because obj_A lacks the tag.
assert db0.compare(obj_A, obj_B, tags=['featured']) is False
# Now add the tag to obj_A as well
db0.tags(obj_A).add("featured")
db0.commit()
# The comparison now returns True
assert db0.compare(obj_A, obj_B, tags=['featured']) is True