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
4fea5909
Commit
4fea5909
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
MAINT: refactor add parent methods and added getters and setters
parent
edf7d590
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
#19500
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
+98
-37
98 additions, 37 deletions
src/caosdb/high_level_api.py
with
98 additions
and
37 deletions
src/caosdb/high_level_api.py
+
98
−
37
View file @
4fea5909
...
...
@@ -109,6 +109,46 @@ class CaosDBPythonEntity(object):
# which must not be changed by the set_property function.
self
.
_forbidden
=
dir
(
self
)
+
[
"
_forbidden
"
]
@property
def
id
(
self
):
"""
Getter for the id.
"""
return
self
.
_id
@id.setter
def
id
(
self
,
val
:
int
):
self
.
_id
=
val
@property
def
name
(
self
):
"""
Getter for the name.
"""
return
self
.
_name
@name.setter
def
name
(
self
,
val
:
str
):
self
.
_name
=
name
@property
def
description
(
self
):
"""
Getter for the description.
"""
return
self
.
_description
@description.setter
def
description
(
self
,
val
:
str
):
self
.
_description
=
description
@property
def
version
(
self
):
"""
Getter for the version.
"""
return
self
.
_version
# @staticmethod
# def _get_new_id():
# """
...
...
@@ -222,16 +262,15 @@ class CaosDBPythonEntity(object):
else
:
self
.
__setattr__
(
name
,
value
)
# add_property = set_property
def
set_id
(
self
,
idx
:
int
):
def
__setattr__
(
self
,
name
:
str
,
val
:
Any
):
"""
Explicitely set the id of this entity.
idx: int
The new ID for this entity.
Allow setting generic properties.
"""
self
.
_id
=
idx
# TODO: implement checking the value to correspond to one of the datatypes
# known for conversion.
super
().
__setattr__
(
name
,
val
)
def
_type_converted_list
(
self
,
val
:
List
,
...
...
@@ -247,10 +286,9 @@ class CaosDBPythonEntity(object):
"""
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
])
return
[
self
.
_type_converted_value
(
i
,
get_list_datatype
(
pr
))
for
i
in
val
]
def
_type_converted_value
(
self
,
val
:
Any
,
...
...
@@ -279,7 +317,7 @@ class CaosDBPythonEntity(object):
elif
pr
==
REFERENCE
:
return
CaosDBPythonUnresolvedReference
(
val
)
elif
pr
==
DATETIME
:
return
val
return
self
.
_parse_datetime
(
val
)
elif
is_list_datatype
(
pr
):
return
self
.
_type_converted_list
(
val
,
pr
)
elif
isinstance
(
val
,
db
.
Entity
):
...
...
@@ -288,50 +326,75 @@ class CaosDBPythonEntity(object):
# Generic references to entities:
return
CaosDBPythonUnresolvedReference
(
val
)
def
_parse_datetime
(
self
,
val
):
def
_parse_datetime
(
self
,
val
:
Union
[
str
,
datetime
]
):
"""
Convert val into a datetime object.
"""
if
isinstance
(
val
,
datetime
):
return
val
# 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
element are indistinguishable from simple types in this
representation.
"""
def
get_property
(
self
,
name
:
str
):
"""
Return the value of the property with name name.
Raise an exception if the property does not exist.
"""
if
not
self
.
property_exists
(
name
):
raise
RuntimeError
(
"
Property {} does not exist.
"
.
format
(
name
))
att
=
self
.
__getattribute__
(
name
)
return
att
def
attribute_as_list
(
self
,
name
:
str
):
"""
This is a workaround for the problem that lists containing only one
element are indistinguishable from simple types in this
representation.
TODO: still relevant? seems to be only a problem if LIST types are not used.
"""
att
=
self
.
get_property
(
name
)
if
isinstance
(
att
,
list
):
return
att
else
:
return
[
att
]
def
_add_parent
(
self
,
parent
):
def
add_parent
(
self
,
parent
:
Union
[
CaosDBPythonUnresolvedParent
,
CaosDBPythonRecordType
]):
"""
TODO
Add a parent to this entity. Either using an unresolved parent or
using a real record type.
"""
self
.
_parents
.
append
(
parent
.
id
)
def
add_parent
(
self
,
parent_id
=
None
,
parent_name
=
None
):
if
self
.
has_parent
(
parent
):
raise
RuntimeError
(
"
Duplicate parent.
"
)
self
.
_parents
.
add
(
parent
)
def
has_parent
(
self
,
parent
:
Union
[
CaosDBPythonUnresolvedParent
,
CaosDBPythonRecordType
]):
"""
TODO
Check whether this parent already exists for this entity.
"""
if
parent_id
is
not
None
:
self
.
_parents
.
append
(
parent
_
id
)
elif
parent_name
is
not
None
:
s
el
f
.
_parents
.
append
(
parent
_
name
)
else
:
raise
ValueError
(
"
no parent identifier supplied
"
)
for
p
in
self
.
_parents
:
if
p
.
id
==
parent
.
id
:
return
True
el
if
p
.
name
==
parent
.
name
:
return
True
return
False
def
get_parent_names
(
self
):
new_plist
=
[]
#
def get_parent_names(self):
#
new_plist = []
for
p
in
self
.
_parents
:
obj_type
=
get_type_of_entity_with
(
p
)
ent
=
obj_type
(
id
=
p
).
retrieve
()
new_plist
.
append
(
ent
.
name
)
#
for p in self._parents:
#
obj_type = get_type_of_entity_with(p)
#
ent = obj_type(id=p).retrieve()
#
new_plist.append(ent.name)
return
new_plist
#
return new_plist
def
resolve_references
(
self
,
deep
=
False
,
visited
=
dict
()):
for
i
in
self
.
_references
:
...
...
@@ -395,8 +458,6 @@ class CaosDBPythonProperty(CaosDBPythonEntity):
pass
class
CaosDBPythonFile
(
CaosDBPythonEntity
):
def
get_File
(
self
,
target
=
None
):
f
=
db
.
File
(
id
=
self
.
_id
).
retrieve
()
...
...
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