summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2014-08-12 23:24:10 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2014-08-12 23:24:10 -0700
commit514a60f24137a3435bfad8f7f448235aa4139da2 (patch)
tree8248dccacd6e5d9cb0d7abc3d9a79d3c2b56c948
parent5e3c69fc471c4b2c7bd31cbf4060740ace3d501e (diff)
parent013f9090000714506af105e6b590e759b326a48e (diff)
downloadnatsort-514a60f24137a3435bfad8f7f448235aa4139da2.tar.gz
natsort version 3.4.1 release3.4.1
- 'natsort' will now use the 'fastnumbers' module if it is installed. This gives up to an extra 30% boost in speed over the previous performance enhancements. - Made documentation point to more 'natsort' resources, and also added a new example in the examples section.
-rw-r--r--.coveragerc3
-rw-r--r--.travis.yml34
-rw-r--r--MANIFEST.in5
-rw-r--r--README.rst42
-rw-r--r--docs/source/changelog.rst9
-rw-r--r--docs/source/conf.py2
-rw-r--r--docs/source/examples.rst19
-rw-r--r--docs/source/intro.rst16
-rw-r--r--natsort/_version.py2
-rw-r--r--natsort/fake_fastnumbers.py30
-rw-r--r--natsort/natsort.py71
-rw-r--r--test_natsort/test_fake_fastnumbers.py26
-rw-r--r--test_natsort/test_natsort.py61
13 files changed, 218 insertions, 102 deletions
diff --git a/.coveragerc b/.coveragerc
index 1bbfe9d..8622bd1 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -9,6 +9,9 @@ exclude_lines =
raise NotImplementedError
raise$
+ # Don't complain about alternate imports
+ except ImportError
+
# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:
diff --git a/.travis.yml b/.travis.yml
index 72df9c9..797241c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,9 +5,13 @@ python:
- 3.2
- 3.3
- 3.4
+env:
+- WITH_FASTNUMBERS=true
+- WITH_FASTNUMBERS=false
install:
- pip install pytest-cov pytest-flakes pytest-pep8
- pip install coveralls
+- if [[ $WITH_FASTNUMBERS == true ]]; then pip install fastnumbers; fi
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install argparse; fi
script:
- python -m pytest --cov natsort --flakes --pep8
@@ -16,18 +20,18 @@ script:
- python -m pytest test_natsort/stress_natsort.py
after_success:
coveralls
-before_deploy:
-- pip install Sphinx numpydoc
-- python setup.py build_sphinx
-deploy:
- provider: pypi
- user: SethMMorton
- password:
- secure: OaYQtVh4mGT0ozN7Ar2lSm2IEVMKIyvOESGPGLwVyVxPqp6oC101MovJ7041bZdjMzirMs54EJwtEGQpKFmDBGcKgbjPiYId5Nqb/yDhLC/ojgarbLoFJvUKV6dWJePyY7EOycrqcMdiDabdG80Bw4zziQExbmIOdUiscsAVVmA=
- on:
- tags: true
- all_branches: true
- repo: SethMMorton/natsort
- python: 3.3
- distributions: "sdist bdist_wheel"
- docs_dir: build/sphinx/html
+# before_deploy:
+# - pip install Sphinx numpydoc
+# - python setup.py build_sphinx
+# deploy:
+# provider: pypi
+# user: SethMMorton
+# password:
+# secure: OaYQtVh4mGT0ozN7Ar2lSm2IEVMKIyvOESGPGLwVyVxPqp6oC101MovJ7041bZdjMzirMs54EJwtEGQpKFmDBGcKgbjPiYId5Nqb/yDhLC/ojgarbLoFJvUKV6dWJePyY7EOycrqcMdiDabdG80Bw4zziQExbmIOdUiscsAVVmA=
+# on:
+# tags: true
+# all_branches: true
+# repo: SethMMorton/natsort
+# python: 2.7
+# distributions: "sdist bdist_wheel"
+# docs_dir: build/sphinx/html
diff --git a/MANIFEST.in b/MANIFEST.in
index ef9c943..be74ec3 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -5,8 +5,11 @@ include natsort/_version.py
include natsort/__main__.py
include natsort/__init__.py
include natsort/py23compat.py
-include test_natsort/test_natsorted.py
+include test_natsort/profile_natsorted.py
+include test_natsort/stress_natsort.py
+include test_natsort/test_natsort.py
include test_natsort/test_main.py
include setup.py
include setup.cfg
prune natsort/__pycache__
+graft docs/source
diff --git a/README.rst b/README.rst
index 04e9d24..5db4219 100644
--- a/README.rst
+++ b/README.rst
@@ -7,8 +7,11 @@ natsort
.. image:: https://coveralls.io/repos/SethMMorton/natsort/badge.png?branch=master
:target: https://coveralls.io/r/SethMMorton/natsort?branch=master
-Natural sorting for python. Check out the source code at
-https://github.com/SethMMorton/natsort.
+Natural sorting for python.
+
+ - Source Code: https://github.com/SethMMorton/natsort
+ - Downloads: https://pypi.python.org/pypi/natsort
+ - Documentation: http://pythonhosted.org//natsort/
Quick Description
-----------------
@@ -37,7 +40,7 @@ Using ``natsorted`` is simple::
``natsorted`` identifies real numbers anywhere in a string and sorts them
naturally.
-Sorting version numbers is just as easy::
+Sorting version numbers is just as easy with the ``versorted`` function::
>>> from natsort import versorted
>>> a = ['version-1.9', 'version-2.0', 'version-1.11', 'version-1.10']
@@ -60,10 +63,10 @@ The natsort algorithm does other fancy things like
- recursively descend into lists of lists
- sort file paths correctly
- allow custom sorting keys
- - allow exposed a natsort_key generator to pass to list.sort
+ - exposes a natsort_key generator to pass to list.sort
-Please see the package documentation for more details, including additional examples
-and recipes.
+Please see the package documentation for more details, including
+`examples and recipes <http://pythonhosted.org//natsort/examples.html>`_.
Shell script
------------
@@ -81,6 +84,16 @@ Requirements
(this includes python 3.x). To run version 2.6, 3.0, or 3.1 the
`argparse <https://pypi.python.org/pypi/argparse>`_ module is required.
+Optional Dependency
+-------------------
+
+The most efficient sorting can occur if you install the
+`fastnumbers <https://pypi.python.org/pypi/fastnumbers>`_ package (it helps
+with the string to number conversions.) ``natsort`` will still run (efficiently)
+without the package, but if you need to squeeze out that extra juice it is
+recommended you include this as a dependency. ``natsort`` will not require (or
+check) that `fastnumbers <https://pypi.python.org/pypi/fastnumbers>`_ is installed.
+
Depreciation Notices
--------------------
@@ -103,7 +116,16 @@ History
-------
These are the last three entries of the changelog. See the package documentation
-for the complete changelog.
+for the complete `changelog <http://pythonhosted.org//natsort/changelog.html>`_.
+
+08-12-2014 v. 3.4.1
+'''''''''''''''''''
+
+ - 'natsort' will now use the 'fastnumbers' module if it is installed. This
+ gives up to an extra 30% boost in speed over the previous performance
+ enhancements.
+ - Made documentation point to more 'natsort' resources, and also added a
+ new example in the examples section.
07-19-2014 v. 3.4.0
'''''''''''''''''''
@@ -148,9 +170,3 @@ for the complete changelog.
- Made docstrings for public functions mirror the README API.
- Connected natsort development to Travis-CI to help ensure quality releases.
-
-06-20-2014 v. 3.2.1
-'''''''''''''''''''
-
- - Re-"Fixed" unorderable types issue on Python 3.x - this workaround
- is for when the problem occurs in the middle of the string.
diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst
index 807bfe5..c4f8c30 100644
--- a/docs/source/changelog.rst
+++ b/docs/source/changelog.rst
@@ -3,6 +3,15 @@
Changelog
---------
+08-12-2014 v. 3.4.1
+'''''''''''''''''''
+
+ - 'natsort' will now use the 'fastnumbers' module if it is installed. This
+ gives up to an extra 30% boost in speed over the previous performance
+ enhancements.
+ - Made documentation point to more 'natsort' resources, and also added a
+ new example in the examples section.
+
07-19-2014 v. 3.4.0
'''''''''''''''''''
diff --git a/docs/source/conf.py b/docs/source/conf.py
index ee8ea53..abeb7da 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -145,7 +145,7 @@ html_theme_path = ['.']
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
-html_static_path = ['_static']
+# html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
diff --git a/docs/source/examples.rst b/docs/source/examples.rst
index 9704495..5176dd1 100644
--- a/docs/source/examples.rst
+++ b/docs/source/examples.rst
@@ -62,6 +62,25 @@ and ``signed=False``, and the :func:`~versorted` is a shortcut for
``natsorted(number_type=None)``. The recommend manner to sort version
numbers is to use :func:`~versorted`.
+Sorting with Alpha, Beta, and Release Candidates
+++++++++++++++++++++++++++++++++++++++++++++++++
+
+By default, if you wish to sort versions with a non-strict versioning
+scheme, you may not get the results you expect::
+
+ >>> a = ['1.2', '1.2rc1', '1.2beta2', '1.2beta', '1.2alpha', '1.2.1', '1.1', '1.3']
+ >>> versorted(a)
+ ['1.1', '1.2', '1.2.1', '1.2alpha', '1.2beta', '1.2beta2', '1.2rc1', '1.3']
+
+To make the '1.2' pre-releases come before '1.2.1', you need to use the following
+recipe::
+
+ >>> versorted(a, key=lambda x: x.replace('.', '~'))
+ ['1.1', '1.2', '1.2alpha', '1.2beta', '1.2beta2', '1.2rc1', '1.2.1', '1.3']
+
+Please see `this issue <https://github.com/SethMMorton/natsort/issues/13>`_ to
+see why this works.
+
Sort OS-Generated Paths
-----------------------
diff --git a/docs/source/intro.rst b/docs/source/intro.rst
index d4977e8..3348356 100644
--- a/docs/source/intro.rst
+++ b/docs/source/intro.rst
@@ -4,13 +4,16 @@
The :mod:`natsort` module
=========================
-Natural sorting for python. Check out the source code at
-https://github.com/SethMMorton/natsort.
+Natural sorting for python.
+
+ - Source Code: https://github.com/SethMMorton/natsort
+ - Downloads: https://pypi.python.org/pypi/natsort
+ - Documentation: http://pythonhosted.org//natsort/
:mod:`natsort` was initially created for sorting scientific output filenames that
contained floating point numbers in the names. There was a serious lack of
algorithms out there that could perform a natural sort on `floats` but
-plenty for ints; check out
+plenty for `ints`; check out
`this StackOverflow question <http://stackoverflow.com/q/4836710/1399279>`_
and its answers and links therein,
`this ActiveState forum <http://code.activestate.com/recipes/285264-natural-string-sorting/>`_,
@@ -109,6 +112,13 @@ If you want to build this documentation, enter::
(this includes python 3.x). To run version 2.6, 3.0, or 3.1 the
`argparse <https://pypi.python.org/pypi/argparse>`_ module is required.
+The most efficient sorting can occur if you install the
+`fastnumbers <https://pypi.python.org/pypi/fastnumbers>`_ package (it helps
+with the string to number conversions.) ``natsort`` will still run (efficiently)
+without the package, but if you need to squeeze out that extra juice it is
+recommended you include this as a dependency. ``natsort`` will not require (or
+check) that `fastnumbers <https://pypi.python.org/pypi/fastnumbers>`_ is installed.
+
:mod:`natsort` comes with a shell script called :mod:`natsort`, or can also be called
from the command line with ``python -m natsort``. The command line script is
only installed onto your ``PATH`` if you don't install via a wheel. There is
diff --git a/natsort/_version.py b/natsort/_version.py
index d220a20..d364806 100644
--- a/natsort/_version.py
+++ b/natsort/_version.py
@@ -2,4 +2,4 @@
from __future__ import (print_function, division,
unicode_literals, absolute_import)
-__version__ = '3.4.0'
+__version__ = '3.4.1'
diff --git a/natsort/fake_fastnumbers.py b/natsort/fake_fastnumbers.py
new file mode 100644
index 0000000..15d7e88
--- /dev/null
+++ b/natsort/fake_fastnumbers.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+"""\
+This module is intended to replicate some of the functionality
+from the fastnumbers module in the event that module is not
+installed.
+"""
+from __future__ import (print_function, division,
+ unicode_literals, absolute_import)
+
+import re
+
+float_re = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$')
+int_re = re.compile(r'[-+]?\d+$')
+
+
+def fast_float(x, regex_matcher=float_re.match):
+ """Convert a string to a float quickly"""
+ return float(x) if regex_matcher(x) else x
+
+
+def fast_int(x, regex_matcher=int_re.match):
+ """\
+ Convert a string to a int quickly, return input as-is if not possible.
+ """
+ return int(x) if regex_matcher(x) else x
+
+
+def isreal(x, ntypes=set([int, float])):
+ """Returns true if the input is a real number, false otherwise."""
+ return type(x) in ntypes
diff --git a/natsort/natsort.py b/natsort/natsort.py
index c67ec3f..5974199 100644
--- a/natsort/natsort.py
+++ b/natsort/natsort.py
@@ -23,6 +23,13 @@ from functools import partial
from itertools import islice
from warnings import warn
+# If the user has fastnumbers installed, they will get great speed
+# benefits. If not, we simulate the functions here.
+try:
+ from fastnumbers import fast_float, fast_int, isreal
+except ImportError:
+ from .fake_fastnumbers import fast_float, fast_int, isreal
+
from .py23compat import u_format, py23_str, py23_zip
# Make sure the doctest works for either python2 or python3
@@ -38,54 +45,37 @@ int_nosign_re = re.compile(r'(\d+)')
int_sign_re = re.compile(r'([-+]?\d+)')
# This dict will help select the correct regex and number conversion function.
regex_and_num_function_chooser = {
- (float, True, True): (float_sign_exp_re, float),
- (float, True, False): (float_sign_noexp_re, float),
- (float, False, True): (float_nosign_exp_re, float),
- (float, False, False): (float_nosign_noexp_re, float),
- (int, True, True): (int_sign_re, int),
- (int, True, False): (int_sign_re, int),
- (int, False, True): (int_nosign_re, int),
- (int, False, False): (int_nosign_re, int),
- (None, True, True): (int_nosign_re, int),
- (None, True, False): (int_nosign_re, int),
- (None, False, True): (int_nosign_re, int),
- (None, False, False): (int_nosign_re, int),
+ (float, True, True): (float_sign_exp_re, fast_float),
+ (float, True, False): (float_sign_noexp_re, fast_float),
+ (float, False, True): (float_nosign_exp_re, fast_float),
+ (float, False, False): (float_nosign_noexp_re, fast_float),
+ (int, True, True): (int_sign_re, fast_int),
+ (int, True, False): (int_sign_re, fast_int),
+ (int, False, True): (int_nosign_re, fast_int),
+ (int, False, False): (int_nosign_re, fast_int),
+ (None, True, True): (int_nosign_re, fast_int),
+ (None, True, False): (int_nosign_re, fast_int),
+ (None, False, True): (int_nosign_re, fast_int),
+ (None, False, False): (int_nosign_re, fast_int),
}
-# Number types. I have to use set([...]) and not {...}
-# because I am supporting Python 2.6.
-number_types = set([float, int])
-
-# This regex is to make sure we don't mistake a number for a file extension
-decimal = re.compile(r'\.\d')
-
def _number_finder(s, regex, numconv, py3_safe):
"""Helper to split numbers"""
- # Split the input string by numbers.
- # If there are no splits, return now.
- # If the input is not a string, ValueError is raised.
+ # Split the input string by numbers. If there are no splits, return now.
+ # If the input is not a string, TypeError is raised.
s = regex.split(s)
if len(s) == 1:
return tuple(s)
# Now convert the numbers to numbers, and leave strings as strings.
# Remove empty strings from the list.
- # Profiling showed that using regex here is much faster than
- # try/except with the numconv function.
- r = regex.match
- s = [numconv(x) if r(x) else x for x in s if x]
+ s = [numconv(x) for x in s if x]
# If the list begins with a number, lead with an empty string.
# This is used to get around the "unorderable types" issue.
- # The most common case will be a string at the front of the
- # list, and in that case the try/except method is faster than
- # using isinstance. This was chosen at the expense of the less
- # common case of a number being at the front of the list.
- try:
- s[0][0] # str supports indexing, but not numbers
- except TypeError:
+ if isreal(s[0]):
s = [''] + s
# The _py3_safe function inserts "" between numbers in the list,
@@ -95,11 +85,12 @@ def _number_finder(s, regex, numconv, py3_safe):
return _py3_safe(s) if py3_safe else s
-def _path_splitter(s):
+def _path_splitter(s, _d_match=re.compile(r'\.\d').match):
"""Split a string into its path components. Assumes a string is a path."""
path_parts = []
p_append = path_parts.append
path_location = s
+
# Continue splitting the path from the back until we have reached
# '..' or '.', or until there is nothing left to split.
while path_location != curdir and path_location != pardir:
@@ -108,29 +99,32 @@ def _path_splitter(s):
if path_location == parent_path:
break
p_append(child_path)
+
# This last append is the base path.
# Only append if the string is non-empty.
if path_location:
p_append(path_location)
+
# We created this list in reversed order, so we now correct the order.
path_parts.reverse()
+
# Now, split off the file extensions using a similar method to above.
# Continue splitting off file extensions until we reach a decimal number
# or there are no more extensions.
base = path_parts.pop()
base_parts = []
b_append = base_parts.append
- d_match = decimal.match
while True:
front = base
base, ext = splitext(front)
- if d_match(ext) or not ext:
+ if _d_match(ext) or not ext:
# Reset base to before the split if the split is invalid.
base = front
break
b_append(ext)
b_append(base)
base_parts.reverse()
+
# Return the split parent paths and then the split basename.
return path_parts + base_parts
@@ -143,12 +137,9 @@ def _py3_safe(parsed_list):
else:
new_list = [parsed_list[0]]
nl_append = new_list.append
- ntypes = number_types
for before, after in py23_zip(islice(parsed_list, 0, length-1),
islice(parsed_list, 1, None)):
- # I realize that isinstance is favored over type, but
- # in this case type is SO MUCH FASTER than isinstance!!
- if type(before) in ntypes and type(after) in ntypes:
+ if isreal(before) and isreal(after):
nl_append("")
nl_append(after)
return new_list
diff --git a/test_natsort/test_fake_fastnumbers.py b/test_natsort/test_fake_fastnumbers.py
new file mode 100644
index 0000000..29ba9af
--- /dev/null
+++ b/test_natsort/test_fake_fastnumbers.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+"""\
+Test the fake fastnumbers module.
+"""
+from natsort.fake_fastnumbers import fast_float, fast_int, isreal
+
+
+def test_fast_float():
+ assert fast_float('45.8') == 45.8
+ assert fast_float('-45') == -45.0
+ assert fast_float('45.8e-2') == 45.8e-2
+ assert fast_float('invalid') == 'invalid'
+
+
+def test_fast_int():
+ assert fast_int('45.8') == '45.8'
+ assert fast_int('-45') == -45
+ assert fast_int('+45') == 45
+ assert fast_int('invalid') == 'invalid'
+
+
+def test_isreal():
+ assert not isreal('45.8')
+ assert isreal(-45)
+ assert isreal(45.8e-2)
+ assert not isreal('invalid')
diff --git a/test_natsort/test_natsort.py b/test_natsort/test_natsort.py
index 0eeed12..264b508 100644
--- a/test_natsort/test_natsort.py
+++ b/test_natsort/test_natsort.py
@@ -11,25 +11,30 @@ from natsort.natsort import _number_finder, _py3_safe, _natsort_key
from natsort.natsort import float_sign_exp_re, float_nosign_exp_re, float_sign_noexp_re
from natsort.natsort import float_nosign_noexp_re, int_nosign_re, int_sign_re
+try:
+ from fastnumbers import fast_float, fast_int
+except ImportError:
+ from natsort.fake_fastnumbers import fast_float, fast_int
+
def test_number_finder():
- assert _number_finder('a5+5.034e-1', float_sign_exp_re, float, False) == ['a', 5.0, 0.5034]
- assert _number_finder('a5+5.034e-1', float_nosign_exp_re, float, False) == ['a', 5.0, '+', 0.5034]
- assert _number_finder('a5+5.034e-1', float_sign_noexp_re, float, False) == ['a', 5.0, 5.034, 'e', -1.0]
- assert _number_finder('a5+5.034e-1', float_nosign_noexp_re, float, False) == ['a', 5.0, '+', 5.034, 'e-', 1.0]
- assert _number_finder('a5+5.034e-1', int_nosign_re, int, False) == ['a', 5, '+', 5, '.', 34, 'e-', 1]
- assert _number_finder('a5+5.034e-1', int_sign_re, int, False) == ['a', 5, 5, '.', 34, 'e', -1]
+ assert _number_finder('a5+5.034e-1', float_sign_exp_re, fast_float, False) == ['a', 5.0, 0.5034]
+ assert _number_finder('a5+5.034e-1', float_nosign_exp_re, fast_float, False) == ['a', 5.0, '+', 0.5034]
+ assert _number_finder('a5+5.034e-1', float_sign_noexp_re, fast_float, False) == ['a', 5.0, 5.034, 'e', -1.0]
+ assert _number_finder('a5+5.034e-1', float_nosign_noexp_re, fast_float, False) == ['a', 5.0, '+', 5.034, 'e-', 1.0]
+ assert _number_finder('a5+5.034e-1', int_nosign_re, fast_int, False) == ['a', 5, '+', 5, '.', 34, 'e-', 1]
+ assert _number_finder('a5+5.034e-1', int_sign_re, fast_int, False) == ['a', 5, 5, '.', 34, 'e', -1]
- assert _number_finder('a5+5.034e-1', float_sign_exp_re, float, True) == ['a', 5.0, '', 0.5034]
- assert _number_finder('a5+5.034e-1', float_nosign_exp_re, float, True) == ['a', 5.0, '+', 0.5034]
- assert _number_finder('a5+5.034e-1', float_sign_noexp_re, float, True) == ['a', 5.0, '', 5.034, 'e', -1.0]
- assert _number_finder('a5+5.034e-1', float_nosign_noexp_re, float, True) == ['a', 5.0, '+', 5.034, 'e-', 1.0]
- assert _number_finder('a5+5.034e-1', int_nosign_re, int, True) == ['a', 5, '+', 5, '.', 34, 'e-', 1]
- assert _number_finder('a5+5.034e-1', int_sign_re, int, True) == ['a', 5, '', 5, '.', 34, 'e', -1]
+ assert _number_finder('a5+5.034e-1', float_sign_exp_re, fast_float, True) == ['a', 5.0, '', 0.5034]
+ assert _number_finder('a5+5.034e-1', float_nosign_exp_re, fast_float, True) == ['a', 5.0, '+', 0.5034]
+ assert _number_finder('a5+5.034e-1', float_sign_noexp_re, fast_float, True) == ['a', 5.0, '', 5.034, 'e', -1.0]
+ assert _number_finder('a5+5.034e-1', float_nosign_noexp_re, fast_float, True) == ['a', 5.0, '+', 5.034, 'e-', 1.0]
+ assert _number_finder('a5+5.034e-1', int_nosign_re, fast_int, True) == ['a', 5, '+', 5, '.', 34, 'e-', 1]
+ assert _number_finder('a5+5.034e-1', int_sign_re, fast_int, True) == ['a', 5, '', 5, '.', 34, 'e', -1]
- assert _number_finder('6a5+5.034e-1', float_sign_exp_re, float, False) == ['', 6.0, 'a', 5.0, 0.5034]
- assert _number_finder('6a5+5.034e-1', float_sign_exp_re, float, True) == ['', 6.0, 'a', 5.0, '', 0.5034]
+ assert _number_finder('6a5+5.034e-1', float_sign_exp_re, fast_float, False) == ['', 6.0, 'a', 5.0, 0.5034]
+ assert _number_finder('6a5+5.034e-1', float_sign_exp_re, fast_float, True) == ['', 6.0, 'a', 5.0, '', 0.5034]
def test_py3_safe():
@@ -47,21 +52,21 @@ def test_natsort_key_private():
assert a == ['num2', 'num3', 'num5']
# The below illustrates how the key works, and how the different options affect sorting.
- assert _natsort_key('a-5.034e1') == ('a', -50.34)
- assert _natsort_key('a-5.034e1', number_type=float, signed=True, exp=True) == ('a', -50.34)
- assert _natsort_key('a-5.034e1', number_type=float, signed=True, exp=False) == ('a', -5.034, 'e', 1.0)
- assert _natsort_key('a-5.034e1', number_type=float, signed=False, exp=True) == ('a-', 50.34)
- assert _natsort_key('a-5.034e1', number_type=float, signed=False, exp=False) == ('a-', 5.034, 'e', 1.0)
- assert _natsort_key('a-5.034e1', number_type=int) == ('a', -5, '.', 34, 'e', 1)
- assert _natsort_key('a-5.034e1', number_type=int, signed=False) == ('a-', 5, '.', 34, 'e', 1)
- assert _natsort_key('a-5.034e1', number_type=None) == _natsort_key('a-5.034e1', number_type=int, signed=False)
- assert _natsort_key('a-5.034e1', key=lambda x: x.upper()) == ('A', -50.34)
+ assert _natsort_key('a-5.034e2') == ('a', -503.4)
+ assert _natsort_key('a-5.034e2', number_type=float, signed=True, exp=True) == ('a', -503.4)
+ assert _natsort_key('a-5.034e2', number_type=float, signed=True, exp=False) == ('a', -5.034, 'e', 2.0)
+ assert _natsort_key('a-5.034e2', number_type=float, signed=False, exp=True) == ('a-', 503.4)
+ assert _natsort_key('a-5.034e2', number_type=float, signed=False, exp=False) == ('a-', 5.034, 'e', 2.0)
+ assert _natsort_key('a-5.034e2', number_type=int) == ('a', -5, '.', 34, 'e', 2)
+ assert _natsort_key('a-5.034e2', number_type=int, signed=False) == ('a-', 5, '.', 34, 'e', 2)
+ assert _natsort_key('a-5.034e2', number_type=None) == _natsort_key('a-5.034e2', number_type=int, signed=False)
+ assert _natsort_key('a-5.034e2', key=lambda x: x.upper()) == ('A', -503.4)
# Iterables are parsed recursively so you can sort lists of lists.
- assert _natsort_key(('a1', 'a-5.034e1')) == (('a', 1.0), ('a', -50.34))
- assert _natsort_key(('a1', 'a-5.034e1'), number_type=None) == (('a', 1), ('a-', 5, '.', 34, 'e', 1))
+ assert _natsort_key(('a1', 'a-5.034e2')) == (('a', 1.0), ('a', -503.4))
+ assert _natsort_key(('a1', 'a-5.034e2'), number_type=None) == (('a', 1), ('a-', 5, '.', 34, 'e', 2))
# A key is applied before recursion, but not in the recursive calls.
- assert _natsort_key(('a1', 'a-5.034e1'), key=itemgetter(1)) == ('a', -50.34)
+ assert _natsort_key(('a1', 'a-5.034e2'), key=itemgetter(1)) == ('a', -503.4)
# Strings that lead with a number get an empty string at the front of the tuple.
# This is designed to get around the "unorderable types" issue.
@@ -100,10 +105,10 @@ def test_natsort_key_public():
# But it raises a depreciation warning
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
- assert natsort_key('a-5.034e1') == _natsort_key('a-5.034e1')
+ assert natsort_key('a-5.034e2') == _natsort_key('a-5.034e2')
assert len(w) == 1
assert "natsort_key is depreciated as of 3.4.0, please use natsort_keygen" in str(w[-1].message)
- assert natsort_key('a-5.034e1', number_type=float, signed=False, exp=False) == _natsort_key('a-5.034e1', number_type=float, signed=False, exp=False)
+ assert natsort_key('a-5.034e2', number_type=float, signed=False, exp=False) == _natsort_key('a-5.034e2', number_type=float, signed=False, exp=False)
# It is called for each element in a list when sorting
with warnings.catch_warnings(record=True) as w: