[Python-ideas] pathlib suggestions

Steve Dower steve.dower at python.org
Wed Jan 25 13:25:40 EST 2017


On 25Jan2017 0816, Petr Viktorin wrote:
> On 01/25/2017 04:33 PM, Todd wrote:
>>     But what if the .tar.gz file is called "spam-4.2.5-final.tar.gz"?
>>     Existing tools like glob and endswith() can deal with the ".tar.gz"
>>     extension reliably, but "fullsuffix" would, arguably, not give the
>>     answers you want.
>>
>> I wouldn't use it in that situation.  The existing "suffix" and "stem"
>> properties also only work reliably under certain situations.
>
> Which situations do you mean? It works quite fine with multiple suffixes:
> The suffix of "pip-9.0.1.tar.gz" is ".gz", and sure enough, you can
> reasonably expect it's a gz-compressed file. If you uncompress it and
> strip the extension, you'll end up with a "pip-9.0.1.tar", where the
> suffix is ".tar" -- and humans would be surprised if it wasn't a tar
> archive.
>

It may be handy if suffixes was a reversed tuple of suffixes (or 
possibly a cumulative tuple):

 >>> Path('pip-9.0.1.tar.gz').suffixes
('.gz', '.tar', '.1', '.0')

This has a nice benefit for comparisons:
 >>> targzs = [f for f in all_files if f.suffixes[:2] == ('.gz', '.tar')]

It doesn't necessarily improve over .endswith(), but it has a slight 
convenience over .split() and arguably demonstrates intent more clearly. 
(Though my biggest issue with all of this is case-sensitivity, which 
probably means we need to add comparison functions to Path flavours in 
order to do this stuff properly.)


The "cumulative tuple" version would be like this:

 >>> Path('pip-9.0.1.tar.gz').suffixes
('.gz', '.tar.gz', '.1.tar.gz', '.0.1.tar.gz')

This doesn't compare as nicely, since now we would use f.suffixes[1] 
which will raise if there is only one suffix (likely). But it does 
return a value which cannot be easily recreated using other functions.

Cheers,
Steve


More information about the Python-ideas mailing list