Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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-advanced-user-tools
Commits
8f96c9cb
Commit
8f96c9cb
authored
5 years ago
by
Henrik tom Wörden
Browse files
Options
Downloads
Patches
Plain Diff
DEV: new utility function
parent
92588f0d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosadvancedtools/cfood.py
+39
-0
39 additions, 0 deletions
src/caosadvancedtools/cfood.py
unittests/test_cfood.py
+2
-2
2 additions, 2 deletions
unittests/test_cfood.py
with
41 additions
and
2 deletions
src/caosadvancedtools/cfood.py
+
39
−
0
View file @
8f96c9cb
...
@@ -262,6 +262,45 @@ def assure_has_parent(entity, parent, to_be_updated=None, verbose=True):
...
@@ -262,6 +262,45 @@ def assure_has_parent(entity, parent, to_be_updated=None, verbose=True):
to_be_updated
.
append
(
entity
)
to_be_updated
.
append
(
entity
)
def
assure_has_property
(
entity
,
name
,
value
,
to_be_updated
=
None
,
verbose
=
True
):
"""
Checks whether `entity` has a property `name` with the value `value`.
If this is the case this function ends. Otherwise the entity is assigned
a new parent. The list to_be_updated is supplied, the entity is added to
the list in order to indicate, that the entity `entity` should be updated.
Otherwise it is directly updated
"""
properties
=
entity
.
get_properties
()
possible_properties
=
[
prop
for
prop
in
properties
if
prop
.
name
==
name
]
contained
=
False
for
el
in
possible_properties
:
if
el
.
value
==
value
:
contained
=
True
break
if
contained
:
if
verbose
:
print
(
"
entity {} has property {} with value {}
"
.
format
(
entity
.
id
,
name
,
value
))
return
if
verbose
:
print
(
"
Adding property {} with value {} to entity {}
"
.
format
(
name
,
value
,
entity
.
id
))
entity
.
add_property
(
name
=
name
,
value
=
value
)
if
to_be_updated
is
None
:
entity
.
update
(
unique
=
False
)
else
:
to_be_updated
.
append
(
entity
)
def
insert_id_based_on_name
(
entity
):
def
insert_id_based_on_name
(
entity
):
if
entity
.
name
is
not
None
and
(
entity
.
id
is
None
or
entity
.
id
<
0
):
if
entity
.
name
is
not
None
and
(
entity
.
id
is
None
or
entity
.
id
<
0
):
entity
.
id
=
get_entity
(
entity
.
name
).
id
entity
.
id
=
get_entity
(
entity
.
name
).
id
...
...
This diff is collapsed.
Click to expand it.
unittests/test_cfood.py
+
2
−
2
View file @
8f96c9cb
...
@@ -24,7 +24,6 @@
...
@@ -24,7 +24,6 @@
import
unittest
import
unittest
import
caosdb
as
db
import
caosdb
as
db
from
caosadvancedtools.cfood
import
(
AbstractCFood
,
assure_has_parent
,
from
caosadvancedtools.cfood
import
(
AbstractCFood
,
assure_has_parent
,
assure_object_is_in_list
)
assure_object_is_in_list
)
...
@@ -43,7 +42,8 @@ class CFoodReTest(unittest.TestCase):
...
@@ -43,7 +42,8 @@ class CFoodReTest(unittest.TestCase):
self
.
assertEquals
(
TestCFood
.
get_re
(),
PATTERN
)
self
.
assertEquals
(
TestCFood
.
get_re
(),
PATTERN
)
self
.
assertEqual
(
TestCFood
.
_pattern
,
None
)
self
.
assertEqual
(
TestCFood
.
_pattern
,
None
)
self
.
assertIsNotNone
(
TestCFood
.
match
(
"
hallo
"
))
self
.
assertIsNotNone
(
TestCFood
.
match
(
"
hallo
"
))
self
.
assertIsNotNone
(
TestCFood
.
_pattern
)
# TODO the caching is of compiled re is disabled currently
# self.assertIsNotNone(TestCFood._pattern)
self
.
assertIsNotNone
(
TestCFood
.
match
(
"
hallo
"
))
self
.
assertIsNotNone
(
TestCFood
.
match
(
"
hallo
"
))
self
.
assertIsNone
(
TestCFood
.
match
(
"
allo
"
))
self
.
assertIsNone
(
TestCFood
.
match
(
"
allo
"
))
...
...
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