Enhanced stat_result objects (was: Better stdlib support for Path objects)

On 6 October 2014 18:47, Barry Warsaw <barry@python.org> wrote:
Over in issue 22570, I lament the fact that while pathlib is awesome, its wider support in the stdlib is pretty sparse.
The other thing that bugs me whenever I try to use pathlib is the non-caching behaviour of the stat-related methods. I know why this is as it is, and I agree with the reasons, but for some reason, I still find that I expect code like p = Path(...) if not p.exists(): print("Nonexistent") elif p.is_dir(): print("Directory") elif p.is_file(): print("File") else: print("Something else") to only call stat once - even though I *don't* expect that of the equivalent os.path based code. I would find it really useful if stat_result objects supported the various is_XXX methods from pathlib. Then I could write code like: p = Path(...) st = p.stat() if st.exists(): ... elif st.is_dir(): ... which explicitly shows that stat is only called once, but doesn't involve verbose and ugly code like if stat.S_ISDIR(st.st_mode): ... Would this be a worthwhile addition? Paul

On 8 October 2014 01:08, Paul Moore <p.f.moore@gmail.com> wrote:
+1 Then Path, DirEntry (from scandir) and stat_result objects would all support the same API. DirEntry.is_dir() and .is_file() methods have an optional follow_symlinks parameter - I'm thinking Path should follow suit. Obviously stat_result would not. Tim Delaney

Paul Moore <p.f.moore@gmail.com> writes:
I would find it really useful if stat_result objects supported the various is_XXX methods from pathlib.
Better, IMO, if those were data attributes of the ‘stat_result’ object, not explicit methods: p = Path(...) st = p.stat() if st.exists: ... elif st.is_dir: ...
which explicitly shows that stat is only called once
The above style IMO signals even stronger that no further filesystem calls are expected.
Would this be a worthwhile addition?
I'd support the above enhancement, yes. -- \ “The fact that I have no remedy for all the sorrows of the | `\ world is no reason for my accepting yours. It simply supports | _o__) the strong probability that yours is a fake.” —Henry L. Mencken | Ben Finney

On 8 October 2014 01:08, Paul Moore <p.f.moore@gmail.com> wrote:
+1 Then Path, DirEntry (from scandir) and stat_result objects would all support the same API. DirEntry.is_dir() and .is_file() methods have an optional follow_symlinks parameter - I'm thinking Path should follow suit. Obviously stat_result would not. Tim Delaney

Paul Moore <p.f.moore@gmail.com> writes:
I would find it really useful if stat_result objects supported the various is_XXX methods from pathlib.
Better, IMO, if those were data attributes of the ‘stat_result’ object, not explicit methods: p = Path(...) st = p.stat() if st.exists: ... elif st.is_dir: ...
which explicitly shows that stat is only called once
The above style IMO signals even stronger that no further filesystem calls are expected.
Would this be a worthwhile addition?
I'd support the above enhancement, yes. -- \ “The fact that I have no remedy for all the sorrows of the | `\ world is no reason for my accepting yours. It simply supports | _o__) the strong probability that yours is a fake.” —Henry L. Mencken | Ben Finney
participants (3)
-
Ben Finney
-
Paul Moore
-
Tim Delaney