Skip to content
Snippets Groups Projects

Improving the compare_entities functions

Merged Alexander Schlemmer requested to merge f-check-merge-entities into dev
Files
2
+ 22
3
@@ -195,7 +195,8 @@ def getCommitIn(folder):
return t.readline().strip()
def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_records: bool = False):
def compare_entities(old_entity: Entity, new_entity: Entity,
compare_referenced_records: bool = False):
"""Compare two entites.
Return a tuple of dictionaries, the first index belongs to additional information for old
@@ -207,7 +208,7 @@ def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_
- Each property lists either an additional property or a property with a changed:
- datatype
- importance or
- value (not implemented yet)
- value
In case of changed information the value listed under the respective key shows the
value that is stored in the respective entity.
@@ -217,6 +218,9 @@ def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_
`compare_referenced_records = False` to prevent infinite recursion in case
of circular references).
NOTE: This function does not work for abstract properties! I.e. it is not possible
to directly compare two entities that are of class caosdb.Property.
Parameters
----------
old_entity, new_entity : Entity
@@ -229,6 +233,12 @@ def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_
identical records are stored in different objects. Default is False.
"""
for entity in (old_entity, new_entity):
if isinstance(entity, Property):
raise NotImplementedError("The function compare_entities does not work for "
"comparing abstract properties.")
olddiff: Dict[str, Any] = {"properties": {}, "parents": []}
newdiff: Dict[str, Any] = {"properties": {}, "parents": []}
@@ -256,7 +266,7 @@ def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_
if not old_entity_attr_exists and not new_entity_attr_exists:
continue
if ((old_entity_attr_exists ^ new_entity_attr_exists)
if ((old_entity_attr_exists != new_entity_attr_exists)
or (oldattr != newattr)):
if old_entity_attr_exists:
@@ -268,8 +278,16 @@ def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_
# properties
for prop in old_entity.properties:
# Find the corresponding property in new_entity:
matching = [p for p in new_entity.properties if p.name == prop.name]
# This is needed for checking for multi properties in old_entity:
# TODO: is there a better way?
matching_old = [p for p in old_entity.properties if p.name == prop.name]
if len(matching_old) != 1:
raise NotImplementedError(
"Comparison not implemented for multi-properties.")
if len(matching) == 0:
olddiff["properties"][prop.name] = {}
elif len(matching) == 1:
@@ -300,6 +318,7 @@ def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_
# scalar reference
if isinstance(prop.value, Entity) and isinstance(matching[0].value, Entity):
# explicitely not recursive to prevent infinite recursion
# TODO: why not use a recursion detection with a cache?
same_value = empty_diff(
prop.value, matching[0].value, compare_referenced_records=False)
# list of references
Loading