DictPath

0.5.0 · active · verified Wed Apr 15

DictPath provides an object-oriented way to navigate and manipulate nested dictionaries using path-like expressions. It allows for robust and type-safe access to dictionary values, supporting various path syntaxes and advanced traversal features. The current stable version is 0.5.0, with a release cadence that has recently seen significant updates focused on API enhancements and performance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating an AccessorPath object and using it to read values from a nested dictionary. It covers basic dot notation, accessing list elements, and using the new subscriptable accessor syntax introduced in 0.5.0. It also shows how to provide a default value for missing paths.

from dictpath import AccessorPath

data = {
    'user': {
        'profile': {
            'name': 'Alice',
            'age': 30,
            'contact': [
                {'type': 'email', 'value': 'alice@example.com'},
                {'type': 'phone', 'value': '123-456-7890'}
            ]
        }
    }
}

# Create a path accessor
path = AccessorPath('user.profile.name')

# Read a value
name = path.read_value(data)
print(f"User name: {name}")

# Access a nested list item
email_path = AccessorPath('user.profile.contact.0.value')
email = email_path.read_value(data)
print(f"User email: {email}")

# Using subscriptable accessors (0.5.0+)
phone_path = AccessorPath('user')['profile']['contact'][1]['value']
phone = phone_path.read_value(data)
print(f"User phone: {phone}")

# Getting a default value if path doesn't exist
missing_path = AccessorPath('user.profile.address')
address = missing_path.read_value(data, default='No address provided')
print(f"User address: {address}")

view raw JSON →