diff --git a/edpop_explorer/readers/stcn.py b/edpop_explorer/readers/stcn.py index bdc10c1..9ccc0ec 100644 --- a/edpop_explorer/readers/stcn.py +++ b/edpop_explorer/readers/stcn.py @@ -15,6 +15,9 @@ def _remove_markup(input_str: str) -> str: def safeget(dictionary: Optional[dict], attribute_chain: tuple, first: bool = False): + """Safely get a (nested) attribute in a JSON-like structure. If the + result is a list and ``first`` is ``True``, return the first item + of the list.""" if len(attribute_chain) == 0: raise ValueError("The attribute_chain argument cannot be empty") attribute = attribute_chain[0] diff --git a/tests/readers/test_stcn.py b/tests/readers/test_stcn.py index ab6b45c..4a5ab51 100644 --- a/tests/readers/test_stcn.py +++ b/tests/readers/test_stcn.py @@ -38,4 +38,9 @@ def test_safeget_nested_second_attribute_nonexistent(): "attribute": { "other_attribute": "value" } - }, ("attribute", "attribute2")) is None \ No newline at end of file + }, ("attribute", "attribute2")) is None + +def test_safeget_first(): + assert safeget({ + "attribute": ["value1", "value2"] + }, ("attribute",), True) == "value1" \ No newline at end of file