Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
ChEMBL
C
ChEMBL
Main Web Interface
Elasticsearch Proxy API
Commits
7207a657
Commit
7207a657
authored
Sep 28, 2021
by
David Mendez
Browse files
Utils: add support for lists in leaves of dict properties paths
parent
b4c5f715
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
6 deletions
+85
-6
utils/dict_property_access.py
utils/dict_property_access.py
+14
-6
utils/test/test_dict_property_access.py
utils/test/test_dict_property_access.py
+71
-0
No files found.
utils/dict_property_access.py
View file @
7207a657
"""
Module with helper functions for accessing
dictionary
properties
Module with helper functions for accessing
container
properties
"""
def
get_property_value
(
dictionary
,
str_property
,
default_null_value
=
None
):
def
get_property_value
(
container
,
str_property
,
default_null_value
=
None
):
"""
:param
dictionary: dictionary
for which to get the value
:param
container: container
for which to get the value
:param str_property: string path of the property, e.g '_metadata.assay_data.assay_subcellular_fraction'
:param default_null_value: value to return when the value in the dict is None.
For example, it can return '' if indicated
...
...
@@ -17,11 +17,19 @@ def get_property_value(dictionary, str_property, default_null_value=None):
prop_parts
=
str_property
.
split
(
'.'
)
current_prop
=
prop_parts
[
0
]
if
len
(
prop_parts
)
>
1
:
current_obj
=
dictionary
.
get
(
current_prop
)
next_path
=
'.'
.
join
(
prop_parts
[
1
::])
current_obj
=
container
.
get
(
current_prop
)
if
current_obj
is
None
:
return
default_null_value
return
get_property_value
(
current_obj
,
'.'
.
join
(
prop_parts
[
1
::]))
return
get_property_value
(
current_obj
,
next_path
)
# support for paths inside a list
is_list
=
isinstance
(
container
,
list
)
if
is_list
:
return
[
get_property_value
(
item
,
current_prop
)
for
item
in
container
]
value
=
dictionary
.
get
(
current_prop
)
# usual paths inside a dict
value
=
container
.
get
(
current_prop
)
value
=
default_null_value
if
value
is
None
else
value
return
value
utils/test/test_dict_property_access.py
0 → 100644
View file @
7207a657
"""
Module to test the dict property access
"""
import
unittest
from
utils
import
dict_property_access
class
TestDictPropertyAccess
(
unittest
.
TestCase
):
"""
Class to test the property access module
"""
def
test_gets_simple_property
(
self
):
"""
Tests that it gets a simple property
"""
value_must_be
=
'hello'
prop_path
=
'prop1'
the_dict
=
{
'prop1'
:
value_must_be
}
value_got
=
dict_property_access
.
get_property_value
(
the_dict
,
prop_path
)
self
.
assertEqual
(
value_must_be
,
value_got
,
msg
=
f
'The value of
{
prop_path
}
was not obtained correctly!'
)
def
test_gets_nested_property
(
self
):
"""
Tests that it gets a nested property
"""
value_must_be
=
'hello'
prop_path
=
'prop1.prop2.prop3'
the_dict
=
{
'prop1'
:
{
'prop2'
:
{
'prop3'
:
value_must_be
}
}
}
value_got
=
dict_property_access
.
get_property_value
(
the_dict
,
prop_path
)
self
.
assertEqual
(
value_must_be
,
value_got
,
msg
=
f
'The value of
{
prop_path
}
was not obtained correctly!'
)
def
test_gets_nested_property
(
self
):
"""
Tests that it gets a nested property
"""
value_must_be
=
[
1
,
2
,
3
]
prop_path
=
'prop1.prop2.prop3.prop4'
the_dict
=
{
'prop1'
:
{
'prop2'
:
{
'prop3'
:
[
{
'prop4'
:
1
},
{
'prop4'
:
2
},
{
'prop4'
:
3
}
]
}
}
}
value_got
=
dict_property_access
.
get_property_value
(
the_dict
,
prop_path
)
self
.
assertEqual
(
value_must_be
,
value_got
,
msg
=
f
'The value of
{
prop_path
}
was not obtained correctly!'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment