diff options
| author | Seth M Morton <seth.m.morton@gmail.com> | 2015-06-24 23:02:54 -0700 |
|---|---|---|
| committer | Seth M Morton <seth.m.morton@gmail.com> | 2015-06-24 23:02:54 -0700 |
| commit | 5c8de308107fe33b76bd8f4840ce8a773c8fd0f0 (patch) | |
| tree | 4d7f92fd9066343b08c1f2e7bd4e5913c1cf4b48 | |
| parent | d8aca1dd56638b2bfff0b70232c6801cf8b4b471 (diff) | |
| download | natsort-5c8de308107fe33b76bd8f4840ce8a773c8fd0f0.tar.gz | |
Moved all non-testing compat functions to compat folder.
This makes the boilerplate at the top of the main natsort files simply
import based, and all the try: excepts and if: else statements are
in the files contained in the compat folder.
| -rw-r--r-- | natsort/compat/fake_fastnumbers.py (renamed from natsort/fake_fastnumbers.py) | 0 | ||||
| -rw-r--r-- | natsort/compat/fastnumbers.py | 28 | ||||
| -rw-r--r-- | natsort/compat/locale.py | 59 | ||||
| -rw-r--r-- | natsort/compat/pathlib.py | 15 | ||||
| -rw-r--r-- | natsort/compat/py23.py | 39 | ||||
| -rw-r--r-- | natsort/locale_help.py | 87 | ||||
| -rw-r--r-- | natsort/natsort.py | 1 | ||||
| -rw-r--r-- | natsort/ns_enum.py | 1 | ||||
| -rw-r--r-- | natsort/unicode_numbers.py | 1 | ||||
| -rw-r--r-- | natsort/utils.py | 42 | ||||
| -rw-r--r-- | setup.cfg | 1 | ||||
| -rw-r--r-- | test_natsort/test_fake_fastnumbers.py | 2 | ||||
| -rw-r--r-- | test_natsort/test_locale_help.py | 9 | ||||
| -rw-r--r-- | test_natsort/test_utils.py | 21 |
14 files changed, 170 insertions, 136 deletions
diff --git a/natsort/fake_fastnumbers.py b/natsort/compat/fake_fastnumbers.py index 6fd954a..6fd954a 100644 --- a/natsort/fake_fastnumbers.py +++ b/natsort/compat/fake_fastnumbers.py diff --git a/natsort/compat/fastnumbers.py b/natsort/compat/fastnumbers.py new file mode 100644 index 0000000..c2c603b --- /dev/null +++ b/natsort/compat/fastnumbers.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import ( + print_function, + division, + unicode_literals, + absolute_import +) + +# If the user has fastnumbers installed, they will get great speed +# benefits. If not, we use the simulated functions that come with natsort. +try: + from fastnumbers import ( + fast_float, + fast_int, + isint, + isfloat, + ) + import fastnumbers + v = list(map(int, fastnumbers.__version__.split('.'))) + if not (v[0] >= 0 and v[1] >= 5): # Require >= version 0.5.0. + raise ImportError +except ImportError: + from natsort.compat.fake_fastnumbers import ( + fast_float, + fast_int, + isint, + isfloat, + ) diff --git a/natsort/compat/locale.py b/natsort/compat/locale.py new file mode 100644 index 0000000..618f666 --- /dev/null +++ b/natsort/compat/locale.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +from __future__ import ( + print_function, + division, + unicode_literals, + absolute_import +) + +# Std. lib imports +import sys + +# Local imports. +from natsort.compat.py23 import PY_VERSION, cmp_to_key + +# Make the strxfrm function from strcoll on Python2 +# It can be buggy (especially on BSD-based systems), +# so prefer PyICU if available. +try: + import PyICU + from locale import getlocale + + # If using PyICU, get the locale from the current global locale, + # then create a sort key from that + def get_pyicu_transform(l, _d={}): + if l not in _d: + if l == (None, None): + c = PyICU.Collator.createInstance(PyICU.Locale()) + else: + loc = '.'.join(l) + c = PyICU.Collator.createInstance(PyICU.Locale(loc)) + _d[l] = c.getSortKey + return _d[l] + use_pyicu = True + null_string = b'' + + def dumb_sort(): + return False +except ImportError: + if sys.version[0] == '2': + from locale import strcoll + strxfrm = cmp_to_key(strcoll) + null_string = strxfrm('') + else: + from locale import strxfrm + null_string = '' + use_pyicu = False + + # On some systems, locale is broken and does not sort in the expected + # order. We will try to detect this and compensate. + def dumb_sort(): + return strxfrm('A') < strxfrm('a') + + +if PY_VERSION >= 3.3: + def _low(x): + return x.casefold() +else: + def _low(x): + return x.lower() diff --git a/natsort/compat/pathlib.py b/natsort/compat/pathlib.py new file mode 100644 index 0000000..f0ab7eb --- /dev/null +++ b/natsort/compat/pathlib.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +from __future__ import ( + print_function, + division, + unicode_literals, + absolute_import +) + +try: + from pathlib import PurePath # PurePath is the base object for Paths. +except ImportError: # pragma: no cover + PurePath = object # To avoid NameErrors. + has_pathlib = False +else: + has_pathlib = True diff --git a/natsort/compat/py23.py b/natsort/compat/py23.py index 99d5ef7..358eecf 100644 --- a/natsort/compat/py23.py +++ b/natsort/compat/py23.py @@ -10,7 +10,8 @@ import functools import sys # These functions are used to make the doctests compatible between -# python2 and python3. This code is pretty much lifted from the iPython +# python2 and python3, and also provide uniform functionality between +# the two versions. This code is pretty much lifted from the iPython # project's py3compat.py file. Credit to the iPython devs. # Numeric form of version @@ -36,6 +37,42 @@ else: py23_zip = itertools.izip +# 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. def _modify_str_or_docstring(str_change_func): diff --git a/natsort/locale_help.py b/natsort/locale_help.py index 5a8933d..bf80168 100644 --- a/natsort/locale_help.py +++ b/natsort/locale_help.py @@ -12,94 +12,15 @@ from __future__ import ( ) # Std. lib imports. -import sys from itertools import chain from locale import localeconv # Local imports. -from natsort.compat.py23 import PY_VERSION - -# We need cmp_to_key for Python2 because strxfrm is broken for unicode. -try: - from functools import cmp_to_key -# cmp_to_key was not created till 2.7. -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 - -# Make the strxfrm function from strcoll on Python2 -# It can be buggy (especially on BSD-based systems), -# so prefer PyICU if available. -try: - import PyICU - from locale import getlocale - - # If using PyICU, get the locale from the current global locale, - # then create a sort key from that - def get_pyicu_transform(l, _d={}): - if l not in _d: - if l == (None, None): - c = PyICU.Collator.createInstance(PyICU.Locale()) - else: - loc = '.'.join(l) - c = PyICU.Collator.createInstance(PyICU.Locale(loc)) - _d[l] = c.getSortKey - return _d[l] - use_pyicu = True - null_string = b'' - - def dumb_sort(): - return False -except ImportError: - if sys.version[0] == '2': - from locale import strcoll - strxfrm = cmp_to_key(strcoll) - null_string = strxfrm('') - else: - from locale import strxfrm - null_string = '' - use_pyicu = False - - # On some systems, locale is broken and does not sort in the expected - # order. We will try to detect this and compensate. - def dumb_sort(): - return strxfrm('A') < strxfrm('a') - - -if PY_VERSION >= 3.3: - def _low(x): - return x.casefold() +from natsort.compat.locale import use_pyicu, _low +if use_pyicu: + from natsort.compat.locale import get_pyicu_transform, getlocale else: - def _low(x): - return x.lower() + from natsort.compat.locale import strxfrm def groupletters(x): diff --git a/natsort/natsort.py b/natsort/natsort.py index 43f4f63..6f90bf9 100644 --- a/natsort/natsort.py +++ b/natsort/natsort.py @@ -10,7 +10,6 @@ descend into lists of lists so you can sort by the sublist contents. See the README or the natsort homepage for more details. """ - from __future__ import ( print_function, division, diff --git a/natsort/ns_enum.py b/natsort/ns_enum.py index a032a50..fdaaf88 100644 --- a/natsort/ns_enum.py +++ b/natsort/ns_enum.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- """This module defines the "ns" enum for natsort.""" - from __future__ import ( print_function, division, diff --git a/natsort/unicode_numbers.py b/natsort/unicode_numbers.py index e4e9ebf..c93ed56 100644 --- a/natsort/unicode_numbers.py +++ b/natsort/unicode_numbers.py @@ -2,7 +2,6 @@ """ Contains all possible non-ASCII unicode numbers. """ - from __future__ import ( print_function, division, diff --git a/natsort/utils.py b/natsort/utils.py index 86efa00..97dfbc1 100644 --- a/natsort/utils.py +++ b/natsort/utils.py @@ -3,7 +3,6 @@ Utilities and definitions for natsort, mostly all used to define the _natsort_key function. """ - from __future__ import ( print_function, division, @@ -23,39 +22,24 @@ from locale import localeconv # Local imports. from natsort.ns_enum import ns, _ns from natsort.unicode_numbers import digits, numeric -from natsort.locale_help import ( - locale_convert, - grouper, - null_string, - use_pyicu, - dumb_sort, -) +from natsort.locale_help import locale_convert, grouper +from natsort.compat.pathlib import PurePath, has_pathlib from natsort.compat.py23 import ( py23_str, py23_zip, PY_VERSION, ) - -# 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, isint, isfloat - import fastnumbers - v = list(map(int, fastnumbers.__version__.split('.'))) - if not (v[0] >= 0 and v[1] >= 5): # Require >= version 0.5.0. - raise ImportError -except ImportError: - from natsort.fake_fastnumbers import fast_float, fast_int, isint, isfloat - -# If the user has pathlib installed, the ns.PATH option will convert -# Path objects to str before sorting. -try: - from pathlib import PurePath # PurePath is the base object for Paths. -except ImportError: # pragma: no cover - PurePath = object # To avoid NameErrors. - has_pathlib = False -else: - has_pathlib = True +from natsort.compat.locale import ( + dumb_sort, + use_pyicu, + null_string, +) +from natsort.compat.fastnumbers import ( + fast_float, + fast_int, + isint, + isfloat, +) # Group algorithm types for easy extraction _NUMBER_ALGORITHMS = ns.FLOAT | ns.INT | ns.UNSIGNED | ns.SIGNED | ns.NOEXP @@ -8,6 +8,7 @@ formats = zip,gztar flakes-ignore = natsort/compat/py23.py UndefinedName natsort/__init__.py UnusedImport + natsort/compat/* UnusedImport docs/source/conf.py ALL test_natsort/test_natsort.py UnusedImport RedefinedWhileUnused test_natsort/test_locale_help.py UnusedImport RedefinedWhileUnused diff --git a/test_natsort/test_fake_fastnumbers.py b/test_natsort/test_fake_fastnumbers.py index a7450ff..594d060 100644 --- a/test_natsort/test_fake_fastnumbers.py +++ b/test_natsort/test_fake_fastnumbers.py @@ -8,7 +8,7 @@ import pytest import unicodedata from math import isnan from natsort.compat.py23 import py23_str -from natsort.fake_fastnumbers import ( +from natsort.compat.fake_fastnumbers import ( fast_float, fast_int, isfloat, diff --git a/test_natsort/test_locale_help.py b/test_natsort/test_locale_help.py index 370edaa..63ff96b 100644 --- a/test_natsort/test_locale_help.py +++ b/test_natsort/test_locale_help.py @@ -8,13 +8,10 @@ import locale import pytest from math import isnan from itertools import chain +from natsort.compat.fake_fastnumbers import fast_float, isfloat +from natsort.locale_help import grouper, locale_convert from natsort.compat.py23 import py23_str -from natsort.fake_fastnumbers import fast_float, isfloat -from natsort.locale_help import ( - grouper, - locale_convert, - use_pyicu, -) +from natsort.compat.locale import use_pyicu from compat.locale import ( load_locale, has_locale_de_DE, diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py index d89d4fe..b974fb3 100644 --- a/test_natsort/test_utils.py +++ b/test_natsort/test_utils.py @@ -11,7 +11,6 @@ from math import isnan from operator import itemgetter from itertools import chain from pytest import raises -from natsort.compat.py23 import py23_str from natsort.ns_enum import ns from natsort.utils import ( _number_extracter, @@ -28,12 +27,18 @@ from natsort.utils import ( _path_splitter, _fix_nan, ) -from natsort.locale_help import ( +from natsort.locale_help import locale_convert +from natsort.compat.py23 import py23_str +from natsort.compat.locale import ( use_pyicu, null_string, - locale_convert, dumb_sort, ) +from natsort.compat.fastnumbers import ( + fast_float, + fast_int, + isint, +) from slow_splitters import ( int_splitter, float_splitter, @@ -52,16 +57,6 @@ from compat.hypothesis import ( use_hypothesis, ) - -try: - from fastnumbers import fast_float, fast_int, isint - import fastnumbers - v = list(map(int, fastnumbers.__version__.split('.'))) - if not (v[0] >= 0 and v[1] >= 5): # Require >= version 0.5.0. - raise ImportError -except ImportError: - from natsort.fake_fastnumbers import fast_float, fast_int, isint - if sys.version[0] == '3': long = int |
