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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
ddd6d1fe
Commit
ddd6d1fe
authored
9 months ago
by
I. Nüske
Browse files
Options
Downloads
Patches
Plain Diff
MNT: Extended some comments, removed extra spaces, added some ToDos
parent
ed328ec7
No related branches found
No related tags found
2 merge requests
!159
Release 0.16.o
,
!155
Review filter lists and compare_entities
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/linkahead/apiutils.py
+44
-41
44 additions, 41 deletions
src/linkahead/apiutils.py
with
44 additions
and
41 deletions
src/linkahead/apiutils.py
+
44
−
41
View file @
ddd6d1fe
...
...
@@ -183,7 +183,7 @@ def compare_entities(old_entity: Entity,
new_entity
:
Entity
,
compare_referenced_records
:
bool
=
False
)
->
tuple
[
dict
[
str
,
Any
],
dict
[
str
,
Any
]]:
"""
Compare two entites.
"""
Compare two entit
i
es.
The following attributes are compared:
- parents
...
...
@@ -200,7 +200,7 @@ def compare_entities(old_entity: Entity,
the following differs:
- datatype
- importance or
- value
(not implemented yet)
- value
In case of changed information the value listed under the respective key is the
value that is stored in the respective entity.
...
...
@@ -226,31 +226,33 @@ def compare_entities(old_entity: Entity,
newdiff
:
dict
[
str
,
Any
]
=
{
"
properties
"
:
{},
"
parents
"
:
[]}
if
old_entity
is
new_entity
:
return
(
olddiff
,
newdiff
)
return
olddiff
,
newdiff
if
type
(
old_entity
)
is
not
type
(
new_entity
):
raise
ValueError
(
"
Comparison of different Entity types is not supported.
"
)
#
##
compare special attributes
###
# compare special attributes
for
attr
in
SPECIAL_ATTRIBUTES
:
if
attr
==
"
value
"
:
continue
oldattr
=
old_entity
.
__getattribute__
(
attr
)
# we consider
e
"" and None
as
nonexistent
# we consider "" and None
to be
nonexistent
old_entity_attr_na
=
(
oldattr
==
""
or
oldattr
is
None
)
newattr
=
new_entity
.
__getattribute__
(
attr
)
# we consider
e
"" and None
as
nonexistent
# we consider "" and None
to be
nonexistent
new_entity_attr_na
=
(
newattr
==
""
or
newattr
is
None
)
# both
un
set
#
in
both
entities the current attribute is not
set
if
old_entity_attr_na
and
new_entity_attr_na
:
continue
# treat datatype separately: if datatype is an object on one side and string on the other.
# treat datatype separately if one datatype is an object and the other
# a string or int, and may be a name or id
if
attr
==
"
datatype
"
:
if
(
not
old_entity_attr_na
and
not
new_entity_attr_na
)
:
if
not
old_entity_attr_na
and
not
new_entity_attr_na
:
if
isinstance
(
oldattr
,
RecordType
):
if
oldattr
.
name
==
newattr
:
continue
...
...
@@ -262,8 +264,7 @@ def compare_entities(old_entity: Entity,
if
newattr
.
id
==
oldattr
:
continue
# only one set or different values
# add to diff if attribute has different values or is not set for one entity
if
((
old_entity_attr_na
^
new_entity_attr_na
)
or
(
oldattr
!=
newattr
)):
if
not
old_entity_attr_na
:
...
...
@@ -272,27 +273,25 @@ def compare_entities(old_entity: Entity,
if
not
new_entity_attr_na
:
newdiff
[
attr
]
=
newattr
# value
if
(
old_entity
.
value
!=
new_entity
.
value
):
# basic comparison of value objects says they are different
# compare value
if
old_entity
.
value
!=
new_entity
.
value
:
# though the values are not equal, they might be equivalent:
same_value
=
False
if
compare_referenced_records
:
# scalar reference
#
both values are
scalar reference
s:
if
isinstance
(
old_entity
.
value
,
Entity
)
and
isinstance
(
new_entity
.
value
,
Entity
):
#
explicitely not recursiv
e to prevent infinite recursion
#
compare_referenced_records=Fals
e to prevent infinite recursion
same_value
=
empty_diff
(
old_entity
.
value
,
new_entity
.
value
,
compare_referenced_records
=
False
)
# list of references
#
both values are a
list of references
:
elif
isinstance
(
old_entity
.
value
,
list
)
and
isinstance
(
new_entity
.
value
,
list
):
# all elements in both lists
actually
are entity objects
# TODO: check
,
whether mixed cases
can
be allowed or
should
lead to an error
#
if
all elements in both lists are entity objects
, check each pair for equality
# TODO: check whether mixed cases
should
be allowed or lead to an error
if
(
all
([
isinstance
(
x
,
Entity
)
for
x
in
old_entity
.
value
])
and
all
([
isinstance
(
x
,
Entity
)
for
x
in
new_entity
.
value
])):
# can't be the same if the lengths are different
#
lists
can't be the same if the lengths are different
if
len
(
old_entity
.
value
)
==
len
(
new_entity
.
value
):
# do a one-by-one comparison:
# the values are the same if all diffs are empty
# the lists are the same if the diffs of each entry pair are empty
same_value
=
all
(
[
empty_diff
(
x
,
y
,
False
)
for
x
,
y
in
zip
(
old_entity
.
value
,
new_entity
.
value
)])
...
...
@@ -301,11 +300,11 @@ def compare_entities(old_entity: Entity,
olddiff
[
"
value
"
]
=
old_entity
.
value
newdiff
[
"
value
"
]
=
new_entity
.
value
# properties
#
compare
properties
for
prop
in
old_entity
.
properties
:
matching
=
[
p
for
p
in
new_entity
.
properties
if
p
.
name
.
lower
()
==
prop
.
name
.
lower
()]
if
len
(
matching
)
==
0
:
# old has prop
and new
does not
# old
_entity
has prop
, new_entity
does not
olddiff
[
"
properties
"
][
prop
.
name
]
=
{}
elif
len
(
matching
)
==
1
:
olddiff
[
"
properties
"
][
prop
.
name
]
=
{}
...
...
@@ -313,9 +312,12 @@ def compare_entities(old_entity: Entity,
oldpropdiff
=
olddiff
[
"
properties
"
][
prop
.
name
]
newpropdiff
=
newdiff
[
"
properties
"
][
prop
.
name
]
# use compare function detect difference of properties
# recursive call to determine the differences between properties
# ToDo: This can lead to infinite recursion if two properties have
# each other as subproperties
od
,
nd
=
compare_entities
(
prop
,
matching
[
0
],
compare_referenced_records
=
compare_referenced_records
)
# we do not care about parents and properties here # TODO do we?
# as we do not care about parents and properties here, discard their entries
# TODO do we?
od
.
pop
(
"
parents
"
)
od
.
pop
(
"
properties
"
)
nd
.
pop
(
"
parents
"
)
...
...
@@ -324,27 +326,28 @@ def compare_entities(old_entity: Entity,
oldpropdiff
.
update
(
od
)
newpropdiff
.
update
(
nd
)
# importance is associated with the record. So do it extra
if
(
old_entity
.
get_importance
(
prop
.
name
)
!=
new_entity
.
get_importance
(
prop
.
name
)):
# As the importance of a property is an attribute of the record and not
# the property, it is not contained in the diff returned by compare_entities
if
old_entity
.
get_importance
(
prop
.
name
)
!=
new_entity
.
get_importance
(
prop
.
name
):
oldpropdiff
[
"
importance
"
]
=
old_entity
.
get_importance
(
prop
.
name
)
newpropdiff
[
"
importance
"
]
=
new_entity
.
get_importance
(
prop
.
name
)
# in case there
was actually
no difference, we remove the dict keys again
if
(
len
(
newpropdiff
)
==
0
and
len
(
oldpropdiff
)
==
0
)
:
# in case there
is
no difference, we remove the dict keys again
if
len
(
newpropdiff
)
==
0
and
len
(
oldpropdiff
)
==
0
:
newdiff
[
"
properties
"
].
pop
(
prop
.
name
)
olddiff
[
"
properties
"
].
pop
(
prop
.
name
)
else
:
raise
NotImplementedError
(
"
Comparison not implemented for multi-properties.
"
)
#
add the properties that are only present in new
#
we have not yet compared properties that do not exist in old_entity
for
prop
in
new_entity
.
properties
:
if
len
([
0
for
p
in
old_entity
.
properties
if
p
.
name
.
lower
()
==
prop
.
name
.
lower
()])
==
0
:
newdiff
[
"
properties
"
][
prop
.
name
]
=
{}
# parents
# TODO we only use names for parents and property matching
#
compare
parents
# ToDo: Compare using filter function, compare inheritance level for RTs
# TODO we
currently
only use names for parents and property matching
for
parent
in
old_entity
.
parents
:
if
len
([
0
for
p
in
new_entity
.
parents
if
p
.
name
.
lower
()
==
parent
.
name
.
lower
()])
==
0
:
olddiff
[
"
parents
"
].
append
(
parent
.
name
)
...
...
@@ -353,7 +356,7 @@ def compare_entities(old_entity: Entity,
if
len
([
0
for
p
in
old_entity
.
parents
if
p
.
name
.
lower
()
==
parent
.
name
.
lower
()])
==
0
:
newdiff
[
"
parents
"
].
append
(
parent
.
name
)
return
(
olddiff
,
newdiff
)
return
olddiff
,
newdiff
def
empty_diff
(
old_entity
:
Entity
,
new_entity
:
Entity
,
...
...
This diff is collapsed.
Click to expand it.
I. Nüske
@i.nueske
mentioned in commit
8c798b17
·
9 months ago
mentioned in commit
8c798b17
mentioned in commit 8c798b177dfe00c4f9f01a6850798d509b899982
Toggle commit list
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