diff --git a/README.rst b/README.rst index 3211c0f..3fef861 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index f61d66e..698718a 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -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: diff --git a/setup.py b/setup.py index 9d53994..ea930e8 100644 --- a/setup.py +++ b/setup.py @@ -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/* diff --git a/test_nested_lookup.py b/test_nested_lookup.py index 7a8b8e1..32e9a57 100644 --- a/test_nested_lookup.py +++ b/test_nested_lookup.py @@ -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