Skip to content

Commit

Permalink
Revert "Revert "Drop support for EOL Pythons""
Browse files Browse the repository at this point in the history
This reverts commit 916a92d.
  • Loading branch information
SethMMorton committed Nov 19, 2018
1 parent b0aa21b commit e18c469
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 85 deletions.
3 changes: 1 addition & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ from the command line with ``python -m natsort``.
Requirements
------------

``natsort`` requires Python version 2.6 or greater or Python 3.3 or greater.
It may run on (but is not tested against) Python 3.2.
``natsort`` requires Python version 2.7 or Python 3.4 or greater.

Optional Dependencies
---------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ sort key so that:
... def __init__(self, bar):
... self.bar = bar
... def __repr__(self):
... return "Foo('{0}')".format(self.bar)
... return "Foo('{}')".format(self.bar)
>>> b = [Foo('num3'), Foo('num5'), Foo('num2')]
>>> natsorted(b, key=attrgetter('bar'))
[Foo('num2'), Foo('num3'), Foo('num5')]
Expand Down
5 changes: 1 addition & 4 deletions docs/howitworks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -897,10 +897,7 @@ It turns out the there is a bug in the legacy Python implementation of
input (https://bugs.python.org/issue2481). :func:`locale.strcoll` works,
but is intended for use with ``cmp``, which does not exist in current Python
implementations. Luckily, the :func:`functools.cmp_to_key` function
makes :func:`locale.strcoll` behave like :func:`locale.strxfrm` (that is, of course,
unless you are on Python 2.6 where :func:`functools.cmp_to_key` doesn't exist,
in which case you simply copy-paste the implementation from Python 2.7
directly into your code ☹).
makes :func:`locale.strcoll` behave like :func:`locale.strxfrm`.

Handling Broken Locale On OSX
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 1 addition & 2 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ from the command line with ``python -m natsort``.
Requirements
------------

:mod:`natsort` requires Python version 2.6 or greater or Python 3.3 or greater.
It may run on (but is not tested against) Python 3.2.
:mod:`natsort` requires Python version 2.7 or Python 3.4 or greater.

Optional Dependencies
---------------------
Expand Down
2 changes: 1 addition & 1 deletion natsort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@
# them to natsort's main namespace.
with warnings.catch_warnings():
warnings.simplefilter("ignore")
globals().update(dict((k, getattr(ns, k)) for k in dir(ns) if k.isupper()))
globals().update(ns._asdict())
2 changes: 1 addition & 1 deletion natsort/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main(*arguments):
parser.add_argument(
"--version",
action="version",
version="%(prog)s {0}".format(natsort.__version__),
version="%(prog)s {}".format(natsort.__version__),
)
parser.add_argument(
"-p",
Expand Down
3 changes: 2 additions & 1 deletion natsort/compat/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

# Std. lib imports.
import sys
from functools import cmp_to_key

# Local imports.
from natsort.compat.py23 import PY_VERSION, cmp_to_key, py23_unichr
from natsort.compat.py23 import PY_VERSION, py23_unichr

# This string should be sorted after any other byte string because
# it contains the max unicode character repeated 20 times.
Expand Down
37 changes: 0 additions & 37 deletions natsort/compat/py23.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,6 @@ def _py23_cmp(a, b):
py23_map = itertools.imap
py23_filter = itertools.ifilter

# cmp_to_key was not created till 2.7, so require this for 2.6
try:
from functools import cmp_to_key
except ImportError: # pragma: no cover

def cmp_to_key(mycmp):
"""Convert a cmp= function into a key= function"""

class K(object):
__slots__ = ["obj"]

def __init__(self, obj):
self.obj = obj

def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0

def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0

def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0

def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0

def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0

def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0

def __hash__(self):
raise TypeError("hash not implemented")

return K


# This function is intended to decorate other functions that will modify
# either a string directly, or a function's docstring.
Expand Down
4 changes: 2 additions & 2 deletions natsort/natsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def natsort_keygen(key=None, alg=ns.DEFAULT, **_kwargs):
alg = utils.args_to_enum(**_kwargs) | alg
except TypeError:
msg = "natsort_keygen: 'alg' argument must be from the enum 'ns'"
raise ValueError(msg + ", got {0}".format(py23_str(alg)))
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():
Expand Down Expand Up @@ -687,7 +687,7 @@ def __new__(cls, x, y, alg=ns.DEFAULT, *args, **kwargs):
alg = utils.args_to_enum(**kwargs) | alg
except TypeError:
msg = "natsort_keygen: 'alg' argument must be " "from the enum 'ns'"
raise ValueError(msg + ", got {0}".format(py23_str(alg)))
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():
Expand Down
27 changes: 12 additions & 15 deletions natsort/ns_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import collections
import warnings

# NOTE: OrderedDict is not used below for compatibility with Python 2.6.

# The below are the base ns options. The values will be stored as powers
# of two so bitmasks can be used to extract the user's requested options.
enum_options = [
Expand Down Expand Up @@ -58,26 +56,25 @@
]

# Construct the list of bitwise distinct enums with their fields.
enum_fields = [(name, 1 << i) for i, name in enumerate(enum_options)]
enum_fields.extend((name, 0) for name in enum_do_nothing)
enum_fields = collections.OrderedDict(
(name, 1 << i) for i, name in enumerate(enum_options)
)
enum_fields.update((name, 0) for name in enum_do_nothing)

for name, combo in enum_combos:
current_mapping = dict(enum_fields)
combined_value = current_mapping[combo[0]]
combined_value = enum_fields[combo[0]]
for combo_name in combo[1:]:
combined_value |= current_mapping[combo_name]
enum_fields.append((name, combined_value))

current_mapping = dict(enum_fields)
enum_fields.extend((alias, current_mapping[name]) for alias, name in enum_aliases)
combined_value |= enum_fields[combo_name]
enum_fields[name] = combined_value

# Finally, extract out the enum field names and their values.
enum_field_names, enum_field_values = zip(*enum_fields)
enum_fields.update(
(alias, enum_fields[name]) for alias, name in enum_aliases
)


# Subclass the namedtuple to improve the docstring.
# noinspection PyUnresolvedReferences
class _NSEnum(collections.namedtuple("_NSEnum", enum_field_names)):
class _NSEnum(collections.namedtuple("_NSEnum", enum_fields.keys())):
"""
Enum to control the `natsort` algorithm.
Expand Down Expand Up @@ -238,7 +235,7 @@ def DIGIT(self): # noqa: N802

# Here is where the instance of the ns enum that will be exported is created.
# It is a poor-man's singleton.
ns = _NSEnum(*enum_field_values)
ns = _NSEnum(*enum_fields.values())

# The below is private for internal use only.
ns_DUMB = 1 << 31
2 changes: 1 addition & 1 deletion natsort/unicode_numeric_hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@
a = py23_unichr(i)
except ValueError:
break
if a in set("0123456789"):
if a in "0123456789":
continue
if unicodedata.numeric(a, None) is not None:
hex_chars.append(i)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ classifiers =
Operating System :: OS Independent
License :: OSI Approved :: MIT License
Natural Language :: English
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 2.6
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3
Programming Language :: Python :: 3.4
Expand Down
19 changes: 5 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
#! /usr/bin/env python

import sys

from setuptools import find_packages, setup

# Very old versions of setuptools do not support the python version
# specifier syntax, so logic must be defined in code (see issue #64).
install_requires = []
extras_require = {"icu": "PyICU >= 1.0.0"}
if sys.version_info[:2] == (2, 6):
install_requires.append("argparse")
else:
extras_require["fast"] = "fastnumbers >= 2.0.0"

setup(
name='natsort',
version='5.5.0',
packages=find_packages(),
install_requires=install_requires,
entry_points={'console_scripts': ['natsort = natsort.__main__:main']},
extras_require=extras_require,
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
extras_require={
'fast': ["fastnumbers >= 2.0.0"],
'icu': ["PyICU >= 1.0.0"]
}
)
4 changes: 2 additions & 2 deletions test_natsort/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
def load_locale(x):
"""Convenience to load a locale, trying ISO8859-1 first."""
try:
locale.setlocale(locale.LC_ALL, str("{0}.ISO8859-1".format(x)))
locale.setlocale(locale.LC_ALL, str("{}.ISO8859-1".format(x)))
except locale.Error:
locale.setlocale(locale.LC_ALL, str("{0}.UTF-8".format(x)))
locale.setlocale(locale.LC_ALL, str("{}.UTF-8".format(x)))


@pytest.fixture()
Expand Down
44 changes: 44 additions & 0 deletions test_natsort/test_ns_enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from natsort import ns


def test_ns_enum():
enum_name_values = [
("FLOAT", 0x0001),
("SIGNED", 0x0002),
("NOEXP", 0x0004),
("PATH", 0x0008),
("LOCALEALPHA", 0x0010),
("LOCALENUM", 0x0020),
("IGNORECASE", 0x0040),
("LOWERCASEFIRST", 0x0080),
("GROUPLETTERS", 0x0100),
("UNGROUPLETTERS", 0x0200),
("NANLAST", 0x0400),
("COMPATIBILITYNORMALIZE", 0x0800),
("NUMAFTER", 0x1000),
("DEFAULT", 0x0000),
("INT", 0x0000),
("UNSIGNED", 0x0000),
("REAL", 0x0003),
("LOCALE", 0x0030),
("I", 0x0000),
("U", 0x0000),
("F", 0x0001),
("S", 0x0002),
("R", 0x0003),
("N", 0x0004),
("P", 0x0008),
("LA", 0x0010),
("LN", 0x0020),
("L", 0x0030),
("IC", 0x0040),
("LF", 0x0080),
("G", 0x0100),
("UG", 0x0200),
("C", 0x0200),
("CAPITALFIRST", 0x0200),
("NL", 0x0400),
("CN", 0x0800),
("NA", 0x1000),
]
assert list(ns._asdict().items()) == enum_name_values
2 changes: 1 addition & 1 deletion test_natsort/test_unicode_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_numeric_chars_contains_all_valid_unicode_numeric_and_digit_characters()
a = py23_unichr(i)
except ValueError:
break
if a in set("0123456789"):
if a in "0123456789":
continue
if unicodedata.numeric(a, None) is not None:
assert i in set_numeric_hex
Expand Down

0 comments on commit e18c469

Please sign in to comment.