naturalsort-1.0.3/0000775000175000017500000000000012166021002014341 5ustar peterpeter00000000000000naturalsort-1.0.3/naturalsort.egg-info/0000775000175000017500000000000012166021002020411 5ustar peterpeter00000000000000naturalsort-1.0.3/naturalsort.egg-info/dependency_links.txt0000664000175000017500000000000112166021002024457 0ustar peterpeter00000000000000 naturalsort-1.0.3/naturalsort.egg-info/top_level.txt0000664000175000017500000000002612166021002023141 0ustar peterpeter00000000000000natsort natsort_tests naturalsort-1.0.3/naturalsort.egg-info/PKG-INFO0000664000175000017500000001305012166021002021505 0ustar peterpeter00000000000000Metadata-Version: 1.0 Name: naturalsort Version: 1.0.3 Summary: Simple natural order sorting API for Python that just works Home-page: https://github.com/xolox/python-naturalsort Author: Peter Odding Author-email: peter@peterodding.com License: UNKNOWN Description: Simple natural order sorting API for Python that just works =========================================================== The ``natsort.natsort()`` function in the ``naturalsort`` package is a very simple alternative to Python's ``sorted()`` function that implements `natural order sorting`_ in Python. The package is available on PyPI, so getting started is very simple:: $ pip install naturalsort $ python > from natsort import natsort > versions = ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] > natsort(['my-package-%s' % v for v in versions]) ['my-package-1.8.1-r26', 'my-package-1.8.1-r30', 'my-package-2.0-r2', 'my-package-2.0-r7', 'my-package-2.0-r11'] The main use case that this package was originally created for is sorting of pathnames with versions numbers embedded in them. This is why the sorting key defined by the ``naturalsort`` package ignores filename extensions (not doing so can give unexpected results). Usage ----- Here's an example of regular sorting (based on the ASCII_ order of individual characters) compared to `natural order sorting`_:: > # Import the sorted() alternative. > from natsort import natsort > > # This is plain old sorting (what we DON'T want). > sorted(['1', '5', '10', '50']) ['1', '10', '5', '50'] > > # This is natural order sorting (what we DO want). > natsort(['1', '5', '10', '50']) ['1', '5', '10', '50'] Why another natsort module?! ---------------------------- There was already a natsort_ package available on PyPI before I uploaded the first release of my naturalsort_ package, so why did I upload another package with a very similar name? Because the two packages implement different forms of natural order sorting! My main use case for natural order sorting has always been for sorting filenames and pathnames, specifically those containing software version numbers. I wrote my naturalsort_ module years ago because I couldn't find any for Python, but never published it. At some point I got sick of manually copying versions of my natural order sorting module back and forth between projects so I decided to either find an alternative available on PyPI or publish my own module. That's when I found the natsort_ package and started using it in several projects. At some point I got bitten in the ass because I didn't properly test the natsort_ package for my use case. Here's a simple scenario which works as I expect it to:: > from natsort import natsorted > natsorted(['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11']) ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] However as I said my actual use case was for sorting filenames with version numbers embedded in them, for example:: > from natsort import natsorted > versions = ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] > natsorted(['my-package-%s' % v for v in versions]) ['my-package-2.0-r2', 'my-package-2.0-r7', 'my-package-2.0-r11', 'my-package-1.8.1-r26', 'my-package-1.8.1-r30'] This result really surprised me when I saw it for the first time, although it is the intended result of the natsort_ package: The hyphen before the version number is interpreted as a negative sign, which explains why 2.0 now comes before 1.8.1. So there's a long answer to a simple question: *the two packages do different things*. Use the naturalsort_ package if you need to reliably sort version numbers regardless of separators and use the natsort_ package if you need to sort strings containing more complex numbers like floating point numbers with negative signs and exponentials. Contact ------- The latest version of ``naturalsort`` is available on PyPI_ and GitHub_. For bug reports please create an issue on GitHub_. If you have questions, suggestions, etc. feel free to send me an e-mail at `peter@peterodding.com`_. License ------- This software is licensed under the `MIT license`_. © 2013 Peter Odding. .. External references: .. _ASCII: http://en.wikipedia.org/wiki/ASCII .. _GitHub: https://github.com/xolox/python-naturalsort .. _MIT license: http://en.wikipedia.org/wiki/MIT_License .. _natsort: https://pypi.python.org/pypi/natsort .. _natural order sorting: http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.htm .. _naturalsort: https://pypi.python.org/pypi/naturalsort .. _peter@peterodding.com: peter@peterodding.com .. _PyPI: https://pypi.python.org/pypi/naturalsort Platform: UNKNOWN naturalsort-1.0.3/naturalsort.egg-info/SOURCES.txt0000664000175000017500000000032312166021002022273 0ustar peterpeter00000000000000LICENSE.txt MANIFEST.in README.rst natsort.py natsort_tests.py setup.py naturalsort.egg-info/PKG-INFO naturalsort.egg-info/SOURCES.txt naturalsort.egg-info/dependency_links.txt naturalsort.egg-info/top_level.txtnaturalsort-1.0.3/MANIFEST.in0000664000175000017500000000003412156576643016125 0ustar peterpeter00000000000000include *.rst include *.txt naturalsort-1.0.3/natsort_tests.py0000775000175000017500000000160312166020125017637 0ustar peterpeter00000000000000#!/usr/bin/env python # Tests for natural order sorting module. # # Author: Peter Odding # Last Change: July 6, 2013 # URL: https://pypi.python.org/pypi/naturalsort # Standard library modules. import unittest # The module we're testing. from natsort import natsort class NaturalSortTestCase(unittest.TestCase): def runTest(self): # This is plain old sorting (what we don't want). assert sorted(['1', '5', '10', '50']) == ['1', '10', '5', '50'] # This is version sorting (what we're after). assert natsort(['1', '5', '10', '50']) == ['1', '5', '10', '50'] # This covers a previously fixed bug. assert natsort(['1.0', '1.5']) == ['1.0', '1.5'] # Test that filename extensions are ignored during sorting. filenames = ['some-package-1.0.deb', 'some-package-1.0-r1.deb'] assert natsort(filenames) == filenames naturalsort-1.0.3/README.rst0000664000175000017500000001060312166020521016035 0ustar peterpeter00000000000000Simple natural order sorting API for Python that just works =========================================================== The ``natsort.natsort()`` function in the ``naturalsort`` package is a very simple alternative to Python's ``sorted()`` function that implements `natural order sorting`_ in Python. The package is available on PyPI, so getting started is very simple:: $ pip install naturalsort $ python > from natsort import natsort > versions = ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] > natsort(['my-package-%s' % v for v in versions]) ['my-package-1.8.1-r26', 'my-package-1.8.1-r30', 'my-package-2.0-r2', 'my-package-2.0-r7', 'my-package-2.0-r11'] The main use case that this package was originally created for is sorting of pathnames with versions numbers embedded in them. This is why the sorting key defined by the ``naturalsort`` package ignores filename extensions (not doing so can give unexpected results). Usage ----- Here's an example of regular sorting (based on the ASCII_ order of individual characters) compared to `natural order sorting`_:: > # Import the sorted() alternative. > from natsort import natsort > > # This is plain old sorting (what we DON'T want). > sorted(['1', '5', '10', '50']) ['1', '10', '5', '50'] > > # This is natural order sorting (what we DO want). > natsort(['1', '5', '10', '50']) ['1', '5', '10', '50'] Why another natsort module?! ---------------------------- There was already a natsort_ package available on PyPI before I uploaded the first release of my naturalsort_ package, so why did I upload another package with a very similar name? Because the two packages implement different forms of natural order sorting! My main use case for natural order sorting has always been for sorting filenames and pathnames, specifically those containing software version numbers. I wrote my naturalsort_ module years ago because I couldn't find any for Python, but never published it. At some point I got sick of manually copying versions of my natural order sorting module back and forth between projects so I decided to either find an alternative available on PyPI or publish my own module. That's when I found the natsort_ package and started using it in several projects. At some point I got bitten in the ass because I didn't properly test the natsort_ package for my use case. Here's a simple scenario which works as I expect it to:: > from natsort import natsorted > natsorted(['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11']) ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] However as I said my actual use case was for sorting filenames with version numbers embedded in them, for example:: > from natsort import natsorted > versions = ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] > natsorted(['my-package-%s' % v for v in versions]) ['my-package-2.0-r2', 'my-package-2.0-r7', 'my-package-2.0-r11', 'my-package-1.8.1-r26', 'my-package-1.8.1-r30'] This result really surprised me when I saw it for the first time, although it is the intended result of the natsort_ package: The hyphen before the version number is interpreted as a negative sign, which explains why 2.0 now comes before 1.8.1. So there's a long answer to a simple question: *the two packages do different things*. Use the naturalsort_ package if you need to reliably sort version numbers regardless of separators and use the natsort_ package if you need to sort strings containing more complex numbers like floating point numbers with negative signs and exponentials. Contact ------- The latest version of ``naturalsort`` is available on PyPI_ and GitHub_. For bug reports please create an issue on GitHub_. If you have questions, suggestions, etc. feel free to send me an e-mail at `peter@peterodding.com`_. License ------- This software is licensed under the `MIT license`_. © 2013 Peter Odding. .. External references: .. _ASCII: http://en.wikipedia.org/wiki/ASCII .. _GitHub: https://github.com/xolox/python-naturalsort .. _MIT license: http://en.wikipedia.org/wiki/MIT_License .. _natsort: https://pypi.python.org/pypi/natsort .. _natural order sorting: http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.htm .. _naturalsort: https://pypi.python.org/pypi/naturalsort .. _peter@peterodding.com: peter@peterodding.com .. _PyPI: https://pypi.python.org/pypi/naturalsort naturalsort-1.0.3/LICENSE.txt0000664000175000017500000000204012156576634016211 0ustar peterpeter00000000000000Copyright (c) 2013 Peter Odding Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. naturalsort-1.0.3/PKG-INFO0000664000175000017500000001305012166021002015435 0ustar peterpeter00000000000000Metadata-Version: 1.0 Name: naturalsort Version: 1.0.3 Summary: Simple natural order sorting API for Python that just works Home-page: https://github.com/xolox/python-naturalsort Author: Peter Odding Author-email: peter@peterodding.com License: UNKNOWN Description: Simple natural order sorting API for Python that just works =========================================================== The ``natsort.natsort()`` function in the ``naturalsort`` package is a very simple alternative to Python's ``sorted()`` function that implements `natural order sorting`_ in Python. The package is available on PyPI, so getting started is very simple:: $ pip install naturalsort $ python > from natsort import natsort > versions = ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] > natsort(['my-package-%s' % v for v in versions]) ['my-package-1.8.1-r26', 'my-package-1.8.1-r30', 'my-package-2.0-r2', 'my-package-2.0-r7', 'my-package-2.0-r11'] The main use case that this package was originally created for is sorting of pathnames with versions numbers embedded in them. This is why the sorting key defined by the ``naturalsort`` package ignores filename extensions (not doing so can give unexpected results). Usage ----- Here's an example of regular sorting (based on the ASCII_ order of individual characters) compared to `natural order sorting`_:: > # Import the sorted() alternative. > from natsort import natsort > > # This is plain old sorting (what we DON'T want). > sorted(['1', '5', '10', '50']) ['1', '10', '5', '50'] > > # This is natural order sorting (what we DO want). > natsort(['1', '5', '10', '50']) ['1', '5', '10', '50'] Why another natsort module?! ---------------------------- There was already a natsort_ package available on PyPI before I uploaded the first release of my naturalsort_ package, so why did I upload another package with a very similar name? Because the two packages implement different forms of natural order sorting! My main use case for natural order sorting has always been for sorting filenames and pathnames, specifically those containing software version numbers. I wrote my naturalsort_ module years ago because I couldn't find any for Python, but never published it. At some point I got sick of manually copying versions of my natural order sorting module back and forth between projects so I decided to either find an alternative available on PyPI or publish my own module. That's when I found the natsort_ package and started using it in several projects. At some point I got bitten in the ass because I didn't properly test the natsort_ package for my use case. Here's a simple scenario which works as I expect it to:: > from natsort import natsorted > natsorted(['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11']) ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] However as I said my actual use case was for sorting filenames with version numbers embedded in them, for example:: > from natsort import natsorted > versions = ['1.8.1-r26', '1.8.1-r30', '2.0-r2', '2.0-r7', '2.0-r11'] > natsorted(['my-package-%s' % v for v in versions]) ['my-package-2.0-r2', 'my-package-2.0-r7', 'my-package-2.0-r11', 'my-package-1.8.1-r26', 'my-package-1.8.1-r30'] This result really surprised me when I saw it for the first time, although it is the intended result of the natsort_ package: The hyphen before the version number is interpreted as a negative sign, which explains why 2.0 now comes before 1.8.1. So there's a long answer to a simple question: *the two packages do different things*. Use the naturalsort_ package if you need to reliably sort version numbers regardless of separators and use the natsort_ package if you need to sort strings containing more complex numbers like floating point numbers with negative signs and exponentials. Contact ------- The latest version of ``naturalsort`` is available on PyPI_ and GitHub_. For bug reports please create an issue on GitHub_. If you have questions, suggestions, etc. feel free to send me an e-mail at `peter@peterodding.com`_. License ------- This software is licensed under the `MIT license`_. © 2013 Peter Odding. .. External references: .. _ASCII: http://en.wikipedia.org/wiki/ASCII .. _GitHub: https://github.com/xolox/python-naturalsort .. _MIT license: http://en.wikipedia.org/wiki/MIT_License .. _natsort: https://pypi.python.org/pypi/natsort .. _natural order sorting: http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.htm .. _naturalsort: https://pypi.python.org/pypi/naturalsort .. _peter@peterodding.com: peter@peterodding.com .. _PyPI: https://pypi.python.org/pypi/naturalsort Platform: UNKNOWN naturalsort-1.0.3/setup.cfg0000664000175000017500000000007312166021002016162 0ustar peterpeter00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 naturalsort-1.0.3/setup.py0000775000175000017500000000126412166020206016066 0ustar peterpeter00000000000000#!/usr/bin/env python from os.path import abspath, dirname, join from setuptools import setup # Fill in the long description (for the benefit of PyPi) # with the contents of README.rst (rendered by GitHub). readme_file = join(dirname(abspath(__file__)), 'README.rst') readme_text = open(readme_file, 'r').read() setup(name='naturalsort', version='1.0.3', description="Simple natural order sorting API for Python that just works", long_description=readme_text, url='https://github.com/xolox/python-naturalsort', author='Peter Odding', author_email='peter@peterodding.com', py_modules=['natsort', 'natsort_tests'], test_suite='natsort_tests') naturalsort-1.0.3/natsort.py0000664000175000017500000000227312166020162016417 0ustar peterpeter00000000000000# Simple natural order sorting API for Python that just works. # # Author: Peter Odding # Last Change: July 6, 2013 # URL: https://wiki.paylogic.eu/ItTools # Standard library modules. import re # Regular expression to match a consecutive run of digits. integer_pattern = re.compile('([0-9]+)') # Regular expression to match what looks like a filename extension. filename_extension_pattern = re.compile(r'(\.tar\.\w+|\.\w+)$') def natsort(l, key=None): """ Sort the given list in the way that humans expect (using natural order sorting). """ return sorted(l, key=lambda v: natsort_key(key and key(v) or v)) def natsort_key(s): """ Turn a string into a list of substrings and numbers that can be used as a sorting key. This is somewhat specialized towards sorting of filenames with version numbers in them; it strips and ignores (what look like) filename extensions. """ s = filename_extension_pattern.sub('', s) return [coerce(c) for c in integer_pattern.split(s) if c != ''] def coerce(s): """ Coerce strings of digits into proper integers. """ if s.isdigit(): return int(s) else: return s