New nested_get function
Return a list of all values from the nested dictionary with the given key.
Change-Id: I43fd8291b57c433a411a9f6e3c2de61b8a6afb42
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/var_funcs.py b/lib/var_funcs.py
index a3ca1ce..ceed9f8 100644
--- a/lib/var_funcs.py
+++ b/lib/var_funcs.py
@@ -678,3 +678,45 @@
report_list = list(filter(None, out_buf.split("\n")))
return list_to_report(report_list, **args)
+
+
+def nested_get(key, dictionary):
+ r"""
+ Return a list of all values from the nested dictionary with the given key.
+
+ Example:
+
+ Given a dictionary named personnel with the following contents:
+
+ personnel:
+ [manager]:
+ [last_name]: Doe
+ [first_name]: John
+ [accountant]:
+ [last_name]: Smith
+ [first_name]: Will
+
+ The following code...
+
+ last_names = nested_get('last_name', personnel)
+ print_var(last_names)
+
+ Would result in the following data:
+
+ last_names:
+ last_names[0]: Doe
+ last_names[1]: Smith
+
+ Description of argument(s):
+ key The key value.
+ dictionary The nested dictionary.
+ """
+
+ result = []
+ for k, v in dictionary.items():
+ if isinstance(v, dict):
+ result += nested_get(key, v)
+ if k == key:
+ result.append(v)
+
+ return result