[Python-ideas] PEP: Extended stat_result (First Draft)

Giampaolo Rodola' g.rodola at gmail.com
Mon May 6 20:28:24 CEST 2013


2013/5/6 Pieter Nagel <pieter at nagel.co.za>:
> Abstract
> ========
>
> This PEP proposes extending the result of ``os.stat()``, ``os.fstat()`` and
> ``os.lstat()`` calls with added methods such as ``is_file()``.  These added
> methods will obviate the need to use the ``stat`` module to interpret the
> result of these calls.

I'm not convinced this is a good proposal.
It duplicates a consistent amount of already existent functionality
just for the sake of avoiding importing the stat module and using
stat's S_IS* functions which "are ugly" because upper-cased.

> Specification
> =============
>
>
> Added methods on ``stat_result``
> --------------------------------
>
> is_dir()
>     Equivalent to ``bool(stat.S_ISDIR(self.st_mode))``.
>
> is_character_device()
>     Equivalent to ``bool(stat.S_ISCHR(self.st_mode))``.
>
> is_block_device()
>     Equivalent to ``bool(stat.S_ISBLK(self.st_mode))``.
>
> is_file()
>     Equivalent to ``bool(stat.S_ISREG(self.st_mode))``.
>
> is_fifo()
>     Equivalent to ``bool(stat.S_ISFIFO(self.st_mode))``.
>
> is_symbolic_link()
>     Equivalent to ``bool(stat.S_ISLNK(self.st_mode))``.
>
> is_socket()
>     Equivalent to ``bool(stat.S_ISSOCK(self.st_mode)``.

These look way too long to me.
If added I'd prefer the naming convention used so far in the
os.path.is* to be kept (therefore isfile, islink, etc.).

> same_stat(other)
>     Equivalent to ``os.path.samestat(self, other)``.

Isn't this redundant?
Aren't you introducing multiple ways for doing the same thing and
going against the Zen?

> Added functions in ``os.path``
> ------------------------------
>
> is_dir(f)
>     This shall be an alias for the existing isdir(f).
> is_file()
>     This shall be an alias for the existing isfile(f).

I'm just -1 about this.
It doesn't add anything and overcrowds and already crowded API.


> Added functions in ``os.path``
> ------------------------------
>
> is_character_device(f)
>     This shall return ``os.stat(f).is_character_device()``, or ``False`` if
>     ``f`` does not exist.
>
> is_block_device(f)
>     This shall return ``os.stat(f).is_block_device()``, or ``False`` if
>     ``f`` does not exist.
>
> is_fifo()
>     This shall return ``os.stat(f).is_fifo()``, or ``False`` if
>     ``f`` does not exist.
>
> is_socket()
>     This shall return ``os.stat(f).is_socket()``, or ``False`` if
>     ``f`` does not exist.

-1 about these too.
os.path provides only isfile(), isdir() and islink() because those are
the most common and portable file types, and that's fine.
Anything else is too specific (also *platform* specific) and does not
deserve a new utility function in os.path.


--- Giampaolo
https://code.google.com/p/pyftpdlib/
https://code.google.com/p/psutil/
https://code.google.com/p/pysendfile/



More information about the Python-ideas mailing list