Skip to main content

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


Download files

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

Source Distribution

dict_traverser-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

dict_traverser-1.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

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

Hashes for dict_traverser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0caea20eb00acf514f80c6d7ea3de14c6d958ab934c6637254de61189e6d2ab8
MD5 fea8c3f29f1e2ee4ab07c4e1d973e891
BLAKE2b-256 c9c63f7db2d7941379f4108657c8943341e7021a8990728ebdcca7f53abe3fe8

See more details on using hashes here.

Provenance

The following attestation bundles were made for dict_traverser-1.0.0.tar.gz:

Publisher: test-and-publish.yaml on cvenencia/dict-traverser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for dict_traverser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8795181f36e0b578106763f007998e7514c1db1d68853d9d8f0615c6934e205e
MD5 03c2ea8abed17a7b10be4ca19f795f53
BLAKE2b-256 d8ccf4410e4e13c599405cdf2a63ba396e631f5146af5f91dbc650b2c4fb1a9a

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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