Skip to main content

Object-oriented paths

Project description

pathable

Package version Python versions License

About

Pathable provides a small set of "path" objects for traversing hierarchical data (mappings, lists, and other subscriptable trees) using a familiar path-like syntax.

It’s especially handy when you want to:

  • express deep lookups as a single object (and pass it around)
  • build paths incrementally (p / "a" / 0 / "b")
  • safely probe (exists(), get(...)) or strictly require segments (//)

Key features

  • Intuitive path-based navigation for nested data (e.g., dicts/lists)
  • Pluggable accessor layer for custom backends
  • Pythonic, chainable API for concise and readable code
  • Per-instance (bounded LRU) cached lookup accessor for repeated reads of the same tree

Quickstart

from pathable import LookupPath

data = {
    "parts": {
        "part1": {"name": "Part One"},
        "part2": {"name": "Part Two"},
    }
}

root = LookupPath.from_lookup(data)

name = (root / "parts" / "part2" / "name").read_value()
assert name == "Part Two"

Usage

from pathable import LookupPath

data = {
    "parts": {
        "part1": {"name": "Part One"},
        "part2": {"name": "Part Two"},
    }
}

p = LookupPath.from_lookup(data)

# Concatenate path segments with /
parts = p / "parts"

# Check membership (mapping keys or list indexes)
assert "part2" in parts

# Read a value
assert (parts / "part2" / "name").read_value() == "Part Two"

# Iterate children as paths
for child in parts:
    print(child, child.read_value())

# Work with keys/items
print(list(parts.keys()))
print({k: v.read_value() for k, v in parts.items()})

# Safe access
print(parts.get("missing", default=None))

# Strict access (raises KeyError if missing)
must_exist = parts // "part2"

# "Open" yields the current value as a context manager
with parts.open() as parts_value:
    assert isinstance(parts_value, dict)

# Optional metadata
print(parts.stat())

Filesystem example

Pathable can also traverse the filesystem via an accessor.

from pathlib import Path

from pathable import FilesystemPath

root_dir = Path(".")
p = FilesystemPath.from_path(root_dir)

readme = p / "README.md"
if readme.exists():
    content = readme.read_value()  # bytes
    print(content[:100])

Core concepts

  • BasePath is a pure path (segments + separator) with / joining.
  • AccessorPath is a BasePath bound to a NodeAccessor, enabling read_value(), exists(), keys(), iteration, etc.
  • FilesystemPath is an AccessorPath specialized for filesystem objects.
  • LookupPath is an AccessorPath specialized for mapping/list lookups.

Notes on parsing:

  • A segment like "a/b" is split into parts using the separator.
  • None segments are ignored.
  • "." segments are ignored (relative no-op).
  • Operations like relative_to() and is_relative_to() also respect the instance separator.

Equality and ordering:

  • BasePath equality, hashing, and ordering are all based on both separator and parts.
  • Ordering is separator-sensitive and deterministic, even when parts mix types (e.g. ints and strings).
  • Path parts are type-sensitive (0 is not equal to "0").

Lookup caching:

  • LookupPath uses a per-instance LRU cache (default maxsize: 128) on its accessor.
  • You can control it via path.accessor.clear_cache(), path.accessor.disable_cache(), and path.accessor.enable_cache(maxsize=...).
  • path.accessor.node is immutable; to point at a different tree, create a new LookupPath/accessor.

Installation

Recommended way (via pip):

pip install pathable

Alternatively you can download the code and install from the repository:

pip install -e git+https://github.com/p1c2u/pathable.git#egg=pathable

Benchmarks

Benchmarks live in tests/benchmarks/ and produce JSON reports.

Local run (recommended as modules):

poetry run python -m tests.benchmarks.bench_parse --output reports/bench-parse.json
poetry run python -m tests.benchmarks.bench_lookup --output reports/bench-lookup.json

Quick sanity run:

poetry run python -m tests.benchmarks.bench_parse --quick --output reports/bench-parse.quick.json
poetry run python -m tests.benchmarks.bench_lookup --quick --output reports/bench-lookup.quick.json

Compare two results (fails if candidate is >20% slower in any scenario):

poetry run python -m tests.benchmarks.compare_results \
    --baseline reports/bench-before.json \
    --candidate reports/bench-after.json \
    --tolerance 0.20

CI (on-demand):

  • GitHub Actions workflow Benchmarks runs via workflow_dispatch and uploads the JSON artifacts.

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

pathable-0.5.0b4.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

pathable-0.5.0b4-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file pathable-0.5.0b4.tar.gz.

File metadata

  • Download URL: pathable-0.5.0b4.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pathable-0.5.0b4.tar.gz
Algorithm Hash digest
SHA256 4d3badb9312b45311f96b79f3e9690b83a69a22a89b64a3199fd86d4c98c28b4
MD5 04c7814dcc267f8ea2dad9faa856e5f0
BLAKE2b-256 f116a507800f10e9e4026c838e3f6b7915ab362f91fee8d76f79626b56ba3f51

See more details on using hashes here.

Provenance

The following attestation bundles were made for pathable-0.5.0b4.tar.gz:

Publisher: python-publish.yml on p1c2u/pathable

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

File details

Details for the file pathable-0.5.0b4-py3-none-any.whl.

File metadata

  • Download URL: pathable-0.5.0b4-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pathable-0.5.0b4-py3-none-any.whl
Algorithm Hash digest
SHA256 571d7e9970f717235155376073746deadb9af49fdcdeba0c1a66c79b1b4d7f7f
MD5 4ea86964ce095c7e9a555cd8889ee9c5
BLAKE2b-256 7029b4722c5b7eab5533dd63635d1c2d21ef197694ed6aefc90af69b063d925b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pathable-0.5.0b4-py3-none-any.whl:

Publisher: python-publish.yml on p1c2u/pathable

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