Skip to content
Merged
16 changes: 15 additions & 1 deletion Doc/library/locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,26 @@ The :mod:`locale` module defines the following exception and functions:
Please note that this function will only work for exactly one %char specifier.
For whole format strings, use :func:`format_string`.

.. deprecated:: 3.7
Use :meth:`format_string` instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looking at this again, I think what we should do is move 'format' under 'format_string', and change its body to say "works like format_string but only accepts a single format specification. That followed by the deprecation notice will make it a lot less likely anyone will use it :)


.. function:: format_string(format, val, grouping=False)

.. function:: format_string(format, val, grouping=False, monetary=False)

Formats a number *val* according to the current :const:`LC_NUMERIC` setting.
The format follows the conventions of the ``%`` operator. For floating point
values, the decimal point is modified if appropriate. If *grouping* is true,
also takes the grouping into account.

If *monetary* is true, the conversion uses monetary thousands separator and
grouping strings.

Processes formatting specifiers as in ``format % val``, but takes the current
locale settings into account.

.. versionchanged:: 3.7
The *monetary* keyword parameter was added.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description of the monetary parameter should be added to the regular docs, and the version changed phrase should just say "the monetary keyword parameter was added"

.. function:: currency(val, symbol=True, grouping=False, international=False)

Expand Down
11 changes: 11 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ New Modules
Improved Modules
================

locale
------

Added another argument *monetary* in :meth:`format_string` of :mod:`locale`.
If *monetary* is true, the conversion uses monetary thousands separator and
grouping strings.
(Contributed by Garvit in :issue:`10379`.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: our convention appears to be to put the Contributed by as part of the paragraph when there's only one paragraph. Ends up formatted the same in html, but might as well make the ReST consistent as well.

os
--

Expand Down Expand Up @@ -169,6 +177,9 @@ Deprecated
``0x03050400`` and ``0x03060000`` (not including) or ``0x03060100`` or
higher. (Contributed by Serhiy Storchaka in :issue:`27867`.)

- Deprecated :meth:`format` from :mod:`locale`, use the :meth:`format_string`
instead. (Contributed by Garvit in :issue:`10379`.)

- Methods
:meth:`MetaPathFinder.find_module() <importlib.abc.MetaPathFinder.find_module>`
(replaced by
Expand Down
28 changes: 16 additions & 12 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import collections
from builtins import str as _builtin_str
import functools
import warnings

# Try importing the _locale module.
#
Expand Down Expand Up @@ -181,12 +182,13 @@ def _strip_padding(s, amount):
r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')

def format(percent, value, grouping=False, monetary=False, *additional):
"""Returns the locale-aware substitution of a %? specifier
(percent).
"""Deprecated, use format_string instead."""
warnings.warn(
"This method will be removed in future versions. "
"Use 'locale.format_string()' instead.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should read "This method will be removed in a future version of Python."

DeprecationWarning, stacklevel=2
)

additional is for format strings which contain one or more
'*' modifiers."""
# this is only for one-percent-specifier strings and this should be checked
match = _percent_re.match(percent)
if not match or len(match.group())!= len(percent):
raise ValueError(("format() must be given exactly one %%char "
Expand Down Expand Up @@ -217,10 +219,12 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
formatted = _strip_padding(formatted, seps)
return formatted

def format_string(f, val, grouping=False):
def format_string(f, val, grouping=False, monetary=False):
"""Formats a string in the same way that the % formatting would use,
but takes the current locale into account.
Grouping is applied if the third parameter is true."""
Grouping is applied if the third parameter is true.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we are editing this, I think there should be a blank line after the first sentence, before the 'Grouping' sentence.

Conversion uses monetary thousands separator and grouping strings if
forth parameter monetary is true."""
percents = list(_percent_re.finditer(f))
new_f = _percent_re.sub('%s', f)

Expand All @@ -230,7 +234,7 @@ def format_string(f, val, grouping=False):
if perc.group()[-1]=='%':
new_val.append('%')
else:
new_val.append(format(perc.group(), val, grouping))
new_val.append(_format(perc.group(), val, grouping, monetary))
else:
if not isinstance(val, tuple):
val = (val,)
Expand All @@ -244,7 +248,7 @@ def format_string(f, val, grouping=False):
new_val.append(_format(perc.group(),
val[i],
grouping,
False,
monetary,
*val[i+1:i+1+starcount]))
i += (1 + starcount)
val = tuple(new_val)
Expand All @@ -262,7 +266,7 @@ def currency(val, symbol=True, grouping=False, international=False):
raise ValueError("Currency formatting is not possible using "
"the 'C' locale.")

s = format('%%.%if' % digits, abs(val), grouping, monetary=True)
s = format_string('%%.%if' % digits, abs(val), grouping, monetary=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we know there's only one format string here, I think this should be a call to _format.

# '<' and '>' are markers if the sign must be inserted between symbol and value
s = '<' + s + '>'

Expand Down Expand Up @@ -298,7 +302,7 @@ def currency(val, symbol=True, grouping=False, international=False):

def str(val):
"""Convert float to string, taking the locale into account."""
return format("%.12g", val)
return format_string("%.12g", val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I think this should be _format.


def delocalize(string):
"Parses a string as a normalized number according to the locale settings."
Expand Down Expand Up @@ -327,7 +331,7 @@ def atoi(string):
def _test():
setlocale(LC_ALL, "")
#do grouping
s1 = format("%d", 123456789,1)
s1 = format_string("%d", 123456789,1)
print(s1, "is", atoi(s1))
#standard formatting
s1 = str(3.14)
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import locale
import sys
import codecs
import warnings

class BaseLocalizedTest(unittest.TestCase):
#
Expand Down Expand Up @@ -197,6 +198,10 @@ def test_padding(self):
self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))

def test_format_deprecation(self):
with self.assertWarns(DeprecationWarning):
locale.format("%-10.f", 4200, grouping=True)

def test_complex_formatting(self):
# Spaces in formatting string
self._test_format_string("One million is %i", 1000000, grouping=1,
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,16 @@ def test_float__format__locale(self):

for i in range(-10, 10):
x = 1234567890.0 * (10.0 ** i)
self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))
self.assertEqual(locale.format_string('%g', x, grouping=True), format(x, 'n'))
self.assertEqual(locale.format_string('%.10g', x, grouping=True), format(x, '.10n'))

@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
def test_int__format__locale(self):
# test locale support for __format__ code 'n' for integers

x = 123456789012345678901234567890
for i in range(0, 30):
self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))
self.assertEqual(locale.format_string('%d', x, grouping=True), format(x, 'n'))

# move to the next integer to test
x = x // 10
Expand Down