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.
Common errors
-
ModuleNotFoundError: No module named 'pathable'
cause The 'pathable' library is not installed in your Python environment or is not accessible on the Python path.fixInstall the library using pip: `pip install pathable` -
KeyError: '<missing_key>'
cause You are attempting to access a path segment that does not exist in the underlying data structure using strict access (e.g., the `//` operator or `.read_value()` on a non-existent path).fixUse safe access methods like `.get('<key>', default=None)` to provide a default value if the key is missing, or check for existence with `.exists()` before strict access. For example, change `p // 'a' // 'c'` to `(p / 'a' / 'c').get(default=None)`. -
AttributeError: 'LookupPath' object has no attribute '<deprecated_method>'
cause You are trying to use a method that has been deprecated or removed in `pathable` version 0.5.0 (e.g., `iter`, `iteritems`, `content`, `get`, `getkey`).fixMigrate to the newer, supported API. For example, instead of `p.get('key')`, use `(p / 'key').read_value()` or direct subscriptable access `p['key']` (if the path is already resolved to the appropriate level), or `p.read_value()` after building the path. For iteration, use standard Python iteration directly on the path object, e.g., `for child in p:`.
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.
- breaking The static method `LookupPath.from_lookup` has been removed or renamed.
Install
-
pip install pathable
Imports
- LookupPath
from pathable import LookupPath
- FilesystemPath
from pathable import FilesystemPath
- NodeAccessor
from pathable import BaseAccessor
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