diff --git a/README.rst b/README.rst index cd427ec..10c4c44 100644 --- a/README.rst +++ b/README.rst @@ -51,6 +51,39 @@ substring of all the keys in the document and returns any values which match. For example: +.. code-block:: python + + from nested_lookup import nested_lookup + + my_document = { + 'name' : 'Russell Ballestrini', + 'email_address' : 'test1@example.com', + 'other' : { + 'secondary_email' : 'test2@example.com', + 'EMAIL_RECOVERY' : 'test3@example.com', + }, + }, + + results = nested_lookup( + key = 'mail', + document = my_document, + wild = True + ) + + print(results) + ['test1@example.com', 'test2@example.com', 'test3@example.com'] + + +output +======== + +There are two `output` modes: + +* `list`: the function returns a list of values corresponding to the matched keys. +* `dict`: the function returns a `dict` with the matched keys as keys and their corresponding values as values. + +For example: + .. code-block:: python from nested_lookup import nested_lookup @@ -66,26 +99,28 @@ For example: results = nested_lookup( key = 'mail', - document = my_document + document = my_document, wild = True, + output = 'dict' ) print(results) - ['test1@example.com', 'test2@example.com', 'test3@example.com'] - + {'email_address': 'test1@example.com', + 'secondary_email': 'test2@example.com', + 'EMAIL_RECOVERY': 'test3@example.com'} + misc ======== -:license: +:license: * Public Domain -:authors: +:authors: * Russell Ballestrini * Douglas Miranda -:web: +:web: * http://russell.ballestrini.net * http://douglasmiranda.com * https://gist.github.com/douglasmiranda/5127251 - diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index d5a7226..645b657 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -1,10 +1,13 @@ from six import iteritems -def nested_lookup(key, document, wild=False): +def nested_lookup(key, document, wild=False, output='list'): """Lookup a key in a nested document, return a list of values""" - return list(_nested_lookup(key, document, wild=wild)) + if output == 'dict': + return dict(_nested_lookup(key, document, wild=wild, output=output)) + else: + return list(_nested_lookup(key, document, wild=wild, output=output)) -def _nested_lookup(key, document, wild=False): +def _nested_lookup(key, document, wild=False, output='list'): """Lookup a key in a nested document, yield a value""" if isinstance(document, list): for d in document: @@ -14,12 +17,14 @@ def _nested_lookup(key, document, wild=False): if isinstance(document, dict): for k, v in iteritems(document): if key == k or (wild and key.lower() in k.lower()): - yield v + if output == 'dict': + yield k, v + else: + yield v elif isinstance(v, dict): - for result in _nested_lookup(key, v, wild=wild): + for result in _nested_lookup(key, v, wild=wild, output=output): yield result elif isinstance(v, list): for d in v: - for result in _nested_lookup(key, d, wild=wild): + for result in _nested_lookup(key, d, wild=wild, output=output): yield result -