Skip to main content

A module to access Python props using dotpath.

Project description

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).

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

dotpath-1.0.0.tar.gz (58.9 kB view details)

Uploaded Source

Built Distribution

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

dotpath-1.0.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file dotpath-1.0.0.tar.gz.

File metadata

  • Download URL: dotpath-1.0.0.tar.gz
  • Upload date:
  • Size: 58.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.6

File hashes

Hashes for dotpath-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f1df4689845ff4bef7e485d51ce865d43fe8cde19a21ab01125f0e7a8d2724e0
MD5 1e99864c41d3523a11da04e235de61ea
BLAKE2b-256 cc550935c514885a14e020d597e2141f79a8e630a63d9d51ecbd425e4dec39ac

See more details on using hashes here.

File details

Details for the file dotpath-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dotpath-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.6

File hashes

Hashes for dotpath-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 770c0cd8189bb9eb481d89ce601609087241dac0f725b356de633fe8f06efbf2
MD5 ad71b8d23c2954c2e826acbdb52642b1
BLAKE2b-256 3a2172afa3d697f62aef2e0b51fab82316cc72d1767058a124972ca3d4f1b99f

See more details on using hashes here.

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