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
print(tv['user.profile.emails.[-1]']) # second@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.1.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.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file dict_traverser-1.0.1.tar.gz.

File metadata

  • Download URL: dict_traverser-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 fa937c16e929168f32defb8526a1a798ead739426b3a826d987965d7d60cbe9c
MD5 aa1b1e6841f119313f5502cb3850da46
BLAKE2b-256 6704d3d2cd25083bbba32662b1e7fff63faf068160241f4c43ab4973d156049e

See more details on using hashes here.

Provenance

The following attestation bundles were made for dict_traverser-1.0.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: dict_traverser-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a834fda7b6067a08e2d7e770289930f89f9db32e8d8c206c15a5feb901b7fa6
MD5 3f3149d8041e8373f12c68de2db38289
BLAKE2b-256 69e792836b1b0774659751bf7e5aab8756b6c4be6d9bbd2700244c6072ee2687

See more details on using hashes here.

Provenance

The following attestation bundles were made for dict_traverser-1.0.1-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