python-benedict/benedict/core/keylists.py

35 lines
967 B
Python
Raw Normal View History

2020-02-04 09:33:20 +00:00
# -*- coding: utf-8 -*-
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
def _get_keylist_for_list(l, parent_keys, indexes):
keylist = []
for key, value in enumerate(l):
keys = list(parent_keys)
2022-02-13 10:35:43 +00:00
keys[-1] += "[{}]".format(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)