File size: 1,383 Bytes
6c50d1f | 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import numpy as np
def extract_dict_value(path="", data={}, value_collection={}):
prefix = ""
if len(path) > 0:
prefix = path + "."
for key in data.keys():
item = data[key]
path = prefix + key
resolve_json_value(path, item, value_collection)
def resolve_json_value(path, item, value_collection={}):
if isinstance(item, str):
value_collection[path] = item
elif isinstance(item, int):
value_collection[path] = item
elif isinstance(item, float):
value_collection[path] = item
elif item == None:
if len(path) > 0:
value_collection[path] = None
elif isinstance(item, dict):
if len(path) > 0:
value_collection[path] = item
extract_dict_value(path, item, value_collection)
elif isinstance(item, list):
if len(path) > 0:
value_collection[path] = item
# FIXME: ignore huge list item
np.random.shuffle(item)
for i, val in enumerate(item):
resolve_json_value(path + '[' + str(i) + ']', val, value_collection)
else:
raise Exception(f'Unknown parameter {path} {str(item)}')
def fetch_object_value_by_attribute_path(attribute_path: str, obj: dict):
tokens = attribute_path.split('.')
content = obj
for token in tokens:
content = content[token]
return content
|