Fix defect where wild would break if document had non string keys.

modified:   README.rst
	modified:   nested_lookup/nested_lookup.py
	modified:   setup.py
	modified:   test_nested_lookup.py
This commit is contained in:
Russell Ballestrini 2019-06-25 14:50:00 -07:00
parent 9348af1790
commit b6acaca980
4 changed files with 14 additions and 6 deletions

View file

@ -1,7 +1,7 @@
nested_lookup
#############
.. image:: https://img.shields.io/badge/pypi-0.2.15-green.svg
.. image:: https://img.shields.io/badge/pypi-0.2.16-green.svg
:target: https://pypi.python.org/pypi/nested-lookup
.. image:: https://travis-ci.org/rameshrvr/nested-lookup.svg?branch=master
:target: https://travis-ci.org/rameshrvr/nested-lookup

View file

@ -13,6 +13,11 @@ def nested_lookup(key, document, wild=False, with_keys=False):
return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
def _is_case_insensitive_substring(a, b):
"""return True if `a` is a case insensitive substring of `b`, else False"""
return isinstance(a, str) and isinstance(b, str) and a.lower() in b.lower()
def _nested_lookup(key, document, wild=False, with_keys=False):
"""Lookup a key in a nested document, yield a value"""
if isinstance(document, list):
@ -22,7 +27,7 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
if isinstance(document, dict):
for k, v in iteritems(document):
if key == k or (wild and key.lower() in k.lower()):
if key == k or (wild and _is_case_insensitive_substring(key, k)):
if with_keys:
yield k, v
else:

View file

@ -17,7 +17,7 @@ with open("requirements.txt", "r") as f:
setup(
name="nested-lookup",
version="0.2.15",
version="0.2.16",
description="Python functions for working with deeply nested documents (lists and dicts) ",
keywords="nested document dictionary dict list lookup schema json xml yaml",
long_description=open("README.rst").read(),
@ -45,6 +45,4 @@ setup(
# build package:
# pip install twine
# python setup.py sdist
# built and uploaded to pypi with this:
# python setup.py sdist bdist_egg register upload
# twine upload dist/*

View file

@ -38,6 +38,7 @@ class TestNestedLookup(TestCase):
"name": "Test",
"date": "YYYY-MM-DD HH:MM:SS",
}
self.subject_dict4 = {1: "a", 2: {"b": 44, "C": 55}, 3: "d", 4: "e"}
def test_nested_lookup(self):
results = nested_lookup("d", self.subject_dict)
@ -74,6 +75,10 @@ class TestNestedLookup(TestCase):
self.assertIn("test2@example.com", results)
self.assertIn("test3@example.com", results)
# test that wild works with a document that has integers as keys.
results = nested_lookup(key="c", document=self.subject_dict4, wild=True)
self.assertIn(55, results)
def test_wild_with_keys_nested_lookup(self):
matches = nested_lookup(
key="mail", document=self.subject_dict2, wild=True, with_keys=True