我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用locale.localeconv()。
def _group_lengths(grouping): """Convert a localeconv-style grouping into a (possibly infinite) iterable of integers representing group lengths. """ # The result from localeconv()['grouping'], and the input to this # function, should be a list of integers in one of the # following three forms: # # (1) an empty list, or # (2) nonempty list of positive integers + [0] # (3) list of positive integers + [locale.CHAR_MAX], or from itertools import chain, repeat if not grouping: return [] elif grouping[-1] == 0 and len(grouping) >= 2: return chain(grouping[:-1], repeat(grouping[-2])) elif grouping[-1] == _locale.CHAR_MAX: return grouping[:-1] else: raise ValueError('unrecognised format for grouping')
def float(self, val): """ Parse a string to a floating point number. Uses locale.atof(), in future with ICU present will use icu.NumberFormat.parse(). """ try: return locale.atof(val) except ValueError: point = locale.localeconv()['decimal_point'] sep = locale.localeconv()['thousands_sep'] try: if point == ',': return locale.atof(val.replace(' ', sep).replace('.', sep)) elif point == '.': return locale.atof(val.replace(' ', sep).replace(',', sep)) else: return None except ValueError: return None #------------------------------------------------------------------------- # # Translations Classes # #-------------------------------------------------------------------------
def test_float_with_comma(self): # set locale to something that doesn't use '.' for the decimal point # float must not accept the locale specific decimal point but # it still has to accept the normal python syntax import locale if not locale.localeconv()['decimal_point'] == ',': return self.assertEqual(float(" 3.14 "), 3.14) self.assertEqual(float("+3.14 "), 3.14) self.assertEqual(float("-3.14 "), -3.14) self.assertEqual(float(".14 "), .14) self.assertEqual(float("3. "), 3.0) self.assertEqual(float("3.e3 "), 3000.0) self.assertEqual(float("3.2e3 "), 3200.0) self.assertEqual(float("2.5e-1 "), 0.25) self.assertEqual(float("5e-1"), 0.5) self.assertRaises(ValueError, float, " 3,14 ") self.assertRaises(ValueError, float, " +3,14 ") self.assertRaises(ValueError, float, " -3,14 ") self.assertRaises(ValueError, float, " 0x3.1 ") self.assertRaises(ValueError, float, " -0x3.p-1 ") self.assertRaises(ValueError, float, " +0x3.p-1 ") self.assertEqual(float(" 25.e-1 "), 2.5) self.assertEqual(support.fcmp(float(" .25e-1 "), .025), 0)
def test_float_with_comma(self): # set locale to something that doesn't use '.' for the decimal point # float must not accept the locale specific decimal point but # it still has to accept the normal python syntax import locale if not locale.localeconv()['decimal_point'] == ',': self.skipTest('decimal_point is not ","') self.assertEqual(float(" 3.14 "), 3.14) self.assertEqual(float("+3.14 "), 3.14) self.assertEqual(float("-3.14 "), -3.14) self.assertEqual(float(".14 "), .14) self.assertEqual(float("3. "), 3.0) self.assertEqual(float("3.e3 "), 3000.0) self.assertEqual(float("3.2e3 "), 3200.0) self.assertEqual(float("2.5e-1 "), 0.25) self.assertEqual(float("5e-1"), 0.5) self.assertRaises(ValueError, float, " 3,14 ") self.assertRaises(ValueError, float, " +3,14 ") self.assertRaises(ValueError, float, " -3,14 ") self.assertRaises(ValueError, float, " 0x3.1 ") self.assertRaises(ValueError, float, " -0x3.p-1 ") self.assertRaises(ValueError, float, " +0x3.p-1 ") self.assertEqual(float(" 25.e-1 "), 2.5) self.assertEqual(test_support.fcmp(float(" .25e-1 "), .025), 0)
def test_float_with_comma(self): # set locale to something that doesn't use '.' for the decimal point # float must not accept the locale specific decimal point but # it still has to accept the normal python syntax import locale if not locale.localeconv()['decimal_point'] == ',': return self.assertEqual(float(" 3.14 "), 3.14) self.assertEqual(float("+3.14 "), 3.14) self.assertEqual(float("-3.14 "), -3.14) self.assertEqual(float(".14 "), .14) self.assertEqual(float("3. "), 3.0) self.assertEqual(float("3.e3 "), 3000.0) self.assertEqual(float("3.2e3 "), 3200.0) self.assertEqual(float("2.5e-1 "), 0.25) self.assertEqual(float("5e-1"), 0.5) self.assertRaises(ValueError, float, " 3,14 ") self.assertRaises(ValueError, float, " +3,14 ") self.assertRaises(ValueError, float, " -3,14 ") self.assertRaises(ValueError, float, " 0x3.1 ") self.assertRaises(ValueError, float, " -0x3.p-1 ") self.assertRaises(ValueError, float, " +0x3.p-1 ") self.assertEqual(float(" 25.e-1 "), 2.5) self.assertAlmostEqual(float(" .25e-1 "), .025)
def test_locale(self): try: oldloc = locale.setlocale(locale.LC_ALL) locale.setlocale(locale.LC_ALL, '') except locale.Error as err: self.skipTest("Cannot set locale: {}".format(err)) try: localeconv = locale.localeconv() sep = localeconv['thousands_sep'] point = localeconv['decimal_point'] text = format(123456789, "n") self.assertIn(sep, text) self.assertEqual(text.replace(sep, ''), '123456789') text = format(1234.5, "n") self.assertIn(sep, text) self.assertIn(point, text) self.assertEqual(text.replace(sep, ''), '1234' + point + '5') finally: locale.setlocale(locale.LC_ALL, oldloc)
def format_satoshis(x, is_diff=False, num_zeros=0, decimal_point=8, whitespaces=False): from locale import localeconv if x is None: return 'unknown' x = int(x) # Some callers pass Decimal scale_factor = pow(10, decimal_point) integer_part = "{:n}".format(int(abs(x) / scale_factor)) if x < 0: integer_part = '-' + integer_part elif is_diff: integer_part = '+' + integer_part dp = localeconv()['decimal_point'] fract_part = ("{:0" + str(decimal_point) + "}").format(abs(x) % scale_factor) fract_part = fract_part.rstrip('0') if len(fract_part) < num_zeros: fract_part += "0" * (num_zeros - len(fract_part)) result = integer_part + dp + fract_part if whitespaces: result += " " * (decimal_point - len(fract_part)) result = " " * (15 - len(result)) + result return result.decode('utf8')
def test_float_with_comma(self): # set locale to something that doesn't use '.' for the decimal point # float must not accept the locale specific decimal point but # it still has to accept the normal python syntax import locale if not locale.localeconv()['decimal_point'] == ',': self.skipTest('decimal_point is not ","') self.assertEqual(float(" 3.14 "), 3.14) self.assertEqual(float("+3.14 "), 3.14) self.assertEqual(float("-3.14 "), -3.14) self.assertEqual(float(".14 "), .14) self.assertEqual(float("3. "), 3.0) self.assertEqual(float("3.e3 "), 3000.0) self.assertEqual(float("3.2e3 "), 3200.0) self.assertEqual(float("2.5e-1 "), 0.25) self.assertEqual(float("5e-1"), 0.5) self.assertRaises(ValueError, float, " 3,14 ") self.assertRaises(ValueError, float, " +3,14 ") self.assertRaises(ValueError, float, " -3,14 ") self.assertRaises(ValueError, float, " 0x3.1 ") self.assertRaises(ValueError, float, " -0x3.p-1 ") self.assertRaises(ValueError, float, " +0x3.p-1 ") self.assertEqual(float(" 25.e-1 "), 2.5) self.assertAlmostEqual(float(" .25e-1 "), .025)
def test_wide_char_separator_decimal_point(self): # locale with wide char separator and decimal point import locale Decimal = self.decimal.Decimal decimal_point = locale.localeconv()['decimal_point'] thousands_sep = locale.localeconv()['thousands_sep'] if decimal_point != '\u066b': self.skipTest('inappropriate decimal point separator' '({!a} not {!a})'.format(decimal_point, '\u066b')) if thousands_sep != '\u066c': self.skipTest('inappropriate thousands separator' '({!a} not {!a})'.format(thousands_sep, '\u066c')) self.assertEqual(format(Decimal('100000000.123'), 'n'), '100\u066c000\u066c000\u066b123')
def test_float_with_comma(self): # set locale to something that doesn't use '.' for the decimal point # float must not accept the locale specific decimal point but # it still has to accept the normal python syntax import locale if not locale.localeconv()['decimal_point'] == ',': return self.assertEqual(float(" 3.14 "), 3.14) self.assertEqual(float("+3.14 "), 3.14) self.assertEqual(float("-3.14 "), -3.14) self.assertEqual(float(".14 "), .14) self.assertEqual(float("3. "), 3.0) self.assertEqual(float("3.e3 "), 3000.0) self.assertEqual(float("3.2e3 "), 3200.0) self.assertEqual(float("2.5e-1 "), 0.25) self.assertEqual(float("5e-1"), 0.5) self.assertRaises(ValueError, float, " 3,14 ") self.assertRaises(ValueError, float, " +3,14 ") self.assertRaises(ValueError, float, " -3,14 ") self.assertRaises(ValueError, float, " 0x3.1 ") self.assertRaises(ValueError, float, " -0x3.p-1 ") self.assertRaises(ValueError, float, " +0x3.p-1 ") self.assertEqual(float(" 25.e-1 "), 2.5) self.assertEqual(test_support.fcmp(float(" .25e-1 "), .025), 0)
def locale_convert(val, func, group): """\ Attempt to convert a string to a number, first converting the decimal place character if needed. Then, if the conversion was not possible (i.e. it is not a number), run it through strxfrm to make the work sorting as requested, possibly grouping first. """ # Format the number so that the conversion function can interpret it. radix = localeconv()['decimal_point'] s = val.replace(radix, '.') if radix != '.' else val # Perform the conversion t = func[0](s) # Return the number or transformed string. # If the input is identical to the output, then no conversion happened. # In this case, we don't want to return the function output because it # may have had characters modified from the above 'replace' call, # so we return the input. if group: if use_pyicu: xfrm = get_pyicu_transform(getlocale()) return xfrm(groupletters(val)) if not func[1](t) else t else: return strxfrm(groupletters(val)) if not func[1](t) else t else: if use_pyicu: xfrm = get_pyicu_transform(getlocale()) return xfrm(val) if not func[1](t) else t else: return strxfrm(val) if not func[1](t) else t
def get_decimal_point(): return locale.localeconv()['decimal_point']
def get_locale_currency_symbol(): """ Get currency symbol from locale """ import locale locale.setlocale(locale.LC_ALL, '') conv = locale.localeconv() return conv['int_curr_symbol']
def setUp(self): self.sep = locale.localeconv()['thousands_sep']
def test_wide_char_separator_decimal_point(self): # locale with wide char separator and decimal point import locale decimal_point = locale.localeconv()['decimal_point'] thousands_sep = locale.localeconv()['thousands_sep'] if decimal_point != '\xd9\xab': self.skipTest('inappropriate decimal point separator' '({!r} not {!r})'.format(decimal_point, '\xd9\xab')) if thousands_sep != '\xd9\xac': self.skipTest('inappropriate thousands separator' '({!r} not {!r})'.format(thousands_sep, '\xd9\xac')) self.assertEqual(format(Decimal('100000000.123'), 'n'), '100\xd9\xac000\xd9\xac000\xd9\xab123')
def currency(value): try: locale.setlocale(locale.LC_ALL,'en_US.UTF-8') except: locale.setlocale(locale.LC_ALL,'') loc = locale.localeconv() return locale.currency(value, loc['currency_symbol'], grouping=True) # this can be used if you're having trouble configuring the proper locale # for your operating system #@register.filter(name='currency') #def currency(value): # return '$' + str(value)
def test_wide_char_separator_decimal_point(self): # locale with wide char separator and decimal point import locale Decimal = self.decimal.Decimal decimal_point = locale.localeconv()['decimal_point'] thousands_sep = locale.localeconv()['thousands_sep'] if decimal_point != '\u066b' or thousands_sep != '\u066c': return self.assertEqual(format(Decimal('100000000.123'), 'n'), '100\u066c000\u066c000\u066b123')
def local_decimal_seperator(): return locale.localeconv()["decimal_point"]
def get_value_with_suffix(value, unit=""): decimal_point = locale.localeconv()["decimal_point"] if abs(value) >= 10 ** 9: target_val, suffix = value / 10 ** 9, "G" elif abs(value) >= 10 ** 6: target_val, suffix = value / 10 ** 6, "M" elif abs(value) >= 10 ** 3: target_val, suffix = value / 10 ** 3, "k" else: target_val, suffix = value, "" return locale.format_string("%.3f", target_val).rstrip("0").rstrip(decimal_point) + suffix + unit
def numeric_convention_information(): print('Numeric conventions:') pprint.pprint(locale.localeconv())
def get_thousands_sep(): sep = locale.localeconv()['thousands_sep'] # If this locale library is broken, some of the thousands separator # characters are incorrectly blank. Here is a lookup table of the # corrections I am aware of. if dumb_sort(): try: loc = '.'.join(locale.getlocale()) except TypeError: # No locale loaded, default to ',' return ',' return {'de_DE.ISO8859-15': '.', 'es_ES.ISO8859-1': '.', 'de_AT.ISO8859-1': '.', 'de_at': '\xa0', 'nl_NL.UTF-8': '.', 'es_es': '.', 'fr_CH.ISO8859-15': '\xa0', 'fr_CA.ISO8859-1': '\xa0', 'de_CH.ISO8859-1': '.', 'fr_FR.ISO8859-15': '\xa0', 'nl_NL.ISO8859-1': '.', 'ca_ES.UTF-8': '.', 'nl_NL.ISO8859-15': '.', 'de_ch': "'", 'ca_es': '.', 'de_AT.ISO8859-15': '.', 'ca_ES.ISO8859-1': '.', 'de_AT.UTF-8': '.', 'es_ES.UTF-8': '.', 'fr_fr': '\xa0', 'es_ES.ISO8859-15': '.', 'de_DE.ISO8859-1': '.', 'nl_nl': '.', 'fr_ch': '\xa0', 'fr_ca': '\xa0', 'de_DE.UTF-8': '.', 'ca_ES.ISO8859-15': '.', 'de_CH.ISO8859-15': '.', 'fr_FR.ISO8859-1': '\xa0', 'fr_CH.ISO8859-1': '\xa0', 'de_de': '.', 'fr_FR.UTF-8': '\xa0', 'fr_CA.ISO8859-15': '\xa0', }.get(loc, sep) else: return sep