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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file dictupdate-0.0.4.tar.gz.

File metadata

  • Download URL: dictupdate-0.0.4.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.9

File hashes

Hashes for dictupdate-0.0.4.tar.gz
Algorithm Hash digest
SHA256 704e1f5db3336cb29c20a3eb8e75f2cf8639c2ec66c3e1fb17145901487cf77b
MD5 abdc917d6e96443397bd24dc23cfbd75
BLAKE2b-256 b336b0372fa8c730333c16b2252139daca9c2624e60e4fe05d46ccb9b8df8f09

See more details on using hashes here.

File details

Details for the file dictupdate-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dictupdate-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.9

File hashes

Hashes for dictupdate-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a8f18f9605a6612e61138fca65cb463b92aef3edbb92c70aa608a4a6864d461d
MD5 24df7855d08b1a97a8132393da896787
BLAKE2b-256 54fb5af37f064f3d275564f3a785ada6b875a22329c00e38bb00d53800d67c3e

See more details on using hashes here.

Supported by

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