Skip to main content

dotpath

A tiny utility to read values from nested dicts/lists using dot-separated paths.

Install

pip install dotpath

Example

from dotpath import get_path, set_path

data = {
    'pets': {
        'dog': {
            'name': 'Billy',
            'age': 5,
            'friends': ['Joe', 'Jack'],
        },
        'cat': {
            'name': 'Joe',
            'age': 6,
            'friends': ['Billy', 'Pop'],
        },
    },
}

dog_name = get_path(data, 'pets.dog.name')  # "Billy"
cat_name = get_path(data, 'pets.cat.name')  # "Joe"
first_friend = get_path(data, 'pets.dog.friends.0')  # "Joe"

set_path(data, 'pets.dog.age', 6) # Modifies the object in place
set_path(data, 'pets.bird.name', 'Kiwi') # Creates a new key
print(data) # {"pets": {"dog": {"name": "Billy", "age": 6, "friends": ["Joe", "Jack"]}, "cat": {"name": "Joe", "age": 6, "friends": ["Billy", "Pop"]}, "bird": {"name": "Kiwi"}}}

API

  • get_path(obj, path, default=None): Returns the value at path or default if the path cannot be resolved.
  • set_path(obj, path, value, default=None): Sets value at path and returns the mutated object or default if it fails.

Notes

  • Path segments are split by ..
  • List indexes are supported via numeric segments (e.g. friends.0).

Metadata

Release files for dotpath 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dotpath 1.0.0
File Size Uploaded
dotpath-1.0.0.tar.gz 58.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dotpath 1.0.0
File Interpreter ABI Platform
dotpath-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 62.7 kB

Release files / dotpath-1.0.0.tar.gz

Download URL dotpath-1.0.0.tar.gz
Size 58.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f1df4689845ff4bef7e485d51ce865d43fe8cde19a21ab01125f0e7a8d2724e0
BLAKE2b-256 checksum
How to use checksums
cc550935c514885a14e020d597e2141f79a8e630a63d9d51ecbd425e4dec39ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release files / dotpath-1.0.0-py3-none-any.whl

Download URL dotpath-1.0.0-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
770c0cd8189bb9eb481d89ce601609087241dac0f725b356de633fe8f06efbf2
BLAKE2b-256 checksum
How to use checksums
3a2172afa3d697f62aef2e0b51fab82316cc72d1767058a124972ca3d4f1b99f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page