-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdic_searcher.py
More file actions
26 lines (22 loc) · 1.49 KB
/
dic_searcher.py
File metadata and controls
26 lines (22 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from collections import OrderedDict # the data appears to contain an OrderedDict.
import pprint
def recurse_object(obj_to_inspect, val_to_find, indexpath=""):
if isinstance(obj_to_inspect, dict):
for key, value in obj_to_inspect.items():
recurse_object(value, val_to_find, indexpath + f"['{key}']")
if isinstance(obj_to_inspect, list):
for key, value in enumerate(obj_to_inspect):
recurse_object(value, val_to_find, indexpath + f"[{key}]")
if isinstance(obj_to_inspect, str):
if obj_to_inspect == val_to_find:
print(f"Value {val_to_find} found at {indexpath}")
dictionary = {'nestedA': OrderedDict(
[('Name', 'TestCase'), ('VarA', 'Local'), ('VarB', None), ('VarC', None), ('VarD', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
('VarE', 'ABCD'), ('VarF', 10.0), ('VarG', False)]), 'nestedB': OrderedDict([('LocA', {'v1': 'ABCD', 'v2': None,
'v3': None, 'v4': [
[[0, 1], [1, 1], [2, 2], [0, 4]], [[0, 2], [1, 2], [2, 3], [4, 4]], ['name', 'ABCD']]}), ('LocB', None),
('LocC', None), ('LocD', True),
('LocE', True), ('LocF', False),
('LocG', False)])}
recurse_object(dictionary, 'ABCD')
pprint.pprint(dictionary)