Skip to content

Commit

Permalink
Add docstring to safeget method and an additional test
Browse files Browse the repository at this point in the history
  • Loading branch information
tijmenbaarda committed Dec 23, 2024
1 parent 145a6fc commit ede0473
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions edpop_explorer/readers/stcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion tests/readers/test_stcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ def test_safeget_nested_second_attribute_nonexistent():
"attribute": {
"other_attribute": "value"
}
}, ("attribute", "attribute2")) is None
}, ("attribute", "attribute2")) is None

def test_safeget_first():
assert safeget({
"attribute": ["value1", "value2"]
}, ("attribute",), True) == "value1"

0 comments on commit ede0473

Please sign in to comment.