Skip to content
Snippets Groups Projects
Commit 8f96c9cb authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

DEV: new utility function

parent 92588f0d
Branches
Tags
No related merge requests found
...@@ -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
......
...@@ -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"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment