From 46281824bb0e8b855086badf12c8fad2a24e5ed9 Mon Sep 17 00:00:00 2001 From: nh916 Date: Fri, 22 Sep 2023 13:01:26 -0700 Subject: [PATCH] created utility function of `find_json_node_by_name` * get JSON node from list of JSON and if it can't find the node, then it returns None * not sure if we want to return None or raise an exception * the search function is a simple linear search * wrote docstrings for the function --- src/cript/utils/__init__.py | 0 src/cript/utils/tools.py | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/cript/utils/__init__.py create mode 100644 src/cript/utils/tools.py diff --git a/src/cript/utils/__init__.py b/src/cript/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/cript/utils/tools.py b/src/cript/utils/tools.py new file mode 100644 index 000000000..51e5394c7 --- /dev/null +++ b/src/cript/utils/tools.py @@ -0,0 +1,24 @@ +from typing import Dict, Optional + + +def find_json_node_by_name(list_of_nodes: Dict, target_name: str) -> Optional[Dict]: + """ + Searches through a list of JSON nodes and finds the desired node by name + + Parameters + ---------- + list_of_nodes: Dict + list of nodes in JSON format + target_name: str + the name of the node you want to find from the list of nodes + + Returns + ------- + Dict + desired node in JSON format + """ + for node_json in list_of_nodes: + if node_json["name"] == target_name: + return node_json + + return None