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
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_set_nested_dict import get_set_nested_dict
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
Built Distribution
File details
Details for the file get_set_nested_dict-0.1.1.tar.gz
.
File metadata
- Download URL: get_set_nested_dict-0.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9bed98d677effde52f56e0db135e3518c59f8d88fa870304ea3ca73e2d80838 |
|
MD5 | b6b2d3f763dd300a031a8d9dc012eb67 |
|
BLAKE2b-256 | 23b8952565e31311756f4a0d1e21f5c4423eb07cc6e75e847c79200c481c1acd |
File details
Details for the file get_set_nested_dict-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: get_set_nested_dict-0.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b61fef6a6a3746f9de3a9a0babf0f150bced0f32ed8664e83931fd3b4dfe8c38 |
|
MD5 | 0188fad1c10a1015d03bca9f696a3f9b |
|
BLAKE2b-256 | 3aa0ac0222e6e4a28e9d2a4b1461aa16fed1546604dd43bdd0be213ede88a4fb |