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
edf7d590
Commit
edf7d590
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
MAINT: refactored the convert to value function
parent
27605bd7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!57
RELEASE 0.7.3
,
!52
F refactor high level api
Pipeline
#19493
canceled
3 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/high_level_api.py
+46
-26
46 additions, 26 deletions
src/caosdb/high_level_api.py
with
46 additions
and
26 deletions
src/caosdb/high_level_api.py
+
46
−
26
View file @
edf7d590
...
...
@@ -31,7 +31,10 @@ This is refactored from apiutils.
"""
from
caosdb.common.datatype
import
(
BOOLEAN
,
DATETIME
,
DOUBLE
,
FILE
,
INTEGER
,
REFERENCE
,
TEXT
,
is_list_datatype
,
get_list_datatype
)
REFERENCE
,
TEXT
,
is_list_datatype
,
get_list_datatype
,
is_reference
)
import
caosdb
as
db
from
.apiutils
import
get_type_of_entity_with
...
...
@@ -39,7 +42,7 @@ from .apiutils import get_type_of_entity_with
from
typing
import
Any
,
Optional
,
List
,
Union
,
Dict
from
dataclasses
import
dataclass
from
datetime
import
datetime
@dataclass
class
CaosDBPropertyMetaData
:
...
...
@@ -230,50 +233,67 @@ class CaosDBPythonEntity(object):
"""
self
.
_id
=
idx
def
_type_converted_list
(
self
,
val
,
pr
):
"""
Convert a list to a python list of the correct type.
"""
prrealpre
=
pr
.
replace
(
"
<
"
,
"
<
"
).
replace
(
"
>
"
,
"
>
"
)
prreal
=
prrealpre
[
prrealpre
.
index
(
"
<
"
)
+
1
:
prrealpre
.
rindex
(
"
>
"
)]
def
_type_converted_list
(
self
,
val
:
List
,
pr
:
str
):
"""
Convert a list to a python list of the correct type.
val: List
The value of a property containing the list.
pr: str
The datatype according to the database entry.
"""
if
not
is_list_datatype
(
pr
):
raise
RuntimeError
(
"
Not a list.
"
)
prreal
=
get_list_datatype
(
pr
)
lst
=
[
self
.
_type_converted_value
(
i
,
prreal
)
for
i
in
val
]
return
([
i
[
0
]
for
i
in
lst
],
lst
[
0
][
1
])
def
_type_converted_value
(
self
,
val
,
pr
):
"""
Convert val to the correct type which is indicated by the database
def
_type_converted_value
(
self
,
val
:
Any
,
pr
:
str
):
"""
Convert val to the correct type which is indicated by the database
type string in pr.
Returns a tuple with two entries:
- the converted value
- True if the value has to be interpreted as an id acting as a reference
If datatype is None return the tuple:
(value, False)
References with ids will be turned into CaosDBPythonUnresolvedReference.
"""
if
val
is
None
:
return
(
None
,
False
)
return
None
elif
pr
is
None
:
return
(
val
,
False
)
return
val
elif
pr
==
DOUBLE
:
return
(
float
(
val
)
,
False
)
return
float
(
val
)
elif
pr
==
BOOLEAN
:
return
(
bool
(
val
)
,
False
)
return
bool
(
val
)
elif
pr
==
INTEGER
:
return
(
int
(
val
)
,
False
)
return
int
(
val
)
elif
pr
==
TEXT
:
return
(
val
,
False
)
return
str
(
val
)
elif
pr
==
FILE
:
return
(
int
(
val
)
,
False
)
return
int
(
val
)
elif
pr
==
REFERENCE
:
return
(
int
(
val
),
True
)
return
CaosDBPythonUnresolvedReference
(
val
)
elif
pr
==
DATETIME
:
return
(
val
,
False
)
elif
pr
[
0
:
4
]
==
"
LIST
"
:
return
val
elif
is_list_datatype
(
pr
)
:
return
self
.
_type_converted_list
(
val
,
pr
)
elif
isinstance
(
val
,
db
.
Entity
):
return
(
convert_to_python_object
(
val
)
,
False
)
return
convert_to_python_object
(
val
)
else
:
return
(
int
(
val
),
True
)
# Generic references to entities:
return
CaosDBPythonUnresolvedReference
(
val
)
def
_parse_datetime
(
self
,
val
):
"""
Convert val into a datetime object.
"""
# TODO: try different representations
return
datetime
.
strptime
(
"
%Y-%m-%d %H:%M:%S
"
,
val
)
def
attribute_as_list
(
self
,
name
):
"""
This is a workaround for the problem that lists containing only one
...
...
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