17
17
import collections
18
18
from builtins import str as _builtin_str
19
19
import functools
20
+ import warnings
20
21
21
22
# Try importing the _locale module.
22
23
#
@@ -180,19 +181,6 @@ def _strip_padding(s, amount):
180
181
_percent_re = re .compile (r'%(?:\((?P<key>.*?)\))?'
181
182
r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]' )
182
183
183
- def format (percent , value , grouping = False , monetary = False , * additional ):
184
- """Returns the locale-aware substitution of a %? specifier
185
- (percent).
186
-
187
- additional is for format strings which contain one or more
188
- '*' modifiers."""
189
- # this is only for one-percent-specifier strings and this should be checked
190
- match = _percent_re .match (percent )
191
- if not match or len (match .group ())!= len (percent ):
192
- raise ValueError (("format() must be given exactly one %%char "
193
- "format specifier, %s not valid" ) % repr (percent ))
194
- return _format (percent , value , grouping , monetary , * additional )
195
-
196
184
def _format (percent , value , grouping = False , monetary = False , * additional ):
197
185
if additional :
198
186
formatted = percent % ((value ,) + additional )
@@ -217,10 +205,13 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
217
205
formatted = _strip_padding (formatted , seps )
218
206
return formatted
219
207
220
- def format_string (f , val , grouping = False ):
208
+ def format_string (f , val , grouping = False , monetary = False ):
221
209
"""Formats a string in the same way that the % formatting would use,
222
210
but takes the current locale into account.
223
- Grouping is applied if the third parameter is true."""
211
+
212
+ Grouping is applied if the third parameter is true.
213
+ Conversion uses monetary thousands separator and grouping strings if
214
+ forth parameter monetary is true."""
224
215
percents = list (_percent_re .finditer (f ))
225
216
new_f = _percent_re .sub ('%s' , f )
226
217
@@ -230,7 +221,7 @@ def format_string(f, val, grouping=False):
230
221
if perc .group ()[- 1 ]== '%' :
231
222
new_val .append ('%' )
232
223
else :
233
- new_val .append (format (perc .group (), val , grouping ))
224
+ new_val .append (_format (perc .group (), val , grouping , monetary ))
234
225
else :
235
226
if not isinstance (val , tuple ):
236
227
val = (val ,)
@@ -244,13 +235,27 @@ def format_string(f, val, grouping=False):
244
235
new_val .append (_format (perc .group (),
245
236
val [i ],
246
237
grouping ,
247
- False ,
238
+ monetary ,
248
239
* val [i + 1 :i + 1 + starcount ]))
249
240
i += (1 + starcount )
250
241
val = tuple (new_val )
251
242
252
243
return new_f % val
253
244
245
+ def format (percent , value , grouping = False , monetary = False , * additional ):
246
+ """Deprecated, use format_string instead."""
247
+ warnings .warn (
248
+ "This method will be removed in a future version of Python."
249
+ "Use 'locale.format_string()' instead." ,
250
+ DeprecationWarning , stacklevel = 2
251
+ )
252
+
253
+ match = _percent_re .match (percent )
254
+ if not match or len (match .group ())!= len (percent ):
255
+ raise ValueError (("format() must be given exactly one %%char "
256
+ "format specifier, %s not valid" ) % repr (percent ))
257
+ return _format (percent , value , grouping , monetary , * additional )
258
+
254
259
def currency (val , symbol = True , grouping = False , international = False ):
255
260
"""Formats val according to the currency settings
256
261
in the current locale."""
@@ -262,7 +267,7 @@ def currency(val, symbol=True, grouping=False, international=False):
262
267
raise ValueError ("Currency formatting is not possible using "
263
268
"the 'C' locale." )
264
269
265
- s = format ('%%.%if' % digits , abs (val ), grouping , monetary = True )
270
+ s = _format ('%%.%if' % digits , abs (val ), grouping , monetary = True )
266
271
# '<' and '>' are markers if the sign must be inserted between symbol and value
267
272
s = '<' + s + '>'
268
273
@@ -298,7 +303,7 @@ def currency(val, symbol=True, grouping=False, international=False):
298
303
299
304
def str (val ):
300
305
"""Convert float to string, taking the locale into account."""
301
- return format ("%.12g" , val )
306
+ return _format ("%.12g" , val )
302
307
303
308
def delocalize (string ):
304
309
"Parses a string as a normalized number according to the locale settings."
@@ -327,7 +332,7 @@ def atoi(string):
327
332
def _test ():
328
333
setlocale (LC_ALL , "" )
329
334
#do grouping
330
- s1 = format ("%d" , 123456789 ,1 )
335
+ s1 = format_string ("%d" , 123456789 ,1 )
331
336
print (s1 , "is" , atoi (s1 ))
332
337
#standard formatting
333
338
s1 = str (3.14 )
0 commit comments