mutapath
This library is for you if you are also annoyed that there is no mutable pathlib wrapper for use cases in which paths are often changed. mutapath solves this by wrapping both, the Python 3 pathlib library, and the alternate path library, and providing a mutable context manager for them.
MutaPath Class
The MutaPath Class allows direct mutation of its attributes at any time, just as any mutable object. Once a file operation is called that is intended to modify its path, the underlying path is also mutated.
>>> from mutapath import MutaPath
>>> folder = MutaPath("/home/joe/doe/folder/sub")
>>> folder
Path('/home/joe/doe/folder/sub')
>>> folder.name = "top"
>>> folder
Path('/home/joe/doe/folder/top')
>>> next = MutaPath("/home/joe/doe/folder/next")
>>> next
Path('/home/joe/doe/folder/next')
>>> next.rename(folder)
>>> next
Path('/home/joe/doe/folder/top')
>>> next.exists()
True
>>> Path('/home/joe/doe/folder/sub').exists()
False
Path Class
This class is immutable by default, just as the pathlib.Path. However, it allows to open a editing context via mutate().
Moreover, there are additional contexts for file operations. They update the file and its path while closing the context.
If the file operations don't succeed, they throw an exception and fall back to the original path value.
>>> from mutapath import Path
>>> folder = Path("/home/joe/doe/folder/sub")
>>> folder
Path('/home/joe/doe/folder/sub')
>>> folder.name = "top"
AttributeError: mutapath.Path is an immutable class, unless mutate() context is used.
>>> folder
Path('/home/joe/doe/folder/sub')
>>> with folder.mutate() as m:
... m.name = "top"
>>> folder
Path('/home/joe/doe/folder/top')
>>> next = Path("/home/joe/doe/folder/next")
>>> next.copy(folder)
>>> next
Path('/home/joe/doe/folder/next')
>>> folder.exists()
True
>>> folder.remove()
>>> with next.renaming() as m:
... m.stem = folder.stem
... m.suffix = ".txt"
>>> next
Path("/home/joe/doe/folder/sub.txt")
>>> next.exists()
True
>>> next.with_name("next").exists()
False
For more in-depth examples, check the tests folder.
Locks
Soft Locks can easily be accessed via the lazy lock property.
Moreover, the mutable context managers in Path (i.e., renaming, moving, copying) allow implicit locking.
The lock object is cached as long as the file is not mutated.
Once the lock is mutated, it is released and regenerated, respecting the new file name.
>>> my_path = Path('/home/doe/folder/sub')
>>> with my_path.lock:
... my_path.write_text("I can write")
Hashing
mutapath paths are hashable by caching the generated hash the first time it is accessed. However, it also adds a warning so that unintended hash usage is avoided. Once mutated after that, the generated hashes don't provide collision detection in binary trees anymore. Don't use them in sets or as keys in dicts. Use the explicit string representation instead, to make the hashing input transparent.
>>> p = Path("/home")
>>> hash(p)
1083235232
>>> hash(Path("/home")) == hash(p)
True
>>> with p.mutate() as m:
... m.name = "home4"
>>> hash(p) # same hash
1083235232
>>> hash(Path("/home")) == hash(p) # they are not equal anymore
True
Metadata
Release files for mutapath 0.17.1
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 |
|---|---|---|---|---|
| mutapath-0.17.1.tar.gz | 16.1 kB | Without provenance | Details for mutapath-0.17.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mutapath-0.17.1-py3-none-any.whl | Python 3 | none | any | Details for mutapath-0.17.1-py3-none-any.whl |
Total release size: 31.3 kB
Release files / mutapath-0.17.1.tar.gz
| Download URL | mutapath-0.17.1.tar.gz |
|---|---|
| Size | 16.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a36fbbc25fac3dd1e3bfbf63f566eef1771a54a9cfbfb11ae1b614b208bb7dd7
|
|
BLAKE2b-256 checksum How to use checksums |
e70389b7833080a55ff812a5627a0e87b4fe703bb3f5a910c46eb28239ddd14f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1041-azure
|
Release files / mutapath-0.17.1-py3-none-any.whl
| Download URL | mutapath-0.17.1-py3-none-any.whl |
|---|---|
| Size | 15.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
81833d71e3c63a3b48c8cc1c0a9e6529541e7e6c610629afb206cdd7376ef0ee
|
|
BLAKE2b-256 checksum How to use checksums |
5ebcf2fc0891bebd041e1ab3785ba3e14737dcd38ac2957f25fb8c3e45c78a09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1041-azure
|
Security for mutapath 0.17.1
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.