Add nested_update.in_place argument (#13)
* Add nested_update.in_place argument * Update README.rst and tests
This commit is contained in:
parent
8baf61f7e6
commit
a924829821
3 changed files with 21 additions and 7 deletions
|
|
@ -14,11 +14,11 @@ A document in this case is a a mixture of Python dictionary and list objects typ
|
||||||
Returns a `list` of matching values.
|
Returns a `list` of matching values.
|
||||||
|
|
||||||
*nested_update:*
|
*nested_update:*
|
||||||
Given a document, find all occurances of the given key and update the value.
|
Given a document, find all occurences of the given key and update the value
|
||||||
Returns a copy of the document.
|
By default, returns a copy of the document. To mutate the original one instead - please specify the `in_place=True` argument.
|
||||||
|
|
||||||
*nested_delete:*
|
*nested_delete:*
|
||||||
Given a document, find all occurances of the given key and delete it.
|
Given a document, find all occurrences of the given key and delete it.
|
||||||
Returns a copy of the document.
|
Returns a copy of the document.
|
||||||
|
|
||||||
*get_all_keys:*
|
*get_all_keys:*
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,10 @@ def _nested_delete(document, key):
|
||||||
return document
|
return document
|
||||||
|
|
||||||
|
|
||||||
def nested_update(document, key, value):
|
def nested_update(document, key, value, in_place=False):
|
||||||
duplicate = copy.deepcopy(document)
|
if not in_place:
|
||||||
return _nested_update(document=duplicate, key=key, value=value)
|
document = copy.deepcopy(document)
|
||||||
|
return _nested_update(document=document, key=key, value=value)
|
||||||
|
|
||||||
|
|
||||||
def _nested_update(document, key, value):
|
def _nested_update(document, key, value):
|
||||||
|
|
|
||||||
|
|
@ -103,6 +103,19 @@ class TestNestedUpdate(BaseLookUpApi):
|
||||||
result, nested_update(self.sample_data1, 'build_version', 'Test1')
|
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_sample_data2(self):
|
def test_sample_data2(self):
|
||||||
result = {
|
result = {
|
||||||
"hardware_details": {
|
"hardware_details": {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue