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 alternatepath 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.
>>>frommutapathimportMutaPath
>>>folder=MutaPath("/home/joe/doe/folder/sub")>>>folderPath('/home/joe/doe/folder/sub')
>>>folder.name="top">>>folderPath('/home/joe/doe/folder/top')
>>>next=MutaPath("/home/joe/doe/folder/next")>>>nextPath('/home/joe/doe/folder/next')
>>>next.rename(folder)>>>nextPath('/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 thepathlib.Path
. However, it allows to open a editing context viamutate()
.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.
>>>frommutapathimportPath
>>>folder=Path("/home/joe/doe/folder/sub")>>>folderPath('/home/joe/doe/folder/sub')
>>>folder.name="top"AttributeError: mutapath.Path is an immutable class, unless mutate() context is used.>>>folderPath('/home/joe/doe/folder/sub')
>>>withfolder.mutate()asm:...m.name="top">>>folderPath('/home/joe/doe/folder/top')
>>>next=Path("/home/joe/doe/folder/next")>>>next.copy(folder)>>>nextPath('/home/joe/doe/folder/next')>>>folder.exists()True>>>folder.remove()
>>>withnext.renaming()asm:...m.stem=folder.stem...m.suffix=".txt">>>nextPath("/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 inPath
(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')>>>withmy_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>>>withp.mutate()asm:...m.name="home4">>>hash(p)# same hash1083235232>>>hash(Path("/home"))==hash(p)# they are not equal anymoreTrue
Documentation
| Immutable Path |
| Mutable Path |
Exception about inconsistencies between the virtual path and the real file system. | |
| Create a new lock object. |