Skip to main content

Get or Set a nested value of nested dict by path

Project description

get_set_nested_dict

Get or Set a nested value of nested dict by path

def get_set_nested_dict(nested_dict, path, **kwargs):
"""
Get or Set a nested value of nested dict by path
:param nested_dict: nested dict object
    {
      "club": [
        {
          "manager": {
            "last_name": "Lionel",
            "first_name": "Messi"
          }
        }
      ]
    }
:param path: path to access the nested dict value
    "club[0].manager.first_name"
:param kwargs: {setter_value: value}
    setter_value='Pulga'
:return: <modified_nested_dict>, getter_value
    ({'club': [{'manager': {'last_name': 'Pulga', 'first_name': 'Messi'}}]}, 'Lionel')
"""

#####Installation: https://pypi.org/project/get_set_nested_dict/

pip install get_set_nested_dict

#####Usage:

nested_dict = {
  "key": [
    {
      "sub_key": {
        "sub_sub_key_1": "Value_1",
        "sub_sub_key_2": "Value_2"
      }
    }
  ]
}

before:

sub_sub_value_1 = nested_dict['key'][0]['sub_key']['sub_sub_key_1']

after:

from get_set_nested_dict import get_set_nested_dict
sub_sub_value_1 = get_set_nested_dict(nested_dict, "key[0].sub_key.sub_sub_key_1")

#####Example:

nested_dict = {
  "club": [
    {
      "manager": {
        "last_name": "Lionel",
        "first_name": "Messi"
      }
    }
  ]
}

before:

manager_last_name = nested_dict['club'][0]['manager']['last_name']
nested_dict['club'][0]['manager']['last_name'] = 'Pulga'

after:

from get_nested_value_by_path import get_nested_value_by_path
path = "club[0].manager.last_name"
nested_dict, manager_last_name = get_set_nested_dict(nested_dict, path, setter_value='Pulga')

>>> nested_dict
{'club': [{'manager': {'last_name': 'Pulga', 'first_name': 'Messi'}}]}
>>> manager_last_name
'Lionel'

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

get_set_nested_dict-0.0.1.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

get_set_nested_dict-0.0.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file get_set_nested_dict-0.0.1.tar.gz.

File metadata

  • Download URL: get_set_nested_dict-0.0.1.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9

File hashes

Hashes for get_set_nested_dict-0.0.1.tar.gz
Algorithm Hash digest
SHA256 57a2c6ddad17fb3318ee43a069495884d10c9cd6aaaf4ea55744e953f37474e2
MD5 096b89d8ea878c348cdb58400f332978
BLAKE2b-256 7dcd40e92bc73b2426af0735bd9d89e3286149bb11f4c21208feeae534b96fd5

See more details on using hashes here.

File details

Details for the file get_set_nested_dict-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: get_set_nested_dict-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9

File hashes

Hashes for get_set_nested_dict-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6ec56861287324b547c2c68813f43860afb53a5af510472b4fbcdf6f402b020
MD5 c85acaea2f25d4c2b2c5d9db14d8bdd0
BLAKE2b-256 e6bdee59cccbe41bb38e815451da5e072d6269b64f31215586af784d8dc04a9e

See more details on using hashes here.

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