diff options
author | Seth M Morton <seth.m.morton@gmail.com> | 2018-11-18 22:14:59 -0800 |
---|---|---|
committer | Seth Morton <seth.m.morton@gmail.com> | 2019-03-03 21:17:49 -0800 |
commit | c1535af21499acec7b6f1d5aa17b613e7d008209 (patch) | |
tree | 6a4060ae1f018b4c29198001ee0c48b605fc4de6 | |
parent | 06719db445ea8642dcce49b99546e983971e7fda (diff) | |
download | natsort-c1535af21499acec7b6f1d5aa17b613e7d008209.tar.gz |
Remove nat_cmp function
cmp is Python 2 only.
-rw-r--r-- | natsort/natsort.py | 91 | ||||
-rw-r--r-- | tests/test_natsort_cmp.py | 83 |
2 files changed, 11 insertions, 163 deletions
diff --git a/natsort/natsort.py b/natsort/natsort.py index 762df16..99b6a1a 100644 --- a/natsort/natsort.py +++ b/natsort/natsort.py @@ -12,11 +12,10 @@ from operator import itemgetter import natsort.compat.locale from natsort import utils -from natsort.compat.py23 import py23_cmp, py23_str, u_format +from natsort.compat.py23 import py23_str from natsort.ns_enum import NS_DUMB, ns -@u_format def decoder(encoding): """ Return a function that can be used to decode bytes to unicode. @@ -58,7 +57,6 @@ def decoder(encoding): return partial(utils.do_decoding, encoding=encoding) -@u_format def as_ascii(s): """ Function to decode an input with the ASCII codec, or return as-is. @@ -82,7 +80,6 @@ def as_ascii(s): return utils.do_decoding(s, "ascii") -@u_format def as_utf8(s): """ Function to decode an input with the UTF-8 codec, or return as-is. @@ -106,7 +103,6 @@ def as_utf8(s): return utils.do_decoding(s, "utf-8") -@u_format def natsort_keygen(key=None, alg=ns.DEFAULT): """ Generate a key to sort strings and numbers naturally. @@ -150,7 +146,7 @@ def natsort_keygen(key=None, alg=ns.DEFAULT): >>> a = ['num5.10', 'num-3', 'num5.3', 'num2'] >>> a.sort(key=natsort_keygen(alg=ns.REAL)) >>> a - [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3'] + ['num-3', 'num2', 'num5.10', 'num5.3'] """ try: @@ -217,7 +213,6 @@ natsort_keygen """ -@u_format def natsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Sorts an iterable naturally. @@ -259,14 +254,13 @@ def natsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): >>> a = ['num3', 'num5', 'num2'] >>> natsorted(a) - [{u}'num2', {u}'num3', {u}'num5'] + ['num2', 'num3', 'num5'] """ key = natsort_keygen(key, alg) return sorted(seq, reverse=reverse, key=key) -@u_format def humansorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Convenience function to properly sort non-numeric characters. @@ -311,15 +305,14 @@ def humansorted(seq, key=None, reverse=False, alg=ns.DEFAULT): >>> a = ['Apple', 'Banana', 'apple', 'banana'] >>> natsorted(a) - [{u}'Apple', {u}'Banana', {u}'apple', {u}'banana'] + ['Apple', 'Banana', 'apple', 'banana'] >>> humansorted(a) - [{u}'apple', {u}'Apple', {u}'banana', {u}'Banana'] + ['apple', 'Apple', 'banana', 'Banana'] """ return natsorted(seq, key, reverse, alg | ns.LOCALE) -@u_format def realsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Convenience function to properly sort signed floats. @@ -365,15 +358,14 @@ def realsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): >>> a = ['num5.10', 'num-3', 'num5.3', 'num2'] >>> natsorted(a) - [{u}'num2', {u}'num5.3', {u}'num5.10', {u}'num-3'] + ['num2', 'num5.3', 'num5.10', 'num-3'] >>> realsorted(a) - [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3'] + ['num-3', 'num2', 'num5.10', 'num5.3'] """ return natsorted(seq, key, reverse, alg | ns.REAL) -@u_format def index_natsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Determine the list of the indexes used to sort the input sequence. @@ -425,9 +417,9 @@ def index_natsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): [2, 0, 1] >>> # Sort both lists by the sort order of a >>> order_by_index(a, index) - [{u}'num2', {u}'num3', {u}'num5'] + ['num2', 'num3', 'num5'] >>> order_by_index(b, index) - [{u}'baz', {u}'foo', {u}'bar'] + ['baz', 'foo', 'bar'] """ if key is None: @@ -443,7 +435,6 @@ def index_natsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): return [x for x, _ in index_seq_pair] -@u_format def index_humansorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ This is a wrapper around ``index_natsorted(seq, alg=ns.LOCALE)``. @@ -493,7 +484,6 @@ def index_humansorted(seq, key=None, reverse=False, alg=ns.DEFAULT): return index_natsorted(seq, key, reverse, alg | ns.LOCALE) -@u_format def index_realsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ This is a wrapper around ``index_natsorted(seq, alg=ns.REAL)``. @@ -540,7 +530,6 @@ def index_realsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): # noinspection PyShadowingBuiltins,PyUnresolvedReferences -@u_format def order_by_index(seq, index, iter=False): """ Order a given sequence by an index sequence. @@ -592,67 +581,9 @@ def order_by_index(seq, index, iter=False): [2, 0, 1] >>> # Sort both lists by the sort order of a >>> order_by_index(a, index) - [{u}'num2', {u}'num3', {u}'num5'] + ['num2', 'num3', 'num5'] >>> order_by_index(b, index) - [{u}'baz', {u}'foo', {u}'bar'] + ['baz', 'foo', 'bar'] """ return (seq[i] for i in index) if iter else [seq[i] for i in index] - - -if float(sys.version[:3]) < 3: - # pylint: disable=unused-variable - # noinspection PyUnresolvedReferences,PyPep8Naming - class natcmp(object): # noqa: N801 - """ - 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 - """ - - cached_keys = {} - - def __new__(cls, x, y, alg=ns.DEFAULT): - try: - ns.DEFAULT | alg - except TypeError: - msg = "natsort_keygen: 'alg' argument must be from the enum 'ns'" - raise ValueError(msg + ", got {}".format(py23_str(alg))) - - # Add the _DUMB option if the locale library is broken. - if alg & ns.LOCALEALPHA and natsort.compat.locale.dumb_sort(): - alg |= NS_DUMB - - if alg not in cls.cached_keys: - cls.cached_keys[alg] = natsort_keygen(alg=alg) - - return py23_cmp(cls.cached_keys[alg](x), cls.cached_keys[alg](y)) diff --git a/tests/test_natsort_cmp.py b/tests/test_natsort_cmp.py deleted file mode 100644 index 41a252f..0000000 --- a/tests/test_natsort_cmp.py +++ /dev/null @@ -1,83 +0,0 @@ -# -*- coding: utf-8 -*- -# pylint: disable=unused-variable -"""These test the natcmp() function. - -Note that these tests are only relevant for Python version < 3. -""" -from functools import partial - -import pytest -from hypothesis import given -from hypothesis.strategies import floats, integers, lists -from natsort import ns -from natsort.compat.py23 import PY_VERSION, py23_cmp - -if PY_VERSION < 3: - from natsort import natcmp - - -class Comparable(object): - """Stub class for testing natcmp functionality.""" - - def __init__(self, value): - self.value = value - - def __cmp__(self, other): - return natcmp(self.value, other.value) - - -@pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -class TestNatCmp: - - def test_classes_can_be_compared(self): - one = Comparable("1") - two = Comparable("2") - another_two = Comparable("2") - ten = Comparable("10") - assert ten > two == another_two > one - - def test_keys_are_being_cached(self, mocker): - natcmp.cached_keys = {} - assert len(natcmp.cached_keys) == 0 - natcmp(0, 0) - assert len(natcmp.cached_keys) == 1 - natcmp(0, 0) - assert len(natcmp.cached_keys) == 1 - - with mocker.patch("natsort.compat.locale.dumb_sort", return_value=False): - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 2 - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 2 - - with mocker.patch("natsort.compat.locale.dumb_sort", return_value=True): - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 3 - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 3 - - def test_illegal_algorithm_raises_error(self): - with pytest.raises(ValueError): - natcmp(0, 0, alg="Just random stuff") - - def test_classes_can_utilize_max_or_min(self): - comparables = [Comparable(i) for i in range(10)] - - assert max(comparables) == comparables[-1] - assert min(comparables) == comparables[0] - - @given(integers(), integers()) - def test_natcmp_works_the_same_for_integers_as_cmp(self, x, y): - assert py23_cmp(x, y) == natcmp(x, y) - - @given(floats(allow_nan=False), floats(allow_nan=False)) - def test_natcmp_works_the_same_for_floats_as_cmp(self, x, y): - assert py23_cmp(x, y) == natcmp(x, y) - - @given(lists(elements=integers())) - def test_sort_strings_with_numbers(self, a_list): - strings = [str(var) for var in a_list] - # noinspection PyArgumentList - natcmp_sorted = sorted(strings, cmp=partial(natcmp, alg=ns.SIGNED)) - - assert sorted(a_list) == [int(var) for var in natcmp_sorted] |