"nested_alter" and update of "nested_update" (#16)
* Update lookup_api.py moved the typecheck of the value argument to the wrapper function. * added tests for nested_update Test * pep8 format standards moded _call_callback out of the nested_alter Method Raised an Exception is no list is provided but treas_as_elemenet is false treat_list_as_element:bool renamed to treat_as_element:bool and set default to True * Auto commit via script * Create CONTRIBUTORS.rst * Rename CONTRIBUTORS.rst to CONTRIBUTING.rst * Update CONTRIBUTING.rst * Fix for issue #17 (#18) * Fix for issue #17 * Updating version (and) Adding Travis CI support * Adding Build status to README * changed the type annotation of the input document (removed it) removed the output type hint change the Readme * Update lookup_api.py moved the typecheck of the value argument to the wrapper function. * added tests for nested_update Test * pep8 format standards moded _call_callback out of the nested_alter Method Raised an Exception is no list is provided but treas_as_elemenet is false treat_list_as_element:bool renamed to treat_as_element:bool and set default to True * changed the type annotation of the input document (removed it) removed the output type hint change the Readme
This commit is contained in:
parent
0e8723b822
commit
4f37d265c8
5 changed files with 670 additions and 91 deletions
32
README.rst
32
README.rst
|
|
@ -24,6 +24,11 @@ A document in this case is a a mixture of Python dictionary and list objects typ
|
|||
Given a document, find all occurrences of the given key and delete it.
|
||||
By default, returns a copy of the document.
|
||||
To mutate the original specify the `in_place=True` argument.
|
||||
|
||||
*nested_alter:*
|
||||
Given a document, find all occurrences of the given key and alter it with a callback function
|
||||
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.
|
||||
|
|
@ -75,6 +80,31 @@ quick tutorial
|
|||
>>> nested_delete(document, 'taco')
|
||||
[{}, {'salsa': [{'burrito': {}}]}]
|
||||
|
||||
|
||||
|
||||
*Nested Alter*:
|
||||
write a callback function which processes a scalar value.
|
||||
Be aware about the possible types which can be passed to the callback functions.
|
||||
In this example we can be sure that only int will be passed, in production you should check the type because it could be anything.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> def callback(data):
|
||||
>>> return data + 10 # add 10 to every taco prize
|
||||
|
||||
The alter-version only works for scalar input (one dict), if you need to adress a list of dicts, you have to
|
||||
manually iterate over those and pass them to nested_update one by one
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> out =[]
|
||||
>>> for elem in document:
|
||||
>>> altered_document = nested_alter(elem,"taco", callback)
|
||||
>>> out.append(altered_document)
|
||||
|
||||
>>> print(out)
|
||||
[ { 'taco' : 52 } , { 'salsa' : [ { 'burrito' : { 'taco' : 79 } } ] } ]
|
||||
|
||||
>>> from nested_lookup import get_all_keys
|
||||
|
||||
>>> get_all_keys(document)
|
||||
|
|
@ -210,8 +240,10 @@ misc
|
|||
* Russell Ballestrini
|
||||
* Douglas Miranda
|
||||
* Ramesh RV
|
||||
* Salfiii (Florian S.)
|
||||
|
||||
:web:
|
||||
* http://russell.ballestrini.net
|
||||
* http://douglasmiranda.com
|
||||
* https://gist.github.com/douglasmiranda/5127251
|
||||
* https://github.com/Salfiii
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue