Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
2e7f0581
Commit
2e7f0581
authored
2 years ago
by
florian
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Allow entity comparisons with inspection of referenced records
parent
31c5e197
No related branches found
No related tags found
2 merge requests
!79
Release 0.10.0
,
!72
F fix merge entity conflicts
Pipeline
#29679
passed
2 years ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/caosdb/apiutils.py
+41
-5
41 additions, 5 deletions
src/caosdb/apiutils.py
with
41 additions
and
5 deletions
src/caosdb/apiutils.py
+
41
−
5
View file @
2e7f0581
...
...
@@ -283,9 +283,28 @@ def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_
matching
[
0
].
unit
if
(
prop
.
value
!=
matching
[
0
].
value
):
olddiff
[
"
properties
"
][
prop
.
name
][
"
value
"
]
=
prop
.
value
newdiff
[
"
properties
"
][
prop
.
name
][
"
value
"
]
=
\
matching
[
0
].
value
# basic comparison of value objects says they are different
same_value
=
False
if
compare_referenced_records
:
# scalar reference
if
isinstance
(
prop
.
value
,
Entity
)
and
isinstance
(
matching
[
0
].
value
,
Entity
):
# exlicitely not recursive to prevent infinite recursion
same_value
=
empty_diff
(
prop
.
value
,
matching
[
0
].
value
,
compare_referenced_records
=
False
)
# list of references
elif
isinstance
(
prop
.
value
,
list
)
and
isinstance
(
matching
[
0
].
value
,
list
):
# all elements in both lists actually are entity objects
if
all
([
isinstance
(
x
,
Entity
)
for
x
in
prop
.
value
])
and
all
([
isinstance
(
x
,
Entity
)
for
x
in
matching
[
0
].
value
]):
# can't be the same if the lengths are different
if
len
(
prop
.
value
)
==
len
(
matching
[
0
].
value
):
# do a one-by-one comparison; the values are the same, if all diffs are empty
same_value
=
all
(
[
empty_diff
(
x
,
y
,
False
)
for
x
,
y
in
zip
(
prop
.
value
,
matching
[
0
].
value
)])
if
not
same_value
:
olddiff
[
"
properties
"
][
prop
.
name
][
"
value
"
]
=
prop
.
value
newdiff
[
"
properties
"
][
prop
.
name
][
"
value
"
]
=
\
matching
[
0
].
value
if
(
len
(
newdiff
[
"
properties
"
][
prop
.
name
])
==
0
and
len
(
olddiff
[
"
properties
"
][
prop
.
name
])
==
0
):
...
...
@@ -338,7 +357,7 @@ def empty_diff(old_entity: Entity, new_entity: Entity, compare_referenced_record
return
True
def
merge_entities
(
entity_a
:
Entity
,
entity_b
:
Entity
):
def
merge_entities
(
entity_a
:
Entity
,
entity_b
:
Entity
,
merge_references_with_empty_diffs
=
True
):
"""
Merge entity_b into entity_a such that they have the same parents and properties.
...
...
@@ -352,13 +371,30 @@ def merge_entities(entity_a: Entity, entity_b: Entity):
Returns entity_a.
WARNING: This function is currently experimental and insufficiently tested. Use with care.
Parameters
----------
entity_a, entity_b : Entity
The entities to be merged. entity_b will be merged into entity_a in place
merge_references_with_empty_diffs : bool, optional
Whether the merge is performed if entity_a and entity_b both reference
record(s) that may be different Python objects but have empty diffs. If
set to `False` a merge conflict will be raised in this case
instead. Default is True.
Returns
-------
entity_a : Entity
The initial entity_a after the in-place merge
"""
logging
.
warning
(
"
This function is currently experimental and insufficiently tested. Use with care.
"
)
# Compare both entities:
diff_r1
,
diff_r2
=
compare_entities
(
entity_a
,
entity_b
)
diff_r1
,
diff_r2
=
compare_entities
(
entity_a
,
entity_b
,
compare_referenced_records
=
merge_references_with_empty_diffs
)
# Go through the comparison and try to apply changes to entity_a:
for
key
in
diff_r2
[
"
parents
"
]:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment