Pathable: Object-Oriented Paths
Pathable is a Python library that provides object-oriented paths for traversing hierarchical data structures like dictionaries and lists, as well as file systems. It offers an intuitive, chainable API for deep lookups, incremental path building, and safe probing of data. The library is actively maintained with frequent releases, with version 0.5.0 being the latest stable release.
Warnings
- breaking `BaseAccessor` was replaced by `NodeAccessor`. Code directly using `BaseAccessor` will fail.
- breaking `AccessorPath` became generic. This change impacts type hints and potentially runtime behavior where type arguments for `AccessorPath` were not previously expected or provided.
- gotcha The `pyrsistent` dependency was removed in version 0.5.0b3. While an internal optimization for `pathable`, if your project implicitly relied on `pyrsistent` being installed as a transitive dependency of `pathable`, it will no longer be available automatically.
Install
-
pip install pathable
Imports
- LookupPath
from pathable import LookupPath
- FilesystemPath
from pathable import FilesystemPath
- NodeAccessor
from pathable import NodeAccessor
Quickstart
from pathable import LookupPath
data = {
"parts": {
"part1": {"name": "Part One"},
"part2": {"name": "Part Two"}
}
}
root = LookupPath.from_lookup(data)
# Navigate using slash operator
name_path = root / "parts" / "part2" / "name"
name = name_path.read_value()
print(f"Value found: {name}")
assert name == "Part Two"
# Check existence
assert (root / "parts" / "part1").exists()
assert not (root / "non_existent").exists()
# Safe access with .get()
part3_name = (root / "parts").get("part3", default=None)
print(f"Value with .get(): {part3_name}")
assert part3_name is None