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
2b75bb00
Commit
2b75bb00
authored
3 years ago
by
Henrik tom Wörden
Committed by
Florian Spreckelsen
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Add resolve_value to Property
parent
df29c846
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!33
MAINT: change arguments of create_user
,
!19
ENH: add resolve_value to Property
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+3
-0
3 additions, 0 deletions
CHANGELOG.md
src/caosdb/apiutils.py
+41
-3
41 additions, 3 deletions
src/caosdb/apiutils.py
unittests/test_apiutils.py
+41
-1
41 additions, 1 deletion
unittests/test_apiutils.py
with
85 additions
and
4 deletions
CHANGELOG.md
+
3
−
0
View file @
2b75bb00
...
@@ -9,12 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
...
@@ -9,12 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ###
### Added ###
*
extended apiutils with
`resolve_reference(Property)`
*
is_reference function for Properties
*
is_reference function for Properties
### Changed ###
### Changed ###
### Deprecated ###
### Deprecated ###
*
`id_query(ids)`
in apiutils
### Removed ###
### Removed ###
### Fixed ###
### Fixed ###
...
...
This diff is collapsed.
Click to expand it.
src/caosdb/apiutils.py
+
41
−
3
View file @
2b75bb00
...
@@ -31,6 +31,7 @@ Some simplified functions for generation of records etc.
...
@@ -31,6 +31,7 @@ Some simplified functions for generation of records etc.
import
sys
import
sys
import
tempfile
import
tempfile
from
collections.abc
import
Iterable
from
collections.abc
import
Iterable
import
warnings
from
subprocess
import
call
from
subprocess
import
call
from
caosdb.common.datatype
import
(
BOOLEAN
,
DATETIME
,
DOUBLE
,
FILE
,
INTEGER
,
from
caosdb.common.datatype
import
(
BOOLEAN
,
DATETIME
,
DOUBLE
,
FILE
,
INTEGER
,
...
@@ -86,9 +87,19 @@ def new_record(record_type, name=None, description=None,
...
@@ -86,9 +87,19 @@ def new_record(record_type, name=None, description=None,
def
id_query
(
ids
):
def
id_query
(
ids
):
q
=
"
FIND Entity with
"
+
"
OR
"
.
join
([
"
id={}
"
.
format
(
id
)
for
id
in
ids
])
warnings
.
warn
(
"
Please use
'
create_id_query
'
, which only creates
"
"
the string.
"
,
DeprecationWarning
)
return
execute_query
(
q
)
return
execute_query
(
create_id_query
(
ids
))
def
create_id_query
(
ids
):
return
"
FIND ENTITY WITH
"
+
"
OR
"
.
join
(
[
"
ID={}
"
.
format
(
id
)
for
id
in
ids
])
def
retrieve_entity_with_id
(
eid
):
return
execute_query
(
"
FIND ENTITY WITH ID={}
"
.
format
(
eid
),
unique
=
True
)
def
retrieve_entities_with_ids
(
entities
):
def
retrieve_entities_with_ids
(
entities
):
...
@@ -96,7 +107,9 @@ def retrieve_entities_with_ids(entities):
...
@@ -96,7 +107,9 @@ def retrieve_entities_with_ids(entities):
step
=
20
step
=
20
for
i
in
range
(
len
(
entities
)
//
step
+
1
):
for
i
in
range
(
len
(
entities
)
//
step
+
1
):
collection
.
extend
(
id_query
(
entities
[
i
*
step
:(
i
+
1
)
*
step
]))
collection
.
extend
(
execute_query
(
create_id_query
(
entities
[
i
*
step
:(
i
+
1
)
*
step
])))
return
collection
return
collection
...
@@ -707,3 +720,28 @@ def _apply_to_ids_of_entity(entity, func):
...
@@ -707,3 +720,28 @@ def _apply_to_ids_of_entity(entity, func):
else
:
else
:
if
prop
.
value
is
not
None
:
if
prop
.
value
is
not
None
:
prop
.
value
=
func
(
prop
.
value
)
prop
.
value
=
func
(
prop
.
value
)
def
resolve_reference
(
prop
:
Property
):
"""
resolves the value of a reference property
The integer value is replaced with the entity object.
If the property is not a reference, then the function returns without
change.
"""
if
not
prop
.
is_reference
(
server_retrieval
=
True
):
return
if
isinstance
(
prop
.
value
,
list
):
referenced
=
[]
for
val
in
prop
.
value
:
if
isinstance
(
val
,
int
):
referenced
.
append
(
retrieve_entity_with_id
(
val
))
else
:
referenced
.
append
(
val
)
prop
.
value
=
referenced
else
:
if
isinstance
(
prop
.
value
,
int
):
prop
.
value
=
retrieve_entity_with_id
(
prop
.
value
)
This diff is collapsed.
Click to expand it.
unittests/test_apiutils.py
+
41
−
1
View file @
2b75bb00
...
@@ -29,7 +29,8 @@
...
@@ -29,7 +29,8 @@
import
caosdb
as
db
import
caosdb
as
db
import
pickle
import
pickle
import
tempfile
import
tempfile
from
caosdb.apiutils
import
apply_to_ids
from
caosdb.apiutils
import
apply_to_ids
,
create_id_query
,
resolve_reference
import
caosdb.apiutils
from
.test_property
import
testrecord
from
.test_property
import
testrecord
...
@@ -62,3 +63,42 @@ def test_apply_to_ids():
...
@@ -62,3 +63,42 @@ def test_apply_to_ids():
assert
rec
.
parents
[
0
].
id
==
-
3456
assert
rec
.
parents
[
0
].
id
==
-
3456
assert
rec
.
properties
[
0
].
id
==
-
23345
assert
rec
.
properties
[
0
].
id
==
-
23345
assert
rec
.
id
==
-
23
assert
rec
.
id
==
-
23
def
test_id_query
():
ids
=
[
1
,
2
,
3
,
4
,
5
]
assert
create_id_query
(
ids
)
==
'
FIND ENTITY WITH ID=1 OR ID=2 OR ID=3 OR ID=4 OR ID=5
'
def
test_resolve_reference
():
original_retrieve_entity_with_id
=
caosdb
.
apiutils
.
retrieve_entity_with_id
caosdb
.
apiutils
.
retrieve_entity_with_id
=
lambda
eid
:
db
.
Record
(
id
=
eid
)
prop
=
db
.
Property
(
id
=
1
,
datatype
=
db
.
REFERENCE
,
value
=
100
)
prop
.
is_valid
=
lambda
:
True
items
=
[
200
,
300
,
400
]
prop_list
=
db
.
Property
(
datatype
=
db
.
LIST
(
db
.
REFERENCE
),
value
=
items
)
prop_list2
=
db
.
Property
(
datatype
=
db
.
LIST
(
db
.
REFERENCE
),
value
=
[
db
.
Record
(
id
=
500
)])
resolve_reference
(
prop
)
resolve_reference
(
prop_list
)
resolve_reference
(
prop_list2
)
assert
prop
.
value
.
id
==
100
assert
isinstance
(
prop
.
value
,
db
.
Entity
)
prop_list_ids
=
[]
for
i
in
prop_list
.
value
:
prop_list_ids
.
append
(
i
.
id
)
assert
isinstance
(
i
,
db
.
Entity
)
assert
prop_list_ids
==
items
for
i
in
prop_list2
.
value
:
assert
i
.
id
==
500
assert
isinstance
(
i
,
db
.
Entity
)
no_reference
=
db
.
Property
(
id
=
5000
,
datatype
=
db
.
INTEGER
,
value
=
2
)
resolve_reference
(
no_reference
)
assert
no_reference
.
value
==
2
assert
no_reference
.
datatype
is
db
.
INTEGER
# restore retrive_entity_with_id
caosdb
.
apiutils
.
retrieve_entity_with_id
=
original_retrieve_entity_with_id
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