Commit 7207a657 authored by David Mendez's avatar David Mendez
Browse files

Utils: add support for lists in leaves of dict properties paths

parent b4c5f715
"""
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
"""
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!')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment