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_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_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_locale_convert_transforms_nonfloat_string_to_strxfrm_string(): locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8') if use_pyicu: from natsort.locale_help import get_pyicu_transform from locale import getlocale strxfrm = get_pyicu_transform(getlocale()) else: from natsort.locale_help import strxfrm assert locale_convert('45,8', fast_float, False) == strxfrm('45,8') assert locale_convert('hello', fast_float, False) == strxfrm('hello') locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters(): locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8') if use_pyicu: from natsort.locale_help import get_pyicu_transform from locale import getlocale strxfrm = get_pyicu_transform(getlocale()) else: from natsort.locale_help import strxfrm assert locale_convert('hello', fast_float, True) == strxfrm('hheelllloo') assert locale_convert('45,8', fast_float, True) == strxfrm('4455,,88') locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters( ): locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8') if use_pyicu: from natsort.locale_help import get_pyicu_transform from locale import getlocale strxfrm = get_pyicu_transform(getlocale()) else: from natsort.locale_help import strxfrm assert locale_convert('hello', fast_float, True) == strxfrm('hheelllloo') assert locale_convert('45,8', fast_float, True) == strxfrm('4455,,88') locale.setlocale(locale.LC_NUMERIC, str(''))
def _number_extracter(s, regex, numconv, py3_safe, use_locale, group_letters): """Helper to separate the string input into numbers and strings.""" conv_check = (numconv, _conv_to_check[numconv]) # Split the input string by numbers. # If the input is not a string, TypeError is raised. s = regex.split(s) # Now convert the numbers to numbers, and leave strings as strings. # Take into account locale if needed, and group letters if needed. # Remove empty strings from the list. if use_locale: s = [locale_convert(x, conv_check, group_letters) for x in s if x] elif group_letters: s = [grouper(x, conv_check) for x in s if x] else: s = [numconv(x) for x in s if x] # If the list begins with a number, lead with an empty string. # This is used to get around the "unorderable types" issue. if not s: # Return empty list for empty results. return [] elif conv_check[1](s[0], num_only=True): s = [null_string if use_locale else ''] + s # The _py3_safe function inserts "" between numbers in the list, # and is used to get around "unorderable types" in complex cases. # It is a separate function that needs to be requested specifically # because it is expensive to call. return _py3_safe(s, use_locale, conv_check[1]) if py3_safe else s
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters(x): assume(len(x) <= 10) 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) 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 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_number_extracter_extracts_numbers_and_strxfrms_strings_with_use_locale(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), False) for i, y in enumerate(t)] assert _number_extracter(s, _int_nosign_re, *int_nosafe_locale_nogroup) == t except OverflowError: pass 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_locale_convert(): locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8') if use_pyicu: from natsort.locale_help import get_pyicu_transform from locale import getlocale strxfrm = get_pyicu_transform(getlocale()) else: from natsort.locale_help import strxfrm assert locale_convert('45.8', fast_float, False) == 45.8 assert locale_convert('45,8', fast_float, False) == strxfrm('45,8') assert locale_convert('hello', fast_float, False) == strxfrm('hello') assert locale_convert('hello', fast_float, True) == strxfrm('hheelllloo') assert locale_convert('45,8', fast_float, True) == strxfrm('4455,,88') locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8') if use_pyicu: strxfrm = get_pyicu_transform(getlocale()) assert locale_convert('45.8', fast_float, False) == 45.8 assert locale_convert('45,8', fast_float, False) == 45.8 assert locale_convert('hello', fast_float, False) == strxfrm('hello') assert locale_convert('hello', fast_float, True) == strxfrm('hheelllloo') locale.setlocale(locale.LC_NUMERIC, '')
def test_locale_convert_transforms_float_string_to_float_with_de_locale(): locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8') assert locale_convert('45.8', fast_float, False) == 45.8 assert locale_convert('45,8', fast_float, False) == 45.8 locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_transforms_float_string_to_float(): locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8') assert locale_convert('45.8', fast_float, False) == 45.8 locale.setlocale(locale.LC_NUMERIC, 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_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_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_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_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_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(''))