Object-oriented paths
Project description
pathable
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
BasePathis a pure path (segments + separator) with/joining.AccessorPathis aBasePathbound to aNodeAccessor, enablingread_value(),exists(),keys(), iteration, etc.FilesystemPathis anAccessorPathspecialized for filesystem objects.LookupPathis anAccessorPathspecialized for mapping/list lookups.
Notes on parsing:
- A segment like
"a/b"is split into parts using the separator. Nonesegments are ignored."."segments are ignored (relative no-op).- Operations like
relative_to()andis_relative_to()also respect the instance separator.
Equality and ordering:
BasePathequality, hashing, and ordering are all based on bothseparatorandparts.- Ordering is separator-sensitive and deterministic, even when parts mix types (e.g. ints and strings).
- Path parts are type-sensitive (
0is not equal to"0").
Lookup caching:
LookupPathuses a per-instance LRU cache (default maxsize: 128) on its accessor.- You can control it via
path.accessor.clear_cache(),path.accessor.disable_cache(), andpath.accessor.enable_cache(maxsize=...). path.accessor.nodeis immutable; to point at a different tree, create a newLookupPath/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
Benchmarksruns viaworkflow_dispatchand uploads the JSON artifacts.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pathable-0.5.0b6.tar.gz.
File metadata
- Download URL: pathable-0.5.0b6.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a36763b534a213894ddbca01db5b869aca52779e89a193078304fb3706b94404
|
|
| MD5 |
f51ca02b3eaeb6fe1224fb6e38d56c15
|
|
| BLAKE2b-256 |
6ebb35058eae372ecf815b265504c9dbef4b2afcaa765fe59b9cafc4dc090814
|
Provenance
The following attestation bundles were made for pathable-0.5.0b6.tar.gz:
Publisher:
python-publish.yml on p1c2u/pathable
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pathable-0.5.0b6.tar.gz -
Subject digest:
a36763b534a213894ddbca01db5b869aca52779e89a193078304fb3706b94404 - Sigstore transparency entry: 953620076
- Sigstore integration time:
-
Permalink:
p1c2u/pathable@04d4826a8fa2cf8f5011cf652dd7349fe4e80e11 -
Branch / Tag:
refs/tags/0.5.0b6 - Owner: https://github.com/p1c2u
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@04d4826a8fa2cf8f5011cf652dd7349fe4e80e11 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pathable-0.5.0b6-py3-none-any.whl.
File metadata
- Download URL: pathable-0.5.0b6-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c66bafb0e93b790098e580a1c89e042a24148d6541098f18d24ae1fc77e1b335
|
|
| MD5 |
3ec866d67bd8b732ccd7773f4a53b86e
|
|
| BLAKE2b-256 |
4df5678521a801273320ebdfb2b62f4dd22d0246ef3ae79baa2064f0b866d462
|
Provenance
The following attestation bundles were made for pathable-0.5.0b6-py3-none-any.whl:
Publisher:
python-publish.yml on p1c2u/pathable
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pathable-0.5.0b6-py3-none-any.whl -
Subject digest:
c66bafb0e93b790098e580a1c89e042a24148d6541098f18d24ae1fc77e1b335 - Sigstore transparency entry: 953620077
- Sigstore integration time:
-
Permalink:
p1c2u/pathable@04d4826a8fa2cf8f5011cf652dd7349fe4e80e11 -
Branch / Tag:
refs/tags/0.5.0b6 - Owner: https://github.com/p1c2u
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@04d4826a8fa2cf8f5011cf652dd7349fe4e80e11 -
Trigger Event:
release
-
Statement type: