summaryrefslogtreecommitdiffstats
path: root/Lib/locale.py
diff options
context:
space:
mode:
authorGarvit Khatri <garvit.khatri@zomato.com>2017-03-28 15:43:38 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2017-03-28 15:43:38 (GMT)
commit1cf93a76c2cf307f2e1e514a8944864f746337ea (patch)
tree85c7a1a948d17182efeaf6e6405ac64105f87f99 /Lib/locale.py
parentc8fa45bac2942accdb24dde318f87c9eb21dbfde (diff)
downloadcpython-1cf93a76c2cf307f2e1e514a8944864f746337ea.zip
cpython-1cf93a76c2cf307f2e1e514a8944864f746337ea.tar.gz
cpython-1cf93a76c2cf307f2e1e514a8944864f746337ea.tar.bz2
bpo-10379: add 'monetary' to format_string, deprecate format
Add the 'monetary' parameter to format_string so that all uses of format can be converted to format_string. Adjust the documentation accordingly, and add a deprecation warning when format is used.
Diffstat (limited to 'Lib/locale.py')
-rw-r--r--Lib/locale.py45
1 files changed, 25 insertions, 20 deletions
diff --git a/Lib/locale.py b/Lib/locale.py
index f8d1d78..50cd652 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -17,6 +17,7 @@ import re
import collections
from builtins import str as _builtin_str
import functools
+import warnings
# Try importing the _locale module.
#
@@ -180,19 +181,6 @@ def _strip_padding(s, amount):
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
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).
-
- 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 "
- "format specifier, %s not valid") % repr(percent))
- return _format(percent, value, grouping, monetary, *additional)
-
def _format(percent, value, grouping=False, monetary=False, *additional):
if additional:
formatted = percent % ((value,) + additional)
@@ -217,10 +205,13 @@ 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.
+ 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 +221,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,13 +235,27 @@ 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)
return new_f % val
+def format(percent, value, grouping=False, monetary=False, *additional):
+ """Deprecated, use format_string instead."""
+ warnings.warn(
+ "This method will be removed in a future version of Python."
+ "Use 'locale.format_string()' instead.",
+ DeprecationWarning, stacklevel=2
+ )
+
+ match = _percent_re.match(percent)
+ if not match or len(match.group())!= len(percent):
+ raise ValueError(("format() must be given exactly one %%char "
+ "format specifier, %s not valid") % repr(percent))
+ return _format(percent, value, grouping, monetary, *additional)
+
def currency(val, symbol=True, grouping=False, international=False):
"""Formats val according to the currency settings
in the current locale."""
@@ -262,7 +267,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('%%.%if' % digits, abs(val), grouping, monetary=True)
# '<' and '>' are markers if the sign must be inserted between symbol and value
s = '<' + s + '>'
@@ -298,7 +303,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("%.12g", val)
def delocalize(string):
"Parses a string as a normalized number according to the locale settings."
@@ -327,7 +332,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)