MCPcopy Index your code
hub / github.com/python/cpython / format_string

Function format_string

Lib/locale.py:211–253  ·  view source on GitHub ↗

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. Conversion uses monetary thousands separator and grouping strings if fourth parameter monetary is true.

(f, val, grouping=False, monetary=False)

Source from the content-addressed store, hash-verified

209 return formatted
210
211def format_string(f, val, grouping=False, monetary=False):
212 """Formats a string in the same way that the % formatting would use,
213 but takes the current locale into account.
214
215 Grouping is applied if the third parameter is true.
216 Conversion uses monetary thousands separator and grouping strings if
217 fourth parameter monetary is true."""
218 global _percent_re
219 if _percent_re is None:
220 import re
221
222 _percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?(?P<modifiers'
223 r'>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
224
225 percents = list(_percent_re.finditer(f))
226 new_f = _percent_re.sub('%s', f)
227
228 if isinstance(val, _collections_abc.Mapping):
229 new_val = []
230 for perc in percents:
231 if perc.group()[-1]=='%':
232 new_val.append('%')
233 else:
234 new_val.append(_format(perc.group(), val, grouping, monetary))
235 else:
236 if not isinstance(val, tuple):
237 val = (val,)
238 new_val = []
239 i = 0
240 for perc in percents:
241 if perc.group()[-1]=='%':
242 new_val.append('%')
243 else:
244 starcount = perc.group('modifiers').count('*')
245 new_val.append(_format(perc.group(),
246 val[i],
247 grouping,
248 monetary,
249 *val[i+1:i+1+starcount]))
250 i += (1 + starcount)
251 val = tuple(new_val)
252
253 return new_f % val
254
255def currency(val, symbol=True, grouping=False, international=False):
256 """Formats val according to the currency settings

Callers 1

_testFunction · 0.85

Calls 7

listClass · 0.85
_formatFunction · 0.70
compileMethod · 0.45
subMethod · 0.45
groupMethod · 0.45
appendMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…