Created
August 4, 2018 12:12
-
-
Save LouiS0616/bd4e1bcea03c6f4f52e7d88fc97da555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
_Path = type(Path()) | |
class Path(_Path): | |
def __iter__(self): | |
if self.is_dir(): | |
return self.iterdir() | |
raise TypeError | |
def with_stem(self, new_stem): | |
""" | |
>>> p = Path('spam/ham/egg.py') | |
>>> p.with_stem('hoge') == Path('spam/ham/hoge.py') | |
True | |
""" | |
return self.with_name( | |
new_stem + self.suffix | |
) | |
def rename(self, arg): | |
if isinstance(arg, str): | |
_Path.rename(self, arg) | |
return | |
if callable(arg): | |
new_name = arg(self.stem) | |
_Path.rename( | |
self, self.with_stem(new_name) | |
) | |
return | |
raise TypeError | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment