def test_locale_convert_transforms_float_string_to_float_with_de_locale(x): assume(not isnan(x)) load_locale('de_DE') assert locale_convert(repr(x), (fast_float, isfloat), False) == x assert locale_convert( repr(x).replace('.', ','), (fast_float, isfloat), False) == x locale.setlocale(locale.LC_NUMERIC, 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_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 test__natsort_key_with_LOCALE_and_UNGROUPLETTERS_places_space_before_string_with_capital_first_letter( x): # Locale aware sorting assume(not any(type(y) == float and isnan(y) for y in x)) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) load_locale('en_US') if dumb_sort(): t = tuple( _number_extracter(s.swapcase(), _float_nosign_exp_re, *float_nosafe_locale_group)) else: t = tuple( _number_extracter(s, _float_nosign_exp_re, *float_nosafe_locale_nogroup)) if not t: r = (t, t) elif t[0] in (null_string, get_strxfrm()(b'\x00') if sys.version[0] == '2' and not use_pyicu else null_string): r = ((b'' if use_pyicu else '', ), t) else: r = ((s[0], ), t) assert _natsort_key(s, None, ns.LOCALE | ns.UNGROUPLETTERS | ns.F) == r # The below are all aliases for UNGROUPLETTERS assert ns.UNGROUPLETTERS == ns.UG assert ns.UNGROUPLETTERS == ns.CAPITALFIRST assert ns.UNGROUPLETTERS == ns.C locale.setlocale(locale.LC_NUMERIC, str(''))
def test_natsorted_with_LOCALE_and_mixed_input_returns_sorted_results_without_error(): load_locale('en_US') a = ['0', 'Á', '2', 'Z'] assert natsorted(a) == ['0', '2', 'Z', 'Á'] a = ['2', 'ä', 'b', 1.5, 3] assert natsorted(a, alg=ns.LOCALE) == [1.5, '2', 3, 'ä', 'b'] locale.setlocale(locale.LC_ALL, str(''))
def test_input_string_transform_factory_removes_thousands_separator_and_is_float_aware_with_LOCALE_and_FLOAT( x, y): load_locale('en_US') t = ''.join(map(methodcaller('rstrip', 'lL'), map(str, map(abs, x)))) # Remove negative signs trailing L s = '' for i, z in enumerate(reversed(t), 1): s = z + s if i % 3 == 0 and i != len(t): s = ',' + s u = ''.join(map(methodcaller('rstrip', 'lL'), map(str, map(abs, y)))) # Remove negative signs trailing L v = '' for i, z in enumerate(reversed(u), 1): v = z + v if i % 3 == 0 and i != len(u): v = ',' + v # Remove all but first comma. a = v.split(',', 1) p = a[0] + ',' + a[1].replace(',', '') assert _input_string_transform_factory(ns.LOCALE)('.'.join( [s, v])) == '.'.join([t, u]) assert _input_string_transform_factory(ns.LOCALE | ns.FLOAT)('.'.join( [s, v])) == '.'.join([t, p]) locale.setlocale(locale.LC_ALL, str(''))
def test_pre_split_function_replaces_decimal_separator_with_LOCALE_example(): load_locale('de_DE') x = '1543,753' assert _pre_split_function(ns.LOCALE)(x) == '1543,753' # Does nothing without FLOAT assert _pre_split_function(ns.LOCALE | ns.FLOAT)(x) == '1543.753' assert _pre_split_function(ns.LOCALEALPHA)(x) == '1543,753' # LOCALEALPHA doesn't do anything... need LOCALENUM locale.setlocale(locale.LC_ALL, str(''))
def test_pre_split_function_removes_thousands_separator_with_LOCALE_example(): load_locale('en_US') x = '12,543,642,642.534,534,980' # Without FLOAT it does not account for decimal. assert _pre_split_function(ns.LOCALE)(x) == '12543642642.534534980' x = '12,543,642,642.534,534,980' # LOCALEALPHA doesn't do anything... need LOCALENUM assert _pre_split_function(ns.LOCALEALPHA)(x) == '12,543,642,642.534,534,980' locale.setlocale(locale.LC_ALL, str(''))
def test_input_string_transform_factory_does_not_replace_invalid_decimal_separator_with_LOCALE_example( ): load_locale('de_DE') x = '154s,t53' assert _input_string_transform_factory(ns.LOCALE | ns.FLOAT)(x) == '154s,t53' locale.setlocale(locale.LC_ALL, str(''))
def test_natsorted_with_LOCALE_and_mixed_input_returns_sorted_results_without_error( ): load_locale('en_US') a = ['0', 'Á', '2', 'Z'] assert natsorted(a) == ['0', '2', 'Z', 'Á'] a = ['2', 'ä', 'b', 1.5, 3] assert natsorted(a, alg=ns.LOCALE) == [1.5, '2', 3, 'ä', 'b'] locale.setlocale(locale.LC_ALL, str(''))
def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters( x): assume(type(fast_float(x)) is not float) load_locale('en_US') strxfrm = get_strxfrm() assert locale_convert(x, (fast_float, isfloat), True) == strxfrm(''.join( chain.from_iterable([low(y), y] for y in x))) locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_transforms_nonfloat_string_to_strxfrm_string_example(): load_locale('en_US') strxfrm = get_strxfrm() assert locale_convert('45,8', (fast_float, isfloat), False) == strxfrm('45,8') assert locale_convert('hello', (fast_float, isfloat), False) == strxfrm('hello') locale.setlocale(locale.LC_NUMERIC, str(''))
def test_natsorted_with_LOCALE_and_CAPITALFIRST_returns_results_sorted_by_capital_first_and_ungrouped( ): a = ['Apple', 'corn', 'Corn', 'Banana', 'apple', 'banana'] load_locale('en_US') assert natsorted(a, alg=ns.LOCALE | ns.CAPITALFIRST) == [ 'Apple', 'Banana', 'Corn', 'apple', 'banana', 'corn' ] locale.setlocale(locale.LC_ALL, str(''))
def test_natsorted_with_LOCALE_and_LOWERCASEFIRST_returns_results_sorted_by_uppercase_first_and_grouped_letters( ): a = ['Apple', 'corn', 'Corn', 'Banana', 'apple', 'banana'] load_locale('en_US') assert natsorted(a, alg=ns.LOCALE | ns.LOWERCASEFIRST) == [ 'Apple', 'apple', 'Banana', 'banana', 'Corn', 'corn' ] locale.setlocale(locale.LC_ALL, str(''))
def test_natsorted_with_LOCALE_and_de_setting_returns_results_sorted_by_de_language( ): load_locale('de_DE') a = ['c', 'a5.467,86', 'ä', 'b', 'a5367.86', 'a5,6', 'a5,50'] assert natsorted(a, alg=ns.LOCALE | ns.F) == [ 'a5,50', 'a5,6', 'a5367.86', 'a5.467,86', 'ä', 'b', 'c' ] locale.setlocale(locale.LC_ALL, str(''))
def test_pre_split_function_leaves_invalid_thousands_separator_with_LOCALE_example(): load_locale('en_US') x = '12,543,642642.5345,34980' assert _pre_split_function(ns.LOCALE)(x) == '12543,642642.5345,34980' x = '12,59443,642,642.53,4534980' assert _pre_split_function(ns.LOCALE)(x) == '12,59443,642642.53,4534980' x = '12543,642,642.5,34534980' assert _pre_split_function(ns.LOCALE)(x) == '12543,642642.5,34534980' 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_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_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters_example( ): load_locale('en_US') strxfrm = get_strxfrm() assert locale_convert('hello', (fast_float, isfloat), True) == strxfrm('hheelllloo') assert locale_convert('45,8', (fast_float, isfloat), True) == strxfrm('4455,,88') locale.setlocale(locale.LC_NUMERIC, str(''))
def test_pre_split_function_removes_thousands_separator_with_LOCALE(x): load_locale('en_US') t = ''.join(map(methodcaller('rstrip', 'lL'), map(str, map(abs, x)))) # Remove negative signs trailing L s = '' for i, y in enumerate(reversed(t), 1): s = y + s if i % 3 == 0 and i != len(t): s = ',' + s assert _pre_split_function(ns.LOCALE)(s) == t locale.setlocale(locale.LC_ALL, str(''))
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters(x): load_locale('en_US') s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) t = int_splitter(s, False, False, null_string) try: # Account for locale bug on Python 3.2 t = [y if i == 0 and y is null_string else locale_convert(y, (fast_int, isint), True) for i, y in enumerate(t)] assert _number_extracter(s, _int_nosign_re, *int_nosafe_locale_group) == t except OverflowError: pass locale.setlocale(locale.LC_NUMERIC, str(''))
def test__natsort_key_with_LOCALE_transforms_floats_according_to_the_current_locale_and_strxfrms_strings(x): # Locale aware sorting assume(not any(type(y) == float and isnan(y) for y in x)) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) load_locale('en_US') if dumb_sort(): assert _natsort_key(s, None, ns.LOCALE | ns.F) == tuple(_number_extracter(s.swapcase(), _float_nosign_exp_re, *float_nosafe_locale_group)) else: assert _natsort_key(s, None, ns.LOCALE | ns.F) == tuple(_number_extracter(s, _float_nosign_exp_re, *float_nosafe_locale_nogroup)) locale.setlocale(locale.LC_NUMERIC, str(''))
def test_input_string_transform_factory_removes_thousands_separator_with_LOCALE_example( ): load_locale('en_US') x = '12,543,642,642.534,534,980' # Without FLOAT it does not account for decimal. assert _input_string_transform_factory( ns.LOCALE)(x) == '12543642642.534534980' x = '12,543,642,642.534,534,980' # LOCALEALPHA doesn't do anything... need LOCALENUM assert _input_string_transform_factory( ns.LOCALEALPHA)(x) == '12,543,642,642.534,534,980' locale.setlocale(locale.LC_ALL, str(''))
def test_input_string_transform_factory_replaces_decimal_separator_with_LOCALE_example( ): load_locale('de_DE') x = '1543,753' assert _input_string_transform_factory( ns.LOCALE)(x) == '1543,753' # Does nothing without FLOAT assert _input_string_transform_factory(ns.LOCALE | ns.FLOAT)(x) == '1543.753' assert _input_string_transform_factory(ns.LOCALEALPHA)( x) == '1543,753' # LOCALEALPHA doesn't do anything... need LOCALENUM locale.setlocale(locale.LC_ALL, str(''))
def test_input_string_transform_factory_removes_thousands_separator_with_LOCALE( x): load_locale('en_US') t = ''.join(map(methodcaller('rstrip', 'lL'), map(str, map(abs, x)))) # Remove negative signs trailing L s = '' for i, y in enumerate(reversed(t), 1): s = y + s if i % 3 == 0 and i != len(t): s = ',' + s assert _input_string_transform_factory(ns.LOCALE)(s) == t locale.setlocale(locale.LC_ALL, str(''))
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters_example( ): load_locale('en_US') strxfrm = get_strxfrm() assert _number_extracter('A5+5.034E-1', _int_nosign_re, *int_nosafe_locale_group) == [ strxfrm('aA'), 5, strxfrm('++'), 5, strxfrm('..'), 34, strxfrm('eE--'), 1 ] locale.setlocale(locale.LC_NUMERIC, 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_input_string_transform_factory_leaves_invalid_thousands_separator_with_LOCALE_example( ): load_locale('en_US') x = '12,543,642642.5345,34980' assert _input_string_transform_factory( ns.LOCALE)(x) == '12543,642642.5345,34980' x = '12,59443,642,642.53,4534980' assert _input_string_transform_factory( ns.LOCALE)(x) == '12,59443,642642.53,4534980' x = '12543,642,642.5,34534980' assert _input_string_transform_factory( ns.LOCALE)(x) == '12543,642642.5,34534980' locale.setlocale(locale.LC_ALL, str(''))
def test_natsorted_with_LOCALE_and_UNGROUPLETTERS_and_mixed_input_returns_sorted_results_without_error( ): load_locale('en_US') a = ['0', 'Á', '2', 'Z'] assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS) == ['0', '2', 'Á', 'Z'] assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS | ns.NUMAFTER) == ['Á', 'Z', '0', '2'] a = ['2', 'ä', 'b', 1.5, 3] assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS) == [1.5, '2', 3, 'ä', 'b'] assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS | ns.NUMAFTER) == ['ä', 'b', 1.5, '2', 3] locale.setlocale(locale.LC_ALL, str(''))
def test__natsort_key_with_LOCALE_transforms_floats_according_to_the_current_locale_and_strxfrms_strings( x): # Locale aware sorting assume(not any(type(y) == float and isnan(y) for y in x)) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) load_locale('en_US') if dumb_sort(): assert _natsort_key(s, None, ns.LOCALE | ns.F) == tuple( _number_extracter(s.swapcase(), _float_nosign_exp_re, *float_nosafe_locale_group)) else: assert _natsort_key(s, None, ns.LOCALE | ns.F) == tuple( _number_extracter(s, _float_nosign_exp_re, *float_nosafe_locale_nogroup)) locale.setlocale(locale.LC_NUMERIC, str(''))
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters( x): load_locale('en_US') s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) t = int_splitter(s, False, False, null_string) try: # Account for locale bug on Python 3.2 t = [ y if i == 0 and y is null_string else locale_convert( y, (fast_int, isint), True) for i, y in enumerate(t) ] assert _number_extracter(s, _int_nosign_re, *int_nosafe_locale_group) == t except OverflowError: pass locale.setlocale(locale.LC_NUMERIC, 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 test_pre_split_function_removes_thousands_separator_and_is_float_aware_with_LOCALE_and_FLOAT(x, y): load_locale('en_US') t = ''.join(map(methodcaller('rstrip', 'lL'), map(str, map(abs, x)))) # Remove negative signs trailing L s = '' for i, z in enumerate(reversed(t), 1): s = z + s if i % 3 == 0 and i != len(t): s = ',' + s u = ''.join(map(methodcaller('rstrip', 'lL'), map(str, map(abs, y)))) # Remove negative signs trailing L v = '' for i, z in enumerate(reversed(u), 1): v = z + v if i % 3 == 0 and i != len(u): v = ',' + v # Remove all but first comma. a = v.split(',', 1) p = a[0] + ',' + a[1].replace(',', '') assert _pre_split_function(ns.LOCALE)('.'.join([s, v])) == '.'.join([t, u]) assert _pre_split_function(ns.LOCALE | ns.FLOAT)('.'.join([s, v])) == '.'.join([t, p]) locale.setlocale(locale.LC_ALL, str(''))
def test__natsort_key_with_LOCALE_and_UNGROUPLETTERS_places_space_before_string_with_capital_first_letter(x): # Locale aware sorting assume(not any(type(y) == float and isnan(y) for y in x)) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) load_locale('en_US') if dumb_sort(): t = tuple(_number_extracter(s.swapcase(), _float_nosign_exp_re, *float_nosafe_locale_group)) else: t = tuple(_number_extracter(s, _float_nosign_exp_re, *float_nosafe_locale_nogroup)) if not t: r = (t, t) elif t[0] in (null_string, get_strxfrm()(b'\x00') if sys.version[0] == '2' and not use_pyicu else null_string): r = ((b'' if use_pyicu else '',), t) else: r = ((s[0],), t) assert _natsort_key(s, None, ns.LOCALE | ns.UNGROUPLETTERS | ns.F) == r # The below are all aliases for UNGROUPLETTERS assert ns.UNGROUPLETTERS == ns.UG assert ns.UNGROUPLETTERS == ns.CAPITALFIRST assert ns.UNGROUPLETTERS == ns.C locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_transforms_float_string_to_float(x): assume(not isnan(x)) load_locale('en_US') assert locale_convert(repr(x), (fast_float, isfloat), False) == x locale.setlocale(locale.LC_NUMERIC, str(''))
def test_natsorted_with_LOCALE_and_de_setting_returns_results_sorted_by_de_language(): load_locale('de_DE') a = ['c', 'ä', 'b', 'a5,6', 'a5,50'] assert natsorted(a, alg=ns.LOCALE | ns.F) == ['a5,50', 'a5,6', 'ä', 'b', 'c'] locale.setlocale(locale.LC_ALL, str(''))
def test_locale_convert_transforms_nonfloat_string_to_strxfrm_string(x): assume(type(fast_float(x)) is not float) load_locale('en_US') strxfrm = get_strxfrm() assert locale_convert(x, (fast_float, isfloat), False) == strxfrm(x) locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters_example(): load_locale('en_US') strxfrm = get_strxfrm() assert locale_convert('hello', (fast_float, isfloat), True) == strxfrm('hheelllloo') assert locale_convert('45,8', (fast_float, isfloat), True) == strxfrm('4455,,88') locale.setlocale(locale.LC_NUMERIC, str(''))
def test_natsorted_with_LOCALE_and_LOWERCASEFIRST_returns_results_sorted_by_uppercase_first_and_grouped_letters(): a = ['Apple', 'corn', 'Corn', 'Banana', 'apple', 'banana'] load_locale('en_US') assert natsorted(a, alg=ns.LOCALE | ns.LOWERCASEFIRST) == ['Apple', 'apple', 'Banana', 'banana', 'Corn', 'corn'] locale.setlocale(locale.LC_ALL, str(''))
def test_locale_convert_transforms_float_string_to_float_example(): load_locale('en_US') assert locale_convert('45.8', (fast_float, isfloat), False) == 45.8 locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters(x): assume(type(fast_float(x)) is not float) load_locale('en_US') strxfrm = get_strxfrm() assert locale_convert(x, (fast_float, isfloat), True) == strxfrm(''.join(chain.from_iterable([low(y), y] for y in x))) locale.setlocale(locale.LC_NUMERIC, str(''))
def test_pre_split_function_does_not_replace_invalid_decimal_separator_with_LOCALE_example(): load_locale('de_DE') x = '154s,t53' assert _pre_split_function(ns.LOCALE | ns.FLOAT)(x) == '154s,t53' locale.setlocale(locale.LC_ALL, str(''))
def test_locale_convert_transforms_float_string_to_float_with_de_locale(x): assume(not isnan(x)) load_locale('de_DE') assert locale_convert(repr(x), (fast_float, isfloat), False) == x assert locale_convert(repr(x).replace('.', ','), (fast_float, isfloat), False) == x locale.setlocale(locale.LC_NUMERIC, str(''))
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters_example(): load_locale('en_US') strxfrm = get_strxfrm() assert _number_extracter('A5+5.034E-1', _int_nosign_re, *int_nosafe_locale_group) == [strxfrm('aA'), 5, strxfrm('++'), 5, strxfrm('..'), 34, strxfrm('eE--'), 1] locale.setlocale(locale.LC_NUMERIC, str(''))
def test_natsorted_with_LOCALE_and_CAPITALFIRST_and_LOWERCASE_returns_results_sorted_by_capital_last_and_ungrouped(): a = ['Apple', 'corn', 'Corn', 'Banana', 'apple', 'banana'] load_locale('en_US') assert natsorted(a, alg=ns.LOCALE | ns.CAPITALFIRST | ns.LOWERCASEFIRST) == ['apple', 'banana', 'corn', 'Apple', 'Banana', 'Corn'] locale.setlocale(locale.LC_ALL, str(''))