def test_natsort_keygen_splits_input_with_locale_and_capitalfirst(): load_locale('en_US') strxfrm = get_strxfrm() with patch('natsort.compat.locale.dumb_sort', return_value=False): assert natsort_keygen(alg=ns.LA | ns.C)(INPUT) == ((('',), (null_string_locale, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1)), (('/',), (strxfrm('/Folder ('), 1, strxfrm(')/Foo'))), (('',), (null_string_locale, 56.7))) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA | ns.C)(b'6A-5.034e+1') == (b'6A-5.034e+1',) locale.setlocale(locale.LC_ALL, str(''))
def _string_component_transform_factory(alg): """ Given a set of natsort algorithms, return the function to operate on the post-split strings according to the user's request. """ # Shortcuts. use_locale = alg & ns.LOCALEALPHA dumb = alg & ns._DUMB group_letters = (alg & ns.GROUPLETTERS) or (use_locale and dumb) nan_val = float('+inf') if alg & ns.NANLAST else float('-inf') # Build the chain of functions to execute in order. func_chain = [] if group_letters: func_chain.append(_groupletters) if use_locale: func_chain.append(get_strxfrm()) kwargs = {'key': chain_functions(func_chain)} if func_chain else {} # Return the correct chained functions. if alg & ns.FLOAT: kwargs['nan'] = nan_val return partial(fast_float, **kwargs) else: return partial(fast_int, **kwargs)
def test_string_component_transform_factory_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert( x): try: assert _string_component_transform_factory(ns._DUMB | ns.LOCALE)( x) == fast_int(x, key=lambda x: get_strxfrm()(_groupletters(x))) except ValueError as e: # handle broken locale lib on BSD. if 'is not in range' not in str(e): raise
def test_string_component_transform_factory_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert_example( ): x = 'hello' assert _string_component_transform_factory(ns._DUMB | ns.LOCALE)(x) == fast_int( x, key=lambda x: get_strxfrm() (_groupletters(x)))
def test_natsort_keygen_splits_input_with_locale(): load_locale('en_US') strxfrm = get_strxfrm() with patch('natsort.compat.locale.dumb_sort', return_value=False): assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string_locale, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1), (strxfrm('/Folder ('), 1, strxfrm(')/Foo')), (null_string_locale, 56.7)) with patch('natsort.compat.locale.dumb_sort', return_value=True): assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string_locale, 6, strxfrm('aa--'), 5, strxfrm('..'), 34, strxfrm('eE++'), 1), (strxfrm('//ffoOlLdDeErR (('), 1, strxfrm('))//ffoOoO')), (null_string_locale, 56.7)) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA)(b'6A-5.034e+1') == (b'6A-5.034e+1',) locale.setlocale(locale.LC_ALL, str(''))
def test_natsort_keygen_splits_input_with_locale_and_capitalfirst(): load_locale("en_US") strxfrm = get_strxfrm() with patch("natsort.compat.locale.dumb_sort", return_value=False): assert natsort_keygen(alg=ns.LA | ns.C)(INPUT) == ( (("",), (null_string, 6, strxfrm("A-"), 5, strxfrm("."), 34, strxfrm("e+"), 1)), (("/",), (strxfrm("/Folder ("), 1, strxfrm(")/Foo"))), (("",), (null_string, 56.7)), ) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA | ns.C)(b"6A-5.034e+1") == (b"6A-5.034e+1",) locale.setlocale(locale.LC_ALL, str(""))
def test_natsort_keygen_splits_input_with_locale(): load_locale("en_US") strxfrm = get_strxfrm() with patch("natsort.compat.locale.dumb_sort", return_value=False): assert natsort_keygen(alg=ns.L)(INPUT) == ( (null_string, 6, strxfrm("A-"), 5, strxfrm("."), 34, strxfrm("e+"), 1), (strxfrm("/Folder ("), 1, strxfrm(")/Foo")), (null_string, 56.7), ) with patch("natsort.compat.locale.dumb_sort", return_value=True): assert natsort_keygen(alg=ns.L)(INPUT) == ( (null_string, 6, strxfrm("aa--"), 5, strxfrm(".."), 34, strxfrm("eE++"), 1), (strxfrm("//ffoOlLdDeErR (("), 1, strxfrm("))//ffoOoO")), (null_string, 56.7), ) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA)(b"6A-5.034e+1") == (b"6A-5.034e+1",) locale.setlocale(locale.LC_ALL, str(""))
def string_component_transform_factory(alg): """ Create a function to either transform a string or convert to a number. Parameters ---------- alg : ns enum Indicate how to format the *str*. Returns ------- func : callable A function to be used as the *component_transform* argument to *parse_string_factory*. See Also -------- parse_string_factory """ # Shortcuts. use_locale = alg & ns.LOCALEALPHA dumb = alg & NS_DUMB group_letters = (alg & ns.GROUPLETTERS) or (use_locale and dumb) nan_val = float("+inf") if alg & ns.NANLAST else float("-inf") # Build the chain of functions to execute in order. func_chain = [] if group_letters: func_chain.append(groupletters) if use_locale: func_chain.append(get_strxfrm()) kwargs = {"key": chain_functions(func_chain)} if func_chain else {} # Return the correct chained functions. if alg & ns.FLOAT: # noinspection PyTypeChecker kwargs["nan"] = nan_val return partial(fast_float, **kwargs) else: return partial(fast_int, **kwargs)
def test_natsort_keygen_with_locale(mocker, arbitrary_input, alg, expected, is_dumb): # First, apply the correct strxfrm function to the string values. strxfrm = get_strxfrm() expected = [list(sub) for sub in expected] try: for i in (2, 4, 6): expected[0][i] = strxfrm(expected[0][i]) for i in (0, 2): expected[1][i] = strxfrm(expected[1][i]) expected = tuple(tuple(sub) for sub in expected) except IndexError: # ns.LOCALE | ns.CAPITALFIRST expected = [[list(subsub) for subsub in sub] for sub in expected] for i in (2, 4, 6): expected[0][1][i] = strxfrm(expected[0][1][i]) for i in (0, 2): expected[1][1][i] = strxfrm(expected[1][1][i]) expected = tuple(tuple(tuple(subsub) for subsub in sub) for sub in expected) with mocker.patch("natsort.compat.locale.dumb_sort", return_value=is_dumb): ns_key = natsort_keygen(alg=alg) assert ns_key(arbitrary_input) == expected
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=get_strxfrm())
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=get_strxfrm())
def test_string_component_transform_factory_with_LOCALE_returns_fast_int_and_groupletters( x): assert _string_component_transform_factory(ns.LOCALE)(x) == fast_int( x, key=get_strxfrm())
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: get_strxfrm()(_groupletters(x)))
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: get_strxfrm()(_groupletters(x))) except ValueError as e: # handle broken locale lib on BSD. if 'is not in range' not in str(e): raise
def no_null(x): """Ensure text does not contain a null character.""" return "\0" not in x @pytest.mark.parametrize( "alg, example_func", [ (ns.INT, fast_int), (ns.DEFAULT, fast_int), (ns.FLOAT, partial(fast_float, nan=float("-inf"))), (ns.FLOAT | ns.NANLAST, partial(fast_float, nan=float("+inf"))), (ns.GROUPLETTERS, partial(fast_int, key=groupletters)), (ns.LOCALE, partial(fast_int, key=lambda x: get_strxfrm()(x))), ( ns.GROUPLETTERS | ns.LOCALE, partial(fast_int, key=lambda x: get_strxfrm()(groupletters(x))), ), ( NS_DUMB | ns.LOCALE, partial(fast_int, key=lambda x: get_strxfrm()(groupletters(x))), ), ( ns.GROUPLETTERS | ns.LOCALE | ns.FLOAT | ns.NANLAST, partial( fast_float, key=lambda x: get_strxfrm()(groupletters(x)), nan=float("+inf"), ),
def test_string_component_transform_factory_with_LOCALE_returns_fast_int_and_groupletters( x): assume(x) assume(not any(y in bad_uni_chars for y in x)) assert _string_component_transform_factory(ns.LOCALE)(x) == fast_int( x, key=get_strxfrm())