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
958fb0e1
Commit
958fb0e1
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: added basic conversion from high level representation back to standard representation
parent
e71431c4
No related branches found
No related tags found
2 merge requests
!57
RELEASE 0.7.3
,
!52
F refactor high level api
Pipeline
#19905
failed
3 years ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosdb/high_level_api.py
+43
-25
43 additions, 25 deletions
src/caosdb/high_level_api.py
unittests/test_high_level_api.py
+34
-0
34 additions, 0 deletions
unittests/test_high_level_api.py
with
77 additions
and
25 deletions
src/caosdb/high_level_api.py
+
43
−
25
View file @
958fb0e1
...
...
@@ -625,30 +625,30 @@ def _single_convert_to_python_object(robj: CaosDBPythonEntity,
robj
.
add_parent
(
CaosDBPythonUnresolvedParent
(
id
=
parent
.
id
,
name
=
parent
.
name
))
# Resolve so far unresolved references also using the provided references container:
# TODO
return
robj
def
_single_convert_to_entity
(
entity
:
db
.
Entity
,
robj
:
CaosDBPythonEntity
,
recursive_depth
,
**
kwargs
):
robj
:
CaosDBPythonEntity
):
"""
recursive_depth: disabled if 0
Convert a CaosDBPythonEntity to an entity in standard pylib format.
entity: db.Entity
An empty entity.
robj: CaosDBPythonEntity
The CaosDBPythonEntity that is supposed to be converted to the entity.
"""
for
base_attribute
in
BASE_ATTRIBUTES
:
modified_base_attribute
=
base_attribute
if
base_attribute
in
(
"
file
"
,
"
path
"
):
modified_base_attribute
=
"
_
"
+
modified_base_attribute
#
for base_attribute in BASE_ATTRIBUTES:
#
modified_base_attribute = base_attribute
#
if base_attribute in ("file", "path"):
#
modified_base_attribute = "_" + modified_base_attribute
entity
.
__setattr__
(
modified_base_attribute
,
robj
.
__getattribute__
(
base_attribute
))
children
=
[]
# entity.__setattr__(modified_base_attribute,
# robj.__getattribute__(base_attribute))
for
parent
in
robj
.
_parents
:
for
parent
in
robj
.
get
_parents
()
:
if
isinstance
(
parent
,
CaosDBPythonUnresolvedParent
):
entity
.
add_parent
(
name
=
parent
.
name
,
id
=
parent
.
id
)
elif
isinstance
(
parent
,
CaosDBPythonRecordType
):
...
...
@@ -656,8 +656,27 @@ def _single_convert_to_entity(entity: db.Entity,
else
:
raise
RuntimeError
(
"
Incompatible class used as parent.
"
)
# TODO: implementation incomplete
raise
NotImplementedError
()
for
prop
in
robj
.
get_properties
():
propval
=
robj
.
__getattribute__
(
prop
)
metadata
=
robj
.
get_property_metadata
(
prop
)
if
isinstance
(
propval
,
CaosDBPythonUnresolvedReference
):
propval
=
propval
.
id
elif
isinstance
(
propval
,
CaosDBPythonEntity
):
propval
=
_single_convert_to_entity
(
db
.
Record
(),
propval
)
elif
isinstance
(
propval
,
list
):
raise
NotImplementedError
()
entity
.
add_property
(
name
=
prop
,
value
=
propval
,
unit
=
metadata
.
unit
,
importance
=
metadata
.
importance
,
datatype
=
metadata
.
datatype
,
description
=
metadata
.
description
,
id
=
metadata
.
id
)
return
entity
def
add_property
(
entity
,
prop
,
name
,
_recursive
=
False
,
datatype
=
None
):
if
datatype
is
None
:
...
...
@@ -730,22 +749,21 @@ def _single_convert_to_entity(entity: db.Entity,
return
[
entity
]
+
children
def
convert_to_entity
(
python_object
,
**
kwargs
):
raise
NotImplementedError
()
def
convert_to_entity
(
python_object
):
if
isinstance
(
python_object
,
db
.
Container
):
# Create a list of objects:
return
[
convert_to_
python_object
(
i
,
**
kwargs
)
for
i
in
python_object
]
return
[
convert_to_
entity
(
i
)
for
i
in
python_object
]
elif
isinstance
(
python_object
,
CaosDBPythonRecord
):
return
_single_convert_to_entity
(
db
.
Record
(),
python_object
,
**
kwargs
)
return
_single_convert_to_entity
(
db
.
Record
(),
python_object
)
elif
isinstance
(
python_object
,
CaosDBPythonFile
):
return
_single_convert_to_entity
(
db
.
File
(),
python_object
,
**
kwargs
)
return
_single_convert_to_entity
(
db
.
File
(),
python_object
)
elif
isinstance
(
python_object
,
CaosDBPythonRecordType
):
return
_single_convert_to_entity
(
db
.
RecordType
(),
python_object
,
**
kwargs
)
return
_single_convert_to_entity
(
db
.
RecordType
(),
python_object
)
elif
isinstance
(
python_object
,
CaosDBPythonProperty
):
return
_single_convert_to_entity
(
db
.
Property
(),
python_object
,
**
kwargs
)
return
_single_convert_to_entity
(
db
.
Property
(),
python_object
)
elif
isinstance
(
python_object
,
CaosDBPythonEntity
):
return
_single_convert_to_entity
(
db
.
Entity
(),
python_object
,
**
kwargs
)
return
_single_convert_to_entity
(
db
.
Entity
(),
python_object
)
else
:
raise
ValueError
(
"
Cannot convert an object of this type.
"
)
...
...
This diff is collapsed.
Click to expand it.
unittests/test_high_level_api.py
+
34
−
0
View file @
958fb0e1
...
...
@@ -29,6 +29,8 @@ from caosdb.high_level_api import (convert_to_entity, convert_to_python_object)
from
caosdb.high_level_api
import
(
CaosDBPythonUnresolvedParent
,
CaosDBPythonUnresolvedReference
,
CaosDBPythonRecord
)
from
caosdb.apiutils
import
compare_entities
import
pytest
from
lxml
import
etree
import
os
...
...
@@ -225,4 +227,36 @@ def test_resolve_references():
obj
=
convert_to_python_object
(
r
)
obj
.
resolve_references
(
True
,
references
)
assert
obj
.
ref
.
ref
.
ref
==
obj
.
ref
def
equal_entities
(
r1
,
r2
):
res
=
compare_entities
(
r1
,
r2
)
if
len
(
res
)
!=
2
:
return
False
for
i
in
range
(
2
):
if
len
(
res
[
i
][
"
parents
"
])
!=
0
or
len
(
res
[
i
][
"
properties
"
])
!=
0
:
return
False
return
True
def
test_conversion_to_entity
():
r
=
db
.
Record
()
r
.
add_parent
(
"
bla
"
)
r
.
add_property
(
name
=
"
a
"
,
value
=
42
)
r
.
add_property
(
name
=
"
b
"
,
value
=
"
test
"
)
obj
=
convert_to_python_object
(
r
)
rconv
=
convert_to_entity
(
obj
)
assert
equal_entities
(
r
,
rconv
)
# With datatype:
r_ref
=
db
.
Record
()
r_ref
.
add_parent
(
"
bla
"
)
r_ref
.
add_property
(
name
=
"
a
"
,
value
=
42
)
r
=
db
.
Record
()
r
.
add_property
(
name
=
"
ref
"
,
value
=
r_ref
)
obj
=
convert_to_python_object
(
r
)
rconv
=
convert_to_entity
(
obj
)
assert
(
rconv
.
get_property
(
"
ref
"
).
value
.
get_property
(
"
a
"
).
value
==
r
.
get_property
(
"
ref
"
).
value
.
get_property
(
"
a
"
).
value
)
assert
len
(
rconv
.
properties
)
==
len
(
r
.
properties
)
assert
len
(
rconv
.
parents
)
==
len
(
r
.
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