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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
3ed11041
Commit
3ed11041
authored
2 years ago
by
florian
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Implement value removal function
parent
6464adec
No related branches found
No related tags found
1 merge request
!108
ENH: F remove value or property
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/caosdb/common/models.py
+60
-4
60 additions, 4 deletions
src/caosdb/common/models.py
with
60 additions
and
4 deletions
src/caosdb/common/models.py
+
60
−
4
View file @
3ed11041
# -*- coding: utf-8 -*-
#
# ** header v3.0
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright (C) 2020-202
2
Indiscale GmbH <info@indiscale.com>
# Copyright (C) 2020 Florian Spreckelsen <f.spreckelsen@indiscale.com>
# Copyright (C) 2020-202
3
Indiscale GmbH <info@indiscale.com>
# Copyright (C) 2020
-2023
Florian Spreckelsen <f.spreckelsen@indiscale.com>
# Copyright (C) 2020-2022 Timm Fitschen <t.fitschen@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
...
...
@@ -22,7 +21,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ** end header
#
"""
...
...
@@ -47,6 +45,7 @@ from os import listdir
from
os.path
import
isdir
from
random
import
randint
from
tempfile
import
NamedTemporaryFile
from
typing
import
Any
,
Optional
from
warnings
import
warn
from
caosdb.common.datatype
import
(
BOOLEAN
,
DATETIME
,
DOUBLE
,
INTEGER
,
TEXT
,
...
...
@@ -453,6 +452,63 @@ class Entity:
return
self
def
remove_value_from_property
(
self
,
property_name
:
str
,
value
:
Any
,
remove_if_empty_afterwards
:
Optional
[
bool
]
=
True
):
"""
Remove a value from a property given by name.
Do nothing if this entity does not have a property of this
``property_name`` or if the property value is different of the given
``value``. By default, the property is removed form this entity if it is
empty (i.e., value=None) after removal of the value. This behavior can
be changed by setting ``remove_if_empty_afterwards`` to ``False`` in which
case the property remains.
Notes
-----
If the property value is a list and the value to be removed occurs more
than once in this list, only it
'
s first occurrance is deleted (similar
to the behavior of Python
'
s ``list.remove()``.)
If the property is already empty and a value != None is to be removed,
the property is not removed afterwards even if
``remove_if_empty_afterwards`` is set to ``True`` (since it hasn
'
t been
emptied **because** this function was called but rather didn
'
t have a
value in the first place). This changes if the value to be removed is
set to ``None`` explicitly.
Parameters
----------
property_name : str
Name of the property from which the ``value`` will be removed.
value
Value that is to be removed.
remove_if_empty_afterwards : bool, optional
Whether the property is to be removed from this entity if it is
emptied by removing the ``value``. Default is ``True``.
Returns
-------
self
This entity.
"""
if
self
.
get_property
(
property_name
)
is
None
:
return
self
empty_afterwards
=
False
if
isinstance
(
self
.
get_property
(
property_name
).
value
,
list
):
if
value
in
self
.
get_property
(
property_name
).
value
:
self
.
get_property
(
property_name
).
value
.
remove
(
value
)
if
self
.
get_property
(
property_name
).
value
==
[]:
self
.
get_property
(
property_name
).
value
=
None
empty_afterwards
=
True
elif
self
.
get_property
(
property_name
).
value
==
value
:
self
.
get_property
(
property_name
).
value
=
None
empty_afterwards
=
True
if
remove_if_empty_afterwards
and
empty_afterwards
:
self
.
remove_property
(
property_name
)
return
self
def
remove_parent
(
self
,
parent
):
self
.
parents
.
remove
(
parent
)
...
...
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