-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-10379: deprecate locale.format in lieu of locale.format_string #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
d0f9863
820e289
4cfd737
a08419f
4e24a3f
6097e47
04577c2
2039406
5b100b8
041349f
795aa65
8190fa9
7e0bfcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,10 @@ New Modules | |
Improved Modules | ||
================ | ||
|
||
* Added another argument *monetary* in :meth:`format_string` of :mod:`locale`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a subsection header before this line for the locale module (the modules with enhancements are listed in this section in alphabetical order). |
||
(Contributed by Garvit in :issue:`10379`.) | ||
|
||
|
||
unittest.mock | ||
------------- | ||
|
||
|
@@ -140,6 +144,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`.) | ||
|
||
|
||
Removed | ||
======= | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import collections | ||
from builtins import str as _builtin_str | ||
import functools | ||
import warnings | ||
|
||
# Try importing the _locale module. | ||
# | ||
|
@@ -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.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
@@ -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,) | ||
|
@@ -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) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 + '>' | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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." | ||
|
@@ -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) | ||
|
There was a problem hiding this comment.
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"