recursive dictionary
def insert_data(dictionary, path, value):
if len(path) == 0:
dictionary.setdefault('*', []).append(value)
else:
insert_data(dictionary.setdefault(path[0], {}), path[1:], value)
def get_data(dictionary, path):
try:
res = get_data(dictionary[path[0]], path[1:])
except (KeyError, IndexError):
res = dictionary.get('*')
return res
class custom_dict(dict):
def insert_data(self, path: list, value: any) -> None:
if len(path) == 0:
self.setdefault('*', []).append(value)
else:
custom_dict.insert_data(self.setdefault(path[0], {}), path[1:], value)
def get_data(self, path: list) -> list:
try:
res = custom_dict.get_data(self[path[0]], path[1:])
except (KeyError, IndexError):
res = self.get('*')
return res