python-benedict/benedict/core/keylists.py

33 lines
936 B
Python
Raw Permalink Normal View History

2020-02-04 09:33:20 +00:00
from benedict.utils import type_util
def _get_keylist_for_dict(d, parent_keys, indexes):
2020-02-04 11:26:10 +00:00
keylist = []
for key, value in d.items():
2020-02-04 09:33:20 +00:00
keys = parent_keys + [key]
2020-02-04 11:26:10 +00:00
keylist += [keys]
keylist += _get_keylist_for_value(value, keys, indexes)
2020-02-04 11:26:10 +00:00
return keylist
2020-02-04 09:33:20 +00:00
2022-03-24 12:26:18 +00:00
def _get_keylist_for_list(ls, parent_keys, indexes):
keylist = []
2022-03-24 12:26:18 +00:00
for key, value in enumerate(ls):
keys = list(parent_keys)
keys[-1] += f"[{key}]"
keylist += [keys]
keylist += _get_keylist_for_value(value, keys, indexes)
return keylist
def _get_keylist_for_value(value, parent_keys, indexes):
if type_util.is_dict(value):
return _get_keylist_for_dict(value, parent_keys, indexes)
elif type_util.is_list(value) and indexes:
return _get_keylist_for_list(value, parent_keys, indexes)
return []
def keylists(d, indexes=False):
return _get_keylist_for_value(d, [], indexes)