Subscriptable Path
A subclass of Python's pathlib objects that allow subscripting/indexing.
I was recently working with some pathlib.Path stuff and found that I
wanted to get the nth folder in a deep path. Naively, I tried:
>>> from pathlib import Path
>>> a = Path("/foo/bar/baz/a/b/c/d.txt")
>>> a[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'PosixPath' object is not subscriptable
Harrumph.
This project aims to make such a thing possible. See Usage for details.
Installation
This is a pure python package with no dependencies. Installing is easy:
pip install subscriptable_path
Usage
Note: Initial versions modify
pathlib.PurePathdirectly, as those classes are a bit of a pain to subclass (because of tricks with__new__). This means that importing this module will affect every usage ofpathlib.PurePath. Future work will adjust this API so that you can usepathlib.PurePathandSubscriptablePathsimultaneously.The goal is to create a SubscriptablePath object that can be imported and replace the
pathlib.Pathobject or can be used alongside. Eg:from subscribtable_path import SubscriptablePath as Path # drop-in replacement from subscriptable_path import SubscriptablePath as SPath # use alongside
It's easiest to show usage with examples:
>>> from subscriptable_path import Path
# Instantiate a Path object, just like `pathlib.Path`:
>>> path = Path("/mnt/foo/bar/1/2/3")
# Get a component of the path:
>>> path[2]
"foo"
>>> path[0]
"/"
>>> path[-1]
"3"
# Slices are also supported for __getitem__
>>> path[1:3]
"mnt/foo"
# Check the path length:
>>> len(path)
7
# Adjust a particular component, modifying the object
>>> path[2] = "hello"
>>> path
Path("/mnt/hello/bar/1/2/3")
# Delete a part of the path, modifying the object
>>> del path[2]
>>> path
Path("/mnt/bar/1/2/3")
# loop through the items in the path
>>> for name in path:
... print(name)
...
"/"
"mnt"
"bar"
"1"
"2"
"3"
# Reverse the path:
# Note that if the path is absolute, the root is kept.
>>> reversed(path)
>>> path
Path("/3/2/1/bar/mnt")
>>> rel_path = Path("1/2/3")
>>> reversed(rel_path)
>>> rel_path
Path("3/2/1")
# And check if an item is in the path:
>>> "foo" in path
False
>>> "bar" in path
True
>>> "ba" in path
False
Development
- Clone the repo:
git clone https://github.com/dougthor42/subscriptable-path - Move into that dir:
cd subscriptable-path - Create a virtual environment:
python -m venv .venv - Activate it:
. .venv/bin/activate - Install python packages:
pip install -U pip setuptools wheelpip install -r requirements-dev.txtpip install -e .
- Run tests to verify:
pytest - Install pre-commit hooks:
pre-commit install - Ready to develop
Deployment
TODO: Move to fully automated deployment with GitHub Actions.
- Update the version in
pyproject.toml - Create a new git tag:
git tag v<version>. python -m build- pip install twine
- twine upload dist/*
Changelog
See CHANGELOG.md.
Metadata
Release files for subscriptable-path 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| subscriptable_path-0.1.0.tar.gz | 5.2 kB | Without provenance | Details for subscriptable_path-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| subscriptable_path-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for subscriptable_path-0.1.0-py3-none-any.whl |
Total release size: 11.0 kB
Release files / subscriptable_path-0.1.0.tar.gz
| Download URL | subscriptable_path-0.1.0.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
168619133844a07ee0ee46fc262fd9c77987f3b0839cecbf8326d35c9329489c
|
|
BLAKE2b-256 checksum How to use checksums |
ccb36186872c8c23b0f090b9b7156a2e54521b7380e7a7bc94907d282d6bbf77
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.10
|
Release files / subscriptable_path-0.1.0-py3-none-any.whl
| Download URL | subscriptable_path-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
95ba941671a4b4e83e090f4866d913bcf3fb8899be2487c4852ee2e8049dbdaa
|
|
BLAKE2b-256 checksum How to use checksums |
0b9a1a02c3d3963e9c5201457b411985fa40be425b2c999fc574372c7e56ed8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.10
|
Security for subscriptable-path 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.