From dfc2483e3ea5c13863b9593f84a3d293de829c9f Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 24 Apr 2018 09:47:47 -0700 Subject: [PATCH 1/5] added `dict` as a possible output when the user wants to keep some structure in the result, the `ouptut` option allows to return a `dict` with the matched keys as keys and corresponding values as values. --- nested_lookup/nested_lookup.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index d5a7226..6e2f368 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -1,10 +1,15 @@ from six import iteritems -def nested_lookup(key, document, wild=False): +def nested_lookup(key, document, wild=False, output='dict'): """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)) + elif output == 'list': + return list(_nested_lookup(key, document, wild=wild, output=output)) + else: + raise ValueError('Output is of unknown type '+output) -def _nested_lookup(key, document, wild=False): +def _nested_lookup(key, document, wild=False, output='dict'): """Lookup a key in a nested document, yield a value""" if isinstance(document, list): for d in document: @@ -14,12 +19,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 + elif output == 'list': + 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 - From 3b1f2ced67032614d6dfd8f4b8a83043fd89328a Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 24 Apr 2018 10:30:29 -0700 Subject: [PATCH 2/5] Updated README.rst and set `list` as default output --- README.rst | 90 ++++++++++++++++++++++++---------- nested_lookup/nested_lookup.py | 10 ++-- 2 files changed, 67 insertions(+), 33 deletions(-) diff --git a/README.rst b/README.rst index cd427ec..3ecd8e0 100644 --- a/README.rst +++ b/README.rst @@ -43,49 +43,85 @@ tutorial [42, 69] -wild -======== + wild + ======== -We also have a `wild` mode that treats the given `key` as a case insensitive -substring of all the keys in the document and returns any values which match. + We also have a `wild` mode that treats the given `key` as a case insensitive + substring of all the keys in the document and returns any values which match. -For example: + For example: -.. code-block:: python + .. code-block:: python - from nested_lookup import nested_lookup + 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', - }, - }, + 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, - ) + 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 + + 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, + output = 'dict' + ) + + print(results) + {'email_address': 'test1@example.com', + 'secondary_email': 'test2@example.com', + 'EMAIL_RECOVERY': 'test3@example.com'} - print(results) - ['test1@example.com', 'test2@example.com', '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 6e2f368..645b657 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -1,15 +1,13 @@ from six import iteritems -def nested_lookup(key, document, wild=False, output='dict'): +def nested_lookup(key, document, wild=False, output='list'): """Lookup a key in a nested document, return a list of values""" if output == 'dict': return dict(_nested_lookup(key, document, wild=wild, output=output)) - elif output == 'list': - return list(_nested_lookup(key, document, wild=wild, output=output)) else: - raise ValueError('Output is of unknown type '+output) + return list(_nested_lookup(key, document, wild=wild, output=output)) -def _nested_lookup(key, document, wild=False, output='dict'): +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: @@ -21,7 +19,7 @@ def _nested_lookup(key, document, wild=False, output='dict'): if key == k or (wild and key.lower() in k.lower()): if output == 'dict': yield k, v - elif output == 'list': + else: yield v elif isinstance(v, dict): for result in _nested_lookup(key, v, wild=wild, output=output): From ef45cadb160f9fd52461a31236ed19e5d1a631b3 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 24 Apr 2018 10:34:41 -0700 Subject: [PATCH 3/5] Corrected formatting of README.rst --- README.rst | 100 ++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/README.rst b/README.rst index 3ecd8e0..9915bb8 100644 --- a/README.rst +++ b/README.rst @@ -43,72 +43,72 @@ tutorial [42, 69] - wild - ======== +wild +======== - We also have a `wild` mode that treats the given `key` as a case insensitive - substring of all the keys in the document and returns any values which match. +We also have a `wild` mode that treats the given `key` as a case insensitive +substring of all the keys in the document and returns any values which match. - For example: +For example: - .. code-block:: python +.. code-block:: python - from nested_lookup import nested_lookup +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', - }, - }, +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 - ) +results = nested_lookup( + key = 'mail', + document = my_document, + wild = True +) - print(results) - ['test1@example.com', 'test2@example.com', 'test3@example.com'] +print(results) +['test1@example.com', 'test2@example.com', 'test3@example.com'] - output - ======== +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. +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: +For example: - .. code-block:: python +.. code-block:: python - from nested_lookup import nested_lookup + 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', - }, - }, + 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, - output = 'dict' - ) + results = nested_lookup( + key = 'mail', + document = my_document, + wild = True, + output = 'dict' + ) - print(results) - {'email_address': 'test1@example.com', - 'secondary_email': 'test2@example.com', - 'EMAIL_RECOVERY': 'test3@example.com'} + print(results) + {'email_address': 'test1@example.com', + 'secondary_email': 'test2@example.com', + 'EMAIL_RECOVERY': 'test3@example.com'} misc From ffa9dc454f6b30f79f69946e6ec98e3ad5a15320 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 24 Apr 2018 10:36:44 -0700 Subject: [PATCH 4/5] Another attempt at correcting formatting of README.rst --- README.rst | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index 9915bb8..7e71e13 100644 --- a/README.rst +++ b/README.rst @@ -53,25 +53,25 @@ For example: .. code-block:: python -from nested_lookup import nested_lookup + from nested_lookup import nested_lookup -my_document = { + 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 -) + results = nested_lookup( + key = 'mail', + document = my_document, + wild = True + ) -print(results) -['test1@example.com', 'test2@example.com', 'test3@example.com'] + print(results) + ['test1@example.com', 'test2@example.com', 'test3@example.com'] output From ebd2e8c1aaee6c36412c39b962848d69cd4af066 Mon Sep 17 00:00:00 2001 From: Guillaume David Date: Tue, 24 Apr 2018 11:16:08 -0700 Subject: [PATCH 5/5] Corrected formatting of `README.rst` --- README.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 7e71e13..10c4c44 100644 --- a/README.rst +++ b/README.rst @@ -78,10 +78,9 @@ 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. + +* `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: