diff --git a/.gitignore b/.gitignore index 0cb92e2..7ebb9bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ __pycache__* +.pytest_cache* *.pyc .coverage build/* diff --git a/README.rst b/README.rst index ac112e5..903ac7f 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ nested_lookup ############# -.. image:: https://img.shields.io/badge/pypi-0.2.11-green.svg +.. image:: https://img.shields.io/badge/pypi-0.2.12-green.svg :target: https://pypi.python.org/pypi/nested-lookup Make working with JSON, YAML, and XML document responses fun again! @@ -14,12 +14,14 @@ A document in this case is a a mixture of Python dictionary and list objects typ Returns a `list` of matching values. *nested_update:* - Given a document, find all occurences of the given key and update the value - By default, returns a copy of the document. To mutate the original one instead - please specify the `in_place=True` argument. + Given a document, find all occurences of the given key and update the value. + By default, returns a copy of the document. + To mutate the original specify the `in_place=True` argument. *nested_delete:* Given a document, find all occurrences of the given key and delete it. - Returns a copy of the document. + By default, returns a copy of the document. + To mutate the original specify the `in_place=True` argument. *get_all_keys:* Fetch all keys from a deeply nested dictionary. diff --git a/nested_lookup/lookup_api.py b/nested_lookup/lookup_api.py index 221ef31..e338ae3 100644 --- a/nested_lookup/lookup_api.py +++ b/nested_lookup/lookup_api.py @@ -2,9 +2,10 @@ import copy from six import iteritems -def nested_delete(document, key): - duplicate = copy.deepcopy(document) - return _nested_delete(document=duplicate, key=key) +def nested_delete(document, key, in_place=False): + if not in_place: + document = copy.deepcopy(document) + return _nested_delete(document=document, key=key) def _nested_delete(document, key): diff --git a/setup.py b/setup.py index ef20e20..e5d89bd 100644 --- a/setup.py +++ b/setup.py @@ -20,8 +20,8 @@ with open('requirements.txt', 'r') as f: setup( name='nested-lookup', - version='0.2.11', - description='lookup a key in a deeply nested document of dicts and lists', + version='0.2.12', + 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(), @@ -40,13 +40,8 @@ setup( classifiers=[ # Specify the Python versions you support here. In particular, ensure # that you indicate whether you support Python 2, Python 3 or both. - 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.2', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', diff --git a/test_lookup_api.py b/test_lookup_api.py index f7d6a60..b2f8b61 100644 --- a/test_lookup_api.py +++ b/test_lookup_api.py @@ -103,18 +103,37 @@ class TestNestedUpdate(BaseLookUpApi): result, nested_update(self.sample_data1, 'build_version', 'Test1') ) - def test_in_place(self): - result = { - "build_version": "Test1", - "os_details": { - "product_version": '10.13.6', - "build_version": 'Test1' - }, - "name": 'Test', - "date": 'YYYY-MM-DD HH:MM:SS' - } - nested_update(self.sample_data1, 'build_version', 'Test1', in_place=True) - self.assertEqual(result, self.sample_data1) + def test_nested_update_in_place_false(self): + """nested_update should mutate and return a copy of the original document""" + before_id = id(self.sample_data1) + result = nested_update(self.sample_data1, 'build_version', 'Test2', in_place=False) + after_id = id(result) + # the object ids should _not_ match. + self.assertNotEqual(before_id, after_id) + + def test_nested_update_in_place_true(self): + """nested_update should mutate and return the original document""" + before_id = id(self.sample_data1) + result = nested_update(self.sample_data1, 'build_version', 'Test2', in_place=True) + after_id = id(result) + # the object ids should match. + self.assertEqual(before_id, after_id) + + def test_nested_delete_in_place_false(self): + """nested_delete should mutate and return a copy of the original document""" + before_id = id(self.sample_data1) + result = nested_delete(self.sample_data1, 'build_version', in_place=False) + after_id = id(result) + # the object ids should _not_ match. + self.assertNotEqual(before_id, after_id) + + def test_nested_delete_in_place_true(self): + """nested_delete should mutate and return the original document""" + before_id = id(self.sample_data1) + result = nested_delete(self.sample_data1, 'build_version', in_place=True) + after_id = id(result) + # the object ids should match. + self.assertEqual(before_id, after_id) def test_sample_data2(self): result = {