Skip to main content

Nested Dict Update With operation like insert , deleter , updater ,search

Project description

Dict Updater

It used to update any nested dict and list object . Also provide the option to update nested dictionary within the list by searching mechanism .

Feature Offer :

  • Update the dictionary object .
  • Delete nested dictionary value .
  • Append nested list object .

How to Install the library

pip install dictupdate

Example

Updating simple dictionary object

from dictupdate import DictUpdater

# source to update 
source = {
    "a" : {
        "b": "value_need_to_update"
    }
}

# target to update
target = {
    "a":{
        "b": "existing value updated",
        "c": "new value added"
    }
}

dict_update = DictUpdater.update(
    data=source,
    update_value=target
)

print(dict_update)

Output :

{'a': {'b': 'existing value updated', 'c': 'new value added'}}

Updating Recursive dictionary with list object.

Updating dictionary with listing object which again contain dictionary object, Normal recursive update operation will fail to update these type value because it required extra search layer. To tackle this issue DictUpdater.update have extra parameter which help to identify key and update value .

operation mapping value structure

{  
< search path separated by seperator(->)::operation : search_key
 }

Let's understand what is search path Bellow given dictionary object which contain list .

source = {
    "a": [
        {
            "search": 1,
            "b": [
                {
                    "c": "required_to_update",
                    "value": "target"
                },
                {},
                {}
            ]
        },
        {}
    ]
}

There is 'target' value is present . If you look carefully there is path generated from root location . which is 'a->b', So technically search path in list contain value is a dictionary key path followed by seperator .

Let try to understand , what is search value ? . In a list of dictionary , which dictionary object we want to update . Means filter dictionary by search.

{ 
  a::operation : search,
  a->b:operation : c,
}

"search" and "c" are dictionary key which help to indentify dict objet . 

What kind of operation currently it supports . update : update the all search dictionary . update_append : update the all search dictionary ,if it failed then append the value in list. insert : it is default operation . append : it append the value in dictionary . delete : it delete the dict value in list.

Example to update recursive list dictionary object .

Update the only target value

from dictupdate import DictUpdater
from pprint import pprint as pp

source = {
    "a": [
        {
            "search": 1,
            "b": [
                {
                    "c": "required_to_update",
                    "value": "target"
                },
                {},
                {}
            ]
        },
        {}
    ]
}

# things which required to update must have exact structure .
update_value = {
    "a": [
        {
            "search": 1,
            "b": [
                {
                    "c": "required_to_update",
                    "value": "new_value"
                }
            ]
        }
    ]
}
print("-------------- Before Update --------------")
pp(source)

new_value = DictUpdater.update(data=source, update_value=update_value, operation_mapping={
    "a::update_append": "search",
    "a->b::update_append": "c"
}, data_muted=False)

print("-------------- After Update --------------")
pp(new_value)

Output :

-------------- Before Update --------------
{'a': [{'b': [{'c': 'required_to_update', 'value': 'target'}, {}, {}],
        'search': 1},
       {}]}
-------------- After Update --------------
{'a': [{'b': [{'c': 'required_to_update', 'value': 'new_value'}, {}, {}],
        'search': 1},
       {}]}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dictupdate-0.0.4.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

dictupdate-0.0.4-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page