def change_key(obj, entry, rename_map): child_entry_path = entry parent_entry_obj = obj # If entry has nested path find the immediate parent if entry.find("/") != -1: parent_entry_path = entry.rsplit("/",1)[0] child_entry_path = entry.rsplit("/",1)[1] parent_entry_obj = iterate_dictionary(obj, parent_entry_path) if parent_entry_obj != None: # Check for list if type(parent_entry_obj) == list: for o in parent_entry_obj: if iterate_dictionary(o, child_entry_path) is not None: # Pop fields with empty array value if rename_map[entry] == "remove": o.pop(child_entry_path) elif o.get(child_entry_path) == []: o.pop(child_entry_path) else: # Rename the key o[rename_map[entry]] = o.pop(child_entry_path) elif child_entry_path in o: o.pop(child_entry_path) else: if parent_entry_obj.get(child_entry_path) is not None: # Pop fields with empty array value if rename_map[entry] == "remove": parent_entry_obj.pop(child_entry_path) elif parent_entry_obj.get(child_entry_path) == []: parent_entry_obj.pop(child_entry_path) else: # Rename the key parent_entry_obj[rename_map[entry]] = parent_entry_obj.pop(child_entry_path)
def _parse_by_template(self, content, mapping): """ Define emplate and add content to payload :param content: :param mapping: :return: """ response = {} for key, val in mapping.items(): response[key] = iterate_dictionary(content, val) # Because all return list as value so we must get real value if response[key] is not None and len( response[key]) == 1 and not isinstance( response[key][0], dict): response[key] = response[key][0] return response
from dictsearch.search import iterate_dictionary A = dict() A["A"] = [] A["A"].append("1") A["A"].append(dict()) A["A"][1]["B"] = 123 A["A"].append("1") A["A"].append(dict()) A["A"][3]["B"] = 123 print(iterate_dictionary(A, "A/B"))
from dictsearch.search import iterate_dictionary d = dict() d["A"] = dict() d["A"]["B"] = [] d["A"]["B"].append(dict()) d["A"]["B"][0]["C"] = 123 d["A"]["B"].append(dict()) d["A"]["B"][1]["C"] = 123 d["A"]["B"].append(dict()) d["A"]["B"][2]["D"] = dict() d["A"]["B"][2]["D"]["E"] = "ASDF" print(d) print(iterate_dictionary(d,"X/Y/Z")) print(iterate_dictionary(d,"A")) print(iterate_dictionary(d,"A/B")) print(iterate_dictionary(d,"A/C")) print(iterate_dictionary(d,"A/B/C")) print(iterate_dictionary(d,"A/B/C/D")) #DOES NOT EXIST; D NOT AFTER PATH "C" print(iterate_dictionary(d,"A/B/D/E")) print(iterate_dictionary(d,"A/B/E")) #DOES NOT EXIST; E NOT A SUBPATH OF "B"
from dictsearch.search import iterate_dictionary d = dict() d["A"] = dict() d["A"]["B"] = [] d["A"]["B"].append(dict()) d["A"]["B"][0]["C"] = 123 d["A"]["B"].append(dict()) d["A"]["B"][1]["C"] = 123 d["A"]["B"].append(dict()) d["A"]["B"][2]["D"] = dict() d["A"]["B"][2]["D"]["E"] = "ASDF" print(d) print(iterate_dictionary(d, "X/Y/Z")) print(iterate_dictionary(d, "A")) print(iterate_dictionary(d, "A/B")) print(iterate_dictionary(d, "A/C")) print(iterate_dictionary(d, "A/B/C")) print(iterate_dictionary(d, "A/B/C/D")) #DOES NOT EXIST; D NOT AFTER PATH "C" print(iterate_dictionary(d, "A/B/D/E")) print(iterate_dictionary(d, "A/B/E")) #DOES NOT EXIST; E NOT A SUBPATH OF "B"