Addition of new feature (get_all_keys)

This commit is contained in:
Ramesh RV 2018-09-23 17:46:50 +05:30
parent 78fac75205
commit bb7ab509eb
5 changed files with 198 additions and 30 deletions

View file

@ -1 +1 @@
from .nested_lookup import nested_lookup
from .nested_lookup import nested_lookup, get_all_keys

View file

@ -2,20 +2,26 @@ from six import iteritems
from collections import defaultdict
def nested_lookup(key, document, wild=False, with_keys=False):
"""Lookup a key in a nested document, return a list of values"""
if with_keys:
d = defaultdict(list)
for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys):
for k, v in _nested_lookup(
key, document, wild=wild, with_keys=with_keys
):
d[k].append(v)
return d
return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
def _nested_lookup(key, document, wild=False, with_keys=False):
"""Lookup a key in a nested document, yield a value"""
if isinstance(document, list):
for d in document:
for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
for result in _nested_lookup(
key, d, wild=wild, with_keys=with_keys
):
yield result
if isinstance(document, dict):
@ -26,9 +32,39 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
else:
yield v
elif isinstance(v, dict):
for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys):
for result in _nested_lookup(
key, v, wild=wild, with_keys=with_keys
):
yield result
elif isinstance(v, list):
for d in v:
for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
for result in _nested_lookup(
key, d, wild=wild, with_keys=with_keys
):
yield result
def get_all_keys(dictionary):
"""
Method to get all keys from a nested dictionary as a List
Args:
dictionary: Nested dictionary
Returns:
List of keys in the dictionary
"""
result_list = []
def recrusion(dictionary):
for key, value in iteritems(dictionary):
if isinstance(value, dict):
result_list.append(key)
recrusion(dictionary=value)
elif isinstance(value, list):
result_list.append(key)
for list_items in value:
recrusion(dictionary=list_items)
else:
result_list.append(key)
recrusion(dictionary=dictionary)
return result_list