diff options
author | d-kiss <speakupness@gmail.com> | 2018-02-03 18:34:59 +0200 |
---|---|---|
committer | d-kiss <speakupness@gmail.com> | 2018-02-03 18:34:59 +0200 |
commit | 4db30fce6b89fbe31bca46a7b7a14ff1316c0661 (patch) | |
tree | 229c22164f92571214ba70ef25aa91b2686a0755 | |
parent | 4135a38bf27be125b6dd2be3bfac1546fa06094b (diff) | |
download | natsort-4db30fce6b89fbe31bca46a7b7a14ff1316c0661.tar.gz |
natcmp does not appear in Python 3 anymore.
-rw-r--r-- | natsort/__init__.py | 11 | ||||
-rw-r--r-- | natsort/compat/py23.py | 7 | ||||
-rw-r--r-- | natsort/natsort.py | 80 | ||||
-rw-r--r-- | test_natsort/test_natsort_cmp.py | 21 | ||||
-rw-r--r-- | test_natsort/test_utils.py | 9 |
5 files changed, 76 insertions, 52 deletions
diff --git a/natsort/__init__.py b/natsort/__init__.py index 3fe1c9f..45ede6e 100644 --- a/natsort/__init__.py +++ b/natsort/__init__.py @@ -7,6 +7,11 @@ from __future__ import ( ) # Local imports. +import sys + +from natsort.utils import chain_functions +from natsort._version import __version__ + from natsort.natsort import ( natsort_key, natsort_keygen, @@ -23,10 +28,10 @@ from natsort.natsort import ( as_ascii, as_utf8, ns, - natcmp, ) -from natsort.utils import chain_functions -from natsort._version import __version__ + +if float(sys.version[:3]) < 3: + from natsort.natsort import natcmp __all__ = [ 'natsort_key', diff --git a/natsort/compat/py23.py b/natsort/compat/py23.py index 8155798..fa56b06 100644 --- a/natsort/compat/py23.py +++ b/natsort/compat/py23.py @@ -30,6 +30,13 @@ py23_basestring = str if sys.version[0] == '3' else basestring # unichr function py23_unichr = chr if sys.version[0] == '3' else unichr + +def _py23_cmp(a, b): + return (a > b) - (a < b) + + +py23_cmp = _py23_cmp if sys.version[0] == '3' else cmp + # zip as an iterator if sys.version[0] == '3': py23_zip = zip diff --git a/natsort/natsort.py b/natsort/natsort.py index 53e5054..20d3ea0 100644 --- a/natsort/natsort.py +++ b/natsort/natsort.py @@ -23,12 +23,14 @@ from functools import partial from warnings import warn # Local imports. +import sys + import natsort.compat.locale from natsort.ns_enum import ns from natsort.compat.py23 import ( u_format, py23_str, -) + py23_cmp) from natsort.utils import ( _natsort_key, _args_to_enum, @@ -675,41 +677,41 @@ def order_by_index(seq, index, iter=False): """ return (seq[i] for i in index) if iter else [seq[i] for i in index] - -def natcmp(x, y, alg=0, **kwargs): - """ - Compare two objects using a key and an algorithm. - - Parameters - ---------- - x : object - First object to compare. - - y : object - Second object to compare. - - alg : ns enum, optional - This option is used to control which algorithm `natsort` - uses when sorting. For details into these options, please see - the :class:`ns` class documentation. The default is `ns.INT`. - - Returns - ------- - out: int - 0 if x and y are equal, 1 if x > y, -1 if y > x. - - See Also - -------- - natsort_keygen : Generates the key that makes natural sorting possible. - - Examples - -------- - Use `natcmp` just like the builtin `cmp`:: - - >>> one = 1 - >>> two = 2 - >>> natcmp(one, two) - -1 - """ - key = natsort_keygen(alg=alg, **kwargs) - return (key(x) > key(y)) - (key(x) < key(y)) +if float(sys.version[:3]) < 3: + def natcmp(x, y, alg=0, **kwargs): + """ + Compare two objects using a key and an algorithm. + + Parameters + ---------- + x : object + First object to compare. + + y : object + Second object to compare. + + alg : ns enum, optional + This option is used to control which algorithm `natsort` + uses when sorting. For details into these options, please see + the :class:`ns` class documentation. The default is `ns.INT`. + + Returns + ------- + out: int + 0 if x and y are equal, 1 if x > y, -1 if y > x. + + See Also + -------- + natsort_keygen : Generates a key that makes natural sorting possible. + + Examples + -------- + Use `natcmp` just like the builtin `cmp`:: + + >>> one = 1 + >>> two = 2 + >>> natcmp(one, two) + -1 + """ + key = natsort_keygen(alg=alg, **kwargs) + return py23_cmp(key(x), key(y)) diff --git a/test_natsort/test_natsort_cmp.py b/test_natsort/test_natsort_cmp.py index 2dbf6b5..74bb61b 100644 --- a/test_natsort/test_natsort_cmp.py +++ b/test_natsort/test_natsort_cmp.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- # pylint: disable=unused-variable -"""These test the natcmp() function.""" +"""These test the natcmp() function. + +Note that these tests are only relevant for Python version < 3. +""" import sys from functools import partial @@ -8,14 +11,14 @@ import pytest from hypothesis import given from hypothesis.strategies import floats, integers, lists -from natsort import natcmp, ns +from natsort import ns -PY_VERSION = float(sys.version[:3]) +from natsort.compat.py23 import py23_cmp +PY_VERSION = float(sys.version[:3]) -if PY_VERSION >= 3.0: - def cmp(a, b): - return (a > b) - (a < b) +if PY_VERSION < 3: + from natsort import natcmp class Comparable(object): @@ -48,13 +51,13 @@ def test__classes_can_utilize_max_or_min(): @pytest.mark.skipif(PY_VERSION >= 3.0, reason='cmp() deprecated in Python 3') @given(integers(), integers()) def test__natcmp_works_the_same_for_integers_as_cmp(x, y): - assert cmp(x, y) == natcmp(x, y) + assert py23_cmp(x, y) == natcmp(x, y) @pytest.mark.skipif(PY_VERSION >= 3.0, reason='cmp() deprecated in Python 3') @given(floats(allow_nan=False), floats(allow_nan=False)) def test__natcmp_works_the_same_for_floats_as_cmp(x, y): - assert cmp(x, y) == natcmp(x, y) + assert py23_cmp(x, y) == natcmp(x, y) @pytest.mark.skipif(PY_VERSION >= 3.0, reason='cmp() deprecated in Python 3') @@ -63,4 +66,4 @@ def test_sort_strings_with_numbers(a_list): strings = map(str, a_list) natcmp_sorted = sorted(strings, cmp=partial(natcmp, alg=ns.REAL)) - assert sorted(a_list) == map(int, natcmp_sorted) + assert sorted(a_list) == [int(var) for var in natcmp_sorted] diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py index f1cffa2..d472702 100644 --- a/test_natsort/test_utils.py +++ b/test_natsort/test_utils.py @@ -23,7 +23,7 @@ from natsort.utils import ( _groupletters, chain_functions, ) -from natsort.compat.py23 import py23_str +from natsort.compat.py23 import py23_str, py23_cmp from natsort.compat.locale import null_string from slow_splitters import ( sep_inserter, @@ -232,3 +232,10 @@ def test_path_splitter_splits_path_string_by_separator_and_removes_extension(x): z = py23_str(pathlib.Path(*x[:-2])) + '.' + x[-1] y = tuple(pathlib.Path(z).parts) assert tuple(_path_splitter(z)) == y[:-1] + (pathlib.Path(z).stem, pathlib.Path(z).suffix) + + +@given(integers()) +def test_py23_cmp(x): + assert py23_cmp(x, x) == 0 + assert py23_cmp(x, x + 1) < 0 + assert py23_cmp(x, x - 1) > 0 |