summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2016-04-18 20:22:27 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2016-04-18 22:29:33 -0700
commitddd811bfe0f39ddf32eb15ab6e3d6abe5eac8988 (patch)
tree9218c6b3074473648833c7d3cbb6747ba27aaf60
parentc911e4a65407949027183a690a4449ca764c9ee5 (diff)
downloadnatsort-ddd811bfe0f39ddf32eb15ab6e3d6abe5eac8988.tar.gz
Changed _ungroupletters into a factory function.
It is now called _post_string_parse_function.
-rw-r--r--natsort/utils.py42
-rw-r--r--test_natsort/test_utils.py36
2 files changed, 48 insertions, 30 deletions
diff --git a/natsort/utils.py b/natsort/utils.py
index 16a19a1..03968c2 100644
--- a/natsort/utils.py
+++ b/natsort/utils.py
@@ -178,11 +178,8 @@ def _natsort_key(val, key, alg):
# Handle NaN.
if any(x != x for x in ret):
ret = _fix_nan(ret, alg)
- if use_locale and alg & ns.UNGROUPLETTERS:
- val = orig_val if (alg & ns._DUMB) else val
- return _ungroupletters(ret, val, null_string, alg)
- else:
- return ret
+ val = orig_val if (alg & ns._DUMB) else val
+ return _post_string_parse_function(alg, null_string)(ret, val)
except (TypeError, AttributeError):
# Check if it is a bytes type, and if so return as a
# one element tuple.
@@ -326,21 +323,32 @@ def _post_split_function(alg):
return partial(fast_int, **kwargs)
-def _ungroupletters(split_val, val, sep, alg):
+def _post_string_parse_function(alg, sep):
"""
- Return a tuple with the first character of the first element
- of the return value as the first element, and the return value
- as the second element. This will be used to perform gross sorting
- by the first letter.
+ Given a set of natsort algorithms, return the function to operate
+ on the post-parsed strings according to the user's request.
"""
- if not split_val:
- return ((), ())
- elif split_val[0] == sep:
- return ((b'' if use_pyicu else '',), split_val)
- elif alg & ns._DUMB and alg & ns.LOWERCASEFIRST:
- return ((val[0].swapcase(),), split_val)
+ if alg & ns.UNGROUPLETTERS and alg & ns.LOCALE:
+ swap = alg & ns._DUMB and alg & ns.LOWERCASEFIRST
+ def func(split_val,
+ val,
+ f=(lambda x: x.swapcase()) if swap else lambda x: x):
+ """
+ Return a tuple with the first character of the first element
+ of the return value as the first element, and the return value
+ as the second element. This will be used to perform gross sorting
+ by the first letter.
+ """
+ split_val = tuple(split_val)
+ if not split_val:
+ return ((), ())
+ elif split_val[0] == sep:
+ return ((b'' if use_pyicu else '',), split_val)
+ else:
+ return ((f(val[0]),), split_val)
+ return func
else:
- return ((val[0],), split_val)
+ return lambda split_val, val: tuple(split_val)
def chain_functions(functions):
diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py
index d94b648..5156dff 100644
--- a/test_natsort/test_utils.py
+++ b/test_natsort/test_utils.py
@@ -31,7 +31,7 @@ from natsort.utils import (
_parse_bytes_function,
_pre_split_function,
_post_split_function,
- _ungroupletters,
+ _post_string_parse_function,
)
from natsort.locale_help import locale_convert, groupletters
from natsort.compat.py23 import py23_str
@@ -333,38 +333,48 @@ def test_post_split_function_with_LOCALE_and_DUMB_returns_fast_int_and_grouplett
raise
-def test_ungroupletters_with_empty_tuple_returns_double_empty_tuple():
- assert _ungroupletters((), '', '', 0) == ((), ())
+def test_post_string_parse_function_with_iterable_returns_tuple_with_no_options_example():
+ assert _post_string_parse_function(0, '')(iter([7]), '') == (7, )
-def test_ungroupletters_with_null_string_first_element_adds_empty_string_on_first_tuple_element():
- assert _ungroupletters(('', 60), '', '', 0) == ((b'',) if use_pyicu else ('',), ('', 60))
+@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
+@given(text())
+def test_post_string_parse_function_with_iterable_returns_tuple_with_no_options(x):
+ assert _post_string_parse_function(0, '')(iter([x]), '') == (x, )
+
+
+def test_post_string_parse_function_with_empty_tuple_returns_double_empty_tuple():
+ assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')((), '') == ((), ())
+
+
+def test_post_string_parse_function_with_null_string_first_element_adds_empty_string_on_first_tuple_element():
+ assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')(('', 60), '') == ((b'',) if use_pyicu else ('',), ('', 60))
-def test_ungroupletters_returns_first_element_in_first_tuple_element_example():
- assert _ungroupletters(('this', 60), 'this60', '', 0) == (('t',), ('this', 60))
+def test_post_string_parse_function_returns_first_element_in_first_tuple_element_example():
+ assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')(('this', 60), 'this60') == (('t',), ('this', 60))
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@given(x=text(), y=floats() | integers())
-def test_ungroupletters_returns_first_element_in_first_tuple_element(x, y):
+def test_post_string_parse_function_returns_first_element_in_first_tuple_element(x, y):
assume(x)
assume(not isnan(y))
assume(not isinf(y))
- assert _ungroupletters((x, y), ''.join(map(py23_str, [x, y])), '', 0) == ((x[0],), (x, y))
+ assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')((x, y), ''.join(map(py23_str, [x, y]))) == ((x[0],), (x, y))
-def test_ungroupletters_returns_first_element_in_first_tuple_element_caseswapped_with_DUMB_and_LOWERCASEFIRST_example():
- assert _ungroupletters(('this', 60), 'this60', '', ns._DUMB | ns.LOWERCASEFIRST) == (('T',), ('this', 60))
+def test_post_string_parse_function_returns_first_element_in_first_tuple_element_caseswapped_with_DUMB_and_LOWERCASEFIRST_example():
+ assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS | ns._DUMB | ns.LOWERCASEFIRST, '')(('this', 60), 'this60') == (('T',), ('this', 60))
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@given(x=text(), y=floats() | integers())
-def test_ungroupletters_returns_first_element_in_first_tuple_element_caseswapped_with_DUMB_and_LOWERCASEFIRST(x, y):
+def test_post_string_parse_function_returns_first_element_in_first_tuple_element_caseswapped_with_DUMB_and_LOWERCASEFIRST(x, y):
assume(x)
assume(not isnan(y))
assume(not isinf(y))
- assert _ungroupletters((x, y), ''.join(map(py23_str, [x, y])), '', ns._DUMB | ns.LOWERCASEFIRST) == ((x[0].swapcase(),), (x, y))
+ assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS | ns._DUMB | ns.LOWERCASEFIRST, '')((x, y), ''.join(map(py23_str, [x, y]))) == ((x[0].swapcase(),), (x, y))
def test_parse_number_function_makes_function_that_returns_tuple_example():