[Python-Dev] Improvements for Pathlib
Hello, In the current incarnation Pathlib is missing some key features I need in my usecases. I want to contribute them but i'd like a bit of feedback on the new api before jumping to implementation. The four things I need are: #1. A context manager for temporary files and dirs (that cleans everything up on exit). Eg: with pathlib.TmpDir(suffix='', prefix='') as tmp: assert isinstance(tmp, pathlib.Path) with pathlib.TmpFile(suffix='', prefix='') as tmp: assert isinstance(tmp, pathlib.Path) #2. A context manager for changing working directory (that switches to the old cwd on exit). Eg: with path.cd(): assert os.getcwd() == path # 3 . Shutil-like features: - copy_file - copy_dir (or copy_tree?) - copy (does copy_dir or copy_file depending on what the source is. How to handle symlinks?) - rm_dir (or rm_tree? Also, this would be required for the TmpDir path). #4. Zip files support. A specialised Path subclass that works inside a zip file. I'm ok having this as an external package but some discussion would be useful as I'd have to rely on pathlib internals. After some brief looking in the pathlib code it would appear the wise thing would be to have the zip path as the "drive" for the ZipPaths that refer to files inside the zip. I'm not sure where the best place is to store the internal zipfile object. Perhaps the accessor? Ideally the ZipFiles would work with the shtuil-like api just fine (or at least the readonly operations). Thanks, -- Ionel M. ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Improvements for Pathlib
+1 for the context manager ideas. Anything that is like a resource should be available as a context manager. On Sat, Nov 8, 2014 at 9:46 AM, Ionel Cristian Mărieș wrote: > Hello, > > In the current incarnation Pathlib is missing some key features I need in > my usecases. I want to contribute them but i'd like a bit of feedback on > the new api before jumping to implementation. > > The four things I need are: > > #1. A context manager for temporary files and dirs (that cleans everything > up on exit). Eg: > > with pathlib.TmpDir(suffix='', prefix='') as tmp: > assert isinstance(tmp, pathlib.Path) > > with pathlib.TmpFile(suffix='', prefix='') as tmp: > assert isinstance(tmp, pathlib.Path) > > #2. A context manager for changing working directory (that switches to the > old cwd on exit). Eg: > > with path.cd(): > assert os.getcwd() == path > > # > 3 > . Shutil-like features: > > - copy_file > - copy_dir (or copy_tree?) > - copy (does copy_dir or copy_file depending on what the source is. How to > handle symlinks?) > - rm_dir (or rm_tree? Also, this would be required for the TmpDir path). > > #4. Zip files support. A specialised Path subclass that works inside a zip > file. I'm ok having this as an external package but some discussion would > be useful as I'd have to rely on pathlib internals. > > After some brief looking in the pathlib code it would appear the wise > thing would be to have the zip path as the "drive" for the ZipPaths that > refer to files inside the zip. > > I'm not sure where the best place is to store the internal zipfile object. > Perhaps the accessor? > > Ideally the ZipFiles would work with the shtuil-like api just fine (or at > least the readonly operations). > > Thanks, > -- Ionel M. > > ___ > Python-Dev mailing list > [email protected] > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/rymg19%40gmail.com > > -- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated." Personal reality distortion fields are immune to contradictory evidence. - srean Check out my website: http://kirbyfan64.github.io/ ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Improvements for Pathlib
On 2014-11-08, at 16:46 , Ionel Cristian Mărieș wrote: > Hello, > > In the current incarnation Pathlib is missing some key features I need in my > usecases. I want to contribute them but i'd like a bit of feedback on the new > api before jumping to implementation. > > The four things I need are: > > #1. A context manager for temporary files and dirs (that cleans everything up > on exit). Eg: > > with pathlib.TmpDir(suffix='', prefix='') as tmp: > assert isinstance(tmp, pathlib.Path) > > with pathlib.TmpFile(suffix='', prefix='') as tmp: > assert isinstance(tmp, pathlib.Path) > Why would pathlib need to provide this when tempfile already does? with tempfile.NamedTemporaryFile(prefix='') as f: tmp = pathlib.Path(f.name) with tempfile.TemporaryDirectoryDirectory(prefix='') as d: tmp = pathlib.Path(d.name) On 2014-11-08, at 19:14 , Ryan Gonzalez wrote: > +1 for the context manager ideas. Anything that is like a resource should be > available as a context manager. The current working directory is not "a resource" though it's a big piece of global state. I've been bitten by context-manager-unexpectedly-being-global-state in the past (with warnings.catch_warnings, I had not noticed the note about thread-safety). All in all, not a fan. ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Improvements for Pathlib
On 11/08/2014 10:46 AM, Xavier Morel wrote: On 2014-11-08, at 16:46 , Ionel Cristian Mărieș wrote: In the current incarnation Pathlib is missing some key features I need in my usecases. I want to contribute them but i'd like a bit of feedback on the new api before jumping to implementation. #1. A context manager for temporary files and dirs (that cleans everything up on exit). Why would pathlib need to provide this when tempfile already does? Because tempfile doesn't accept PathLib instances? -- ~Ethan~ ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Improvements for Pathlib
On 2014-11-08, at 20:02 , Ionel Cristian Mărieș wrote: > On Saturday, November 8, 2014, Xavier Morel wrote: > > Why would pathlib need to provide this when tempfile already does? > > with tempfile.NamedTemporaryFile(prefix='') as f: > tmp = pathlib.Path(f.name) > > with tempfile.TemporaryDirectoryDirectory(prefix='') as d: > tmp = pathlib.Path(d.name) > > For the same reason pathlib provides path manipulation functionality while > os.path already does: a cohesive and more convenient api. In other words, I > want to have less calls and variables around (less room for errors, less > visual bloat), and this is such a common pattern it's worth supporting it in > pathlib. But now you've got two different places for tempfiles depending whether you want them to have an fs path or not. And the API isn't even more convenient when it comes to tempfiles, only to tempdir. Which could be fixed just as easily by adding a `path` attribute to tempfile.TemporaryDirectory. On 2014-11-08, at 21:41 , Ethan Furman wrote: > On 11/08/2014 10:46 AM, Xavier Morel wrote: >> On 2014-11-08, at 16:46 , Ionel Cristian Mărieș wrote: >>> >>> In the current incarnation Pathlib is missing some key features I need in >>> my usecases. I want to contribute them but i'd like a bit of feedback on >>> the new api before jumping to implementation. > >>> #1. A context manager for temporary files and dirs (that cleans everything >>> up on exit). >> >> Why would pathlib need to provide this when tempfile already does? > > Because tempfile doesn't accept PathLib instances? Which can be fixed by adding support for `dir` being a Path in mkstemp, mktemp and mkdtemp. ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Improvements for Pathlib
On 08Nov2014 17:46, Ionel Cristian Mărieș wrote: #2. A context manager for changing working directory (that switches to the old cwd on exit). Eg: with path.cd(): assert os.getcwd() == path As elsewhere remarked, the current working directory is process global state. Changing it can easily have unwanted (and invisible until breakage becomes glaring) side effects. -1 on this element from me I'm afraid. Cheers, Cameron Simpson I couldn't think of anything else to do with it, so I put it on the web. ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
