General, node-yielding generator. Yields (node, path) tuples when it finds values that are dict instances. A path is a sequence of hashable values that can be used as either keys to a mapping (dict) or indices to a sequence (list). A path is always wrt to some object. Give
(node, path=())
| 130 | |
| 131 | |
| 132 | def node_generator(node, path=()): |
| 133 | """ |
| 134 | General, node-yielding generator. |
| 135 | |
| 136 | Yields (node, path) tuples when it finds values that are dict |
| 137 | instances. |
| 138 | |
| 139 | A path is a sequence of hashable values that can be used as either keys to |
| 140 | a mapping (dict) or indices to a sequence (list). A path is always wrt to |
| 141 | some object. Given an object, a path explains how to get from the top level |
| 142 | of that object to a nested value in the object. |
| 143 | |
| 144 | :param (dict) node: Part of a dict to be traversed. |
| 145 | :param (tuple[str]) path: Defines the path of the current node. |
| 146 | :return: (Generator) |
| 147 | |
| 148 | Example: |
| 149 | |
| 150 | >>> for node, path in node_generator({'a': {'b': 5}}): |
| 151 | ... print(node, path) |
| 152 | {'a': {'b': 5}} () |
| 153 | {'b': 5} ('a',) |
| 154 | |
| 155 | """ |
| 156 | if not isinstance(node, dict): |
| 157 | return # in case it's called with a non-dict node at top level |
| 158 | yield node, path |
| 159 | for key, val in node.items(): |
| 160 | if isinstance(val, dict): |
| 161 | for item in node_generator(val, path + (key,)): |
| 162 | yield item |
| 163 | |
| 164 | |
| 165 | def get_by_path(obj, path): |