Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a new function: get_value_in_nested_dict #31

Open
wants to merge 1 commit into
base: release
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions esm_parser/esm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,43 @@ def find_key(d_search, k_search, exc_strings = "", level = "", paths2finds = [],
return paths2finds


def get_value_in_nested_dict(container, search_key):
"""Checks for the ``search_key`` inside the (nested) dictionary ``container``
and returns the generator object of values of that key.

Parameters
----------
container : dict
dictionary to search for
search_key : any "hashable" Python object, eg. str, int

Yields
------
result : generator object
value(s) of the key ``search_key`` in the dictionary
"""

# case 1: the container to search in is a dictionary
if isinstance(container, dict):
# what we are looking for is already a key in the dictionary, yield
# it's value
if search_key in container:
yield container[search_key]

# or it might be inside its values. For each value search recursively
for value in container.values():
for result in get_value_in_nested_dict(value, search_key):
yield result

# case 2: the node of the nested dictionary is a list. Recursively search
# its items
elif isinstance(container, list):
for item in container:
for result in get_value_in_nested_dict(item, search_key):
yield result



def user_note(note_heading, note_text):
"""
Notify the user about something. In the future this should also write in the log.
Expand Down