diff options
author | Seth M Morton <seth.m.morton@gmail.com> | 2016-05-02 23:19:11 -0700 |
---|---|---|
committer | Seth M Morton <seth.m.morton@gmail.com> | 2016-05-02 23:20:59 -0700 |
commit | ca7d4fc9aa840ca47b46dfed5e31eaa261245226 (patch) | |
tree | 80afe6a433b7d751d199599747e70e24d113528f | |
parent | 4dc43b7fdc83449c435d4a1311f1a8e1166f2abd (diff) | |
download | natsort-ca7d4fc9aa840ca47b46dfed5e31eaa261245226.tar.gz |
locale_convert is now locale_convert_function.
It now returns the appropriate function for the given configuration
to do the locale conversion. No unnecessary if statements.
-rw-r--r-- | natsort/locale_help.py | 8 | ||||
-rw-r--r-- | natsort/utils.py | 4 | ||||
-rw-r--r-- | test_natsort/test_locale_help.py | 8 | ||||
-rw-r--r-- | test_natsort/test_utils.py | 27 |
4 files changed, 32 insertions, 15 deletions
diff --git a/natsort/locale_help.py b/natsort/locale_help.py index ee302df..c905f96 100644 --- a/natsort/locale_help.py +++ b/natsort/locale_help.py @@ -27,11 +27,11 @@ def groupletters(x): return ''.join(chain.from_iterable([_low(y), y] for y in x)) -def locale_convert(x): +def locale_convert_function(): """ - Use the appropriate locale tranformation function on the given input. + Return a function that will use the appropriate locale tranformation. """ if use_pyicu: - return get_pyicu_transform(getlocale())(x) + return get_pyicu_transform(getlocale()) else: - return strxfrm(x) + return strxfrm diff --git a/natsort/utils.py b/natsort/utils.py index ff0bde3..aa16db0 100644 --- a/natsort/utils.py +++ b/natsort/utils.py @@ -25,7 +25,7 @@ from operator import methodcaller # Local imports. from natsort.ns_enum import ns from natsort.unicode_numbers import digits, numeric -from natsort.locale_help import locale_convert, groupletters +from natsort.locale_help import locale_convert_function, groupletters from natsort.compat.pathlib import PurePath, has_pathlib from natsort.compat.py23 import ( py23_str, @@ -317,7 +317,7 @@ def _post_split_function(alg): if group_letters: func_chain.append(groupletters) if use_locale: - func_chain.append(locale_convert) + func_chain.append(locale_convert_function()) kwargs = {'key': chain_functions(func_chain)} if func_chain else {} # Return the correct chained functions. diff --git a/test_natsort/test_locale_help.py b/test_natsort/test_locale_help.py index 240bfc5..3b8148d 100644 --- a/test_natsort/test_locale_help.py +++ b/test_natsort/test_locale_help.py @@ -9,7 +9,7 @@ import pytest from math import isnan from itertools import chain from natsort.compat.fake_fastnumbers import fast_float -from natsort.locale_help import groupletters, locale_convert +from natsort.locale_help import groupletters, locale_convert_function from natsort.compat.py23 import py23_str from natsort.compat.locale import use_pyicu from compat.locale import ( @@ -48,8 +48,8 @@ def test_groupeletters_returns_letters_with_lowercase_transform_of_letter(x): def test_locale_convert_transforms_string_to_strxfrm_string_example(): load_locale('en_US') strxfrm = get_strxfrm() - assert locale_convert('45,8') == strxfrm('45,8') - assert locale_convert('hello') == strxfrm('hello') + assert locale_convert_function()('45,8') == strxfrm('45,8') + assert locale_convert_function()('hello') == strxfrm('hello') locale.setlocale(locale.LC_NUMERIC, str('')) @@ -61,5 +61,5 @@ def test_locale_convert_transforms_string_to_strxfrm_string(x): assume(not any(i in bad_uni_chars for i in x)) load_locale('en_US') strxfrm = get_strxfrm() - assert locale_convert(x) == strxfrm(x) + assert locale_convert_function()(x) == strxfrm(x) locale.setlocale(locale.LC_NUMERIC, str('')) diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py index e6c21c9..d56f0b4 100644 --- a/test_natsort/test_utils.py +++ b/test_natsort/test_utils.py @@ -34,7 +34,10 @@ from natsort.utils import ( _post_split_function, _post_string_parse_function, ) -from natsort.locale_help import locale_convert, groupletters +from natsort.locale_help import ( + locale_convert_function, + groupletters, +) from natsort.compat.py23 import py23_str from natsort.compat.locale import ( use_pyicu, @@ -69,6 +72,7 @@ from compat.hypothesis import ( binary, use_hypothesis, ) +from compat.locale import bad_uni_chars if sys.version[0] == '3': long = int @@ -292,9 +296,22 @@ def test_post_split_function_with_GROUPLETTERS_returns_fast_int_and_groupletters assert _post_split_function(ns.GROUPLETTERS)(x) == fast_int(x, key=groupletters) +def test_post_split_function_with_LOCALE_returns_fast_int_and_groupletters_example(): + x = 'hello' + assert _post_split_function(ns.LOCALE)(x) == fast_int(x, key=locale_convert_function()) + + +@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') +@given(text()) +def test_post_split_function_with_LOCALE_returns_fast_int_and_groupletters(x): + assume(x) + assume(not any(y in bad_uni_chars for y in x)) + assert _post_split_function(ns.LOCALE)(x) == fast_int(x, key=locale_convert_function()) + + def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_groupletters_and_locale_convert_example(): x = 'hello' - assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert(groupletters(x))) + assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x))) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @@ -302,7 +319,7 @@ def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_g def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_groupletters_and_locale_convert(x): assume(x) try: - assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert(groupletters(x))) + assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x))) except ValueError as e: # handle broken locale lib on BSD. if 'is not in range' not in str(e): raise @@ -310,7 +327,7 @@ def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_g def test_post_split_function_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert_example(): x = 'hello' - assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert(groupletters(x))) + assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x))) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @@ -318,7 +335,7 @@ def test_post_split_function_with_LOCALE_and_DUMB_returns_fast_int_and_grouplett def test_post_split_function_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert(x): assume(x) try: - assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert(groupletters(x))) + assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x))) except ValueError as e: # handle broken locale lib on BSD. if 'is not in range' not in str(e): raise |