Dict wrapper for accessing, updating, and deleting values in deeply nested dictionaries and lists using dot-notation paths with optional array indexing.
Project description
dict-traverser
Simple wrapper to work with deeply nested Python dictionaries and lists using dot-notation paths.
It supports safe lookups, nested updates, automatic structure creation, array indexing, and key deletion—ideal for working with JSON-like data structures.
Installation
pip install dict-traverser
Usage
DictTraverser
from dict_traverser import DictTraverser
data = {
'user': {
'profile': {
'name': 'Alice',
'emails': ['first@example.com', 'second@example.com'],
},
},
}
tv = DictTraverser(data)
# Get values
print(tv['user.profile.name']) # 'Alice'
print(tv.get('user.profile.age', 30)) # 30 (default)
print(tv['user.profile.emails.[0]']) # first@example.com
# Set values (automatically creates the necessary structures)
tv['user.profile.age'] = 25
tv['user.profile.emails.[2]'] = 'third@example.com'
# Delete values
del tv['user.profile.name']
# Update with another dict
tv.update({
'user': {'profile': {'country': 'US'}},
})
# Access the underlying dictionary
print(tv.obj)
# Resulting dictionary after the updates above
{
'user': {
'profile': {
# Name got deleted
'emails': [
'first@example.com',
'second@example.com',
'third@example.com', # Added to the array
],
'age': 25, # Added by setting the key
'country': 'US', # Added by update() call
},
},
}
DictTraverser with custom separator
If the keys in your data include dots (.), you can provide a custom separator to DictTraverser:
from dict_traverser import DictTraverser
data = {
'user': {
'settings': {
'theme': 'dark',
'device.type': 'mobile',
},
},
}
tv = DictTraverser(data, '.') # '.' is the default
try:
tv['user.settings.device.type']
except KeyError:
# Will raise KeyError because it tried to access a
# dictionary called `api` inside `settings`
pass
tv = DictTraverser(data, '+') # Now use + signs for separators
print(tv['user+settings+theme']) # dark
tv.separator = '|' # You can also change the separator after creation
print(tv['user|settings|device.type']) # mobile
unwind
Utility function to convert a nested dictionary to a one-level-deep dictionary with dot-notation keys
from dict_traverser import unwind
data = {
'user': {
'profile': {
'name': 'Alice',
'emails': ['first@example.com', 'second@example.com'],
},
},
}
print(unwind(data))
{
'user.profile.name': 'Alice',
'user.profile.emails': ['first@example.com', 'second@example.com'],
}
print(unwind(data, separator='|'))
{
'user|profile|name': 'Alice',
'user|profile|emails': ['first@example.com', 'second@example.com'],
}
print(unwind(data, initial_key='prefix'))
{
'prefix.user.profile.name': 'Alice',
'prefix.user.profile.emails': ['first@example.com', 'second@example.com'],
}
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dict_traverser-1.0.0.tar.gz.
File metadata
- Download URL: dict_traverser-1.0.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0caea20eb00acf514f80c6d7ea3de14c6d958ab934c6637254de61189e6d2ab8
|
|
| MD5 |
fea8c3f29f1e2ee4ab07c4e1d973e891
|
|
| BLAKE2b-256 |
c9c63f7db2d7941379f4108657c8943341e7021a8990728ebdcca7f53abe3fe8
|
Provenance
The following attestation bundles were made for dict_traverser-1.0.0.tar.gz:
Publisher:
test-and-publish.yaml on cvenencia/dict-traverser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dict_traverser-1.0.0.tar.gz -
Subject digest:
0caea20eb00acf514f80c6d7ea3de14c6d958ab934c6637254de61189e6d2ab8 - Sigstore transparency entry: 496899465
- Sigstore integration time:
-
Permalink:
cvenencia/dict-traverser@b79dc5024e592630499da19d16b4c9aaf8065278 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/cvenencia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
test-and-publish.yaml@b79dc5024e592630499da19d16b4c9aaf8065278 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dict_traverser-1.0.0-py3-none-any.whl.
File metadata
- Download URL: dict_traverser-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8795181f36e0b578106763f007998e7514c1db1d68853d9d8f0615c6934e205e
|
|
| MD5 |
03c2ea8abed17a7b10be4ca19f795f53
|
|
| BLAKE2b-256 |
d8ccf4410e4e13c599405cdf2a63ba396e631f5146af5f91dbc650b2c4fb1a9a
|
Provenance
The following attestation bundles were made for dict_traverser-1.0.0-py3-none-any.whl:
Publisher:
test-and-publish.yaml on cvenencia/dict-traverser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dict_traverser-1.0.0-py3-none-any.whl -
Subject digest:
8795181f36e0b578106763f007998e7514c1db1d68853d9d8f0615c6934e205e - Sigstore transparency entry: 496899484
- Sigstore integration time:
-
Permalink:
cvenencia/dict-traverser@b79dc5024e592630499da19d16b4c9aaf8065278 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/cvenencia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
test-and-publish.yaml@b79dc5024e592630499da19d16b4c9aaf8065278 -
Trigger Event:
release
-
Statement type: