Issue #13918: Provide a locale.delocalize() function which can remove
locale-specific number formatting from a string representing a number, without then converting it to a specific type. Patch by Cédric Krier.
This commit is contained in:
parent
fce60eaf15
commit
b64bca9852
|
@ -387,6 +387,14 @@ The :mod:`locale` module defines the following exception and functions:
|
||||||
``str(float)``, but takes the decimal point into account.
|
``str(float)``, but takes the decimal point into account.
|
||||||
|
|
||||||
|
|
||||||
|
.. function:: delocalize(string)
|
||||||
|
|
||||||
|
Converts a string into a normalized number string, following the
|
||||||
|
:const:'LC_NUMERIC`settings.
|
||||||
|
|
||||||
|
.. versionadded:: 3.5
|
||||||
|
|
||||||
|
|
||||||
.. function:: atof(string)
|
.. function:: atof(string)
|
||||||
|
|
||||||
Converts a string to a floating point number, following the :const:`LC_NUMERIC`
|
Converts a string to a floating point number, following the :const:`LC_NUMERIC`
|
||||||
|
|
|
@ -301,8 +301,8 @@ def str(val):
|
||||||
"""Convert float to integer, taking the locale into account."""
|
"""Convert float to integer, taking the locale into account."""
|
||||||
return format("%.12g", val)
|
return format("%.12g", val)
|
||||||
|
|
||||||
def atof(string, func=float):
|
def delocalize(string):
|
||||||
"Parses a string as a float according to the locale settings."
|
"Parses a string as a normalized number according to the locale settings."
|
||||||
#First, get rid of the grouping
|
#First, get rid of the grouping
|
||||||
ts = localeconv()['thousands_sep']
|
ts = localeconv()['thousands_sep']
|
||||||
if ts:
|
if ts:
|
||||||
|
@ -311,12 +311,15 @@ def atof(string, func=float):
|
||||||
dd = localeconv()['decimal_point']
|
dd = localeconv()['decimal_point']
|
||||||
if dd:
|
if dd:
|
||||||
string = string.replace(dd, '.')
|
string = string.replace(dd, '.')
|
||||||
#finally, parse the string
|
return string
|
||||||
return func(string)
|
|
||||||
|
|
||||||
def atoi(str):
|
def atof(string, func=float):
|
||||||
|
"Parses a string as a float according to the locale settings."
|
||||||
|
return func(delocalize(string))
|
||||||
|
|
||||||
|
def atoi(string):
|
||||||
"Converts a string to an integer according to the locale settings."
|
"Converts a string to an integer according to the locale settings."
|
||||||
return atof(str, int)
|
return int(delocalize(string))
|
||||||
|
|
||||||
def _test():
|
def _test():
|
||||||
setlocale(LC_ALL, "")
|
setlocale(LC_ALL, "")
|
||||||
|
|
|
@ -524,5 +524,59 @@ class TestMiscellaneous(unittest.TestCase):
|
||||||
locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
|
locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
|
||||||
|
|
||||||
|
|
||||||
|
class BaseDelocalizeTest(BaseLocalizedTest):
|
||||||
|
|
||||||
|
def _test_delocalize(self, value, out):
|
||||||
|
self.assertEqual(locale.delocalize(value), out)
|
||||||
|
|
||||||
|
def _test_atof(self, value, out):
|
||||||
|
self.assertEqual(locale.atof(value), out)
|
||||||
|
|
||||||
|
def _test_atoi(self, value, out):
|
||||||
|
self.assertEqual(locale.atoi(value), out)
|
||||||
|
|
||||||
|
|
||||||
|
class TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest):
|
||||||
|
|
||||||
|
def test_delocalize(self):
|
||||||
|
self._test_delocalize('50000.00', '50000.00')
|
||||||
|
self._test_delocalize('50,000.00', '50000.00')
|
||||||
|
|
||||||
|
def test_atof(self):
|
||||||
|
self._test_atof('50000.00', 50000.)
|
||||||
|
self._test_atof('50,000.00', 50000.)
|
||||||
|
|
||||||
|
def test_atoi(self):
|
||||||
|
self._test_atoi('50000', 50000)
|
||||||
|
self._test_atoi('50,000', 50000)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest):
|
||||||
|
|
||||||
|
def test_delocalize(self):
|
||||||
|
self._test_delocalize('50000.00', '50000.00')
|
||||||
|
|
||||||
|
def test_atof(self):
|
||||||
|
self._test_atof('50000.00', 50000.)
|
||||||
|
|
||||||
|
def test_atoi(self):
|
||||||
|
self._test_atoi('50000', 50000)
|
||||||
|
|
||||||
|
|
||||||
|
class TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest):
|
||||||
|
|
||||||
|
def test_delocalize(self):
|
||||||
|
self._test_delocalize('50000,00', '50000.00')
|
||||||
|
self._test_delocalize('50 000,00', '50000.00')
|
||||||
|
|
||||||
|
def test_atof(self):
|
||||||
|
self._test_atof('50000,00', 50000.)
|
||||||
|
self._test_atof('50 000,00', 50000.)
|
||||||
|
|
||||||
|
def test_atoi(self):
|
||||||
|
self._test_atoi('50000', 50000)
|
||||||
|
self._test_atoi('50 000', 50000)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -181,6 +181,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #13918: Provide a locale.delocalize() function which can remove
|
||||||
|
locale-specific number formatting from a string representing a number,
|
||||||
|
without then converting it to a specific type. Patch by Cédric Krier.
|
||||||
|
|
||||||
- Issue #22676: Make the pickling of global objects which don't have a
|
- Issue #22676: Make the pickling of global objects which don't have a
|
||||||
__module__ attribute less slow.
|
__module__ attribute less slow.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue