hugovk / pypi-tools

Command-line Python scripts to do things with PyPI
https://hugovk.github.io/pypi-tools
23 stars 2 forks source link

Use natsort to sort '3.10', 'None', 'Sure.0' #9

Closed hugovk closed 5 years ago

hugovk commented 5 years ago

The lists of versions includes a '3.10' and sorted(all_versions) puts them in alphabetical order as '3.1', '3.10', '3.2':

['2.6',
 '2.7',
 '2.7b2',
 '2.7rc1',
 '2.7rc2',
 '2.8',
 '3.1',
 '3.10',
 '3.2',
 '3.2a4',
 '3.2rc1',
 '3.3',
 '3.4',
 '3.5',
 '3.6',
 '3.7',
 '3.8',
 '3.9',
 '5.0',
 'None',
 'Sure.0']

One option is to use from distutils.version import LooseVersion, but that doesn't like 'None', 'Sure.0':

    all_versions.sort(key=LooseVersion)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/version.py", line 52, in __lt__
    c = self._cmp(other)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/version.py", line 337, in _cmp
    if self.version < other.version:
TypeError: '<' not supported between instances of 'str' and 'int'

Instead, we can use natsort which deals with '3.10', 'None', 'Sure.0':

['2.6',
 '2.7',
 '2.7b2',
 '2.7rc1',
 '2.7rc2',
 '2.8',
 '3.1',
 '3.2',
 '3.2a4',
 '3.2rc1',
 '3.3',
 '3.4',
 '3.5',
 '3.6',
 '3.7',
 '3.8',
 '3.9',
 '3.10',
 '5.0',
 'None',
 'Sure.0']