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 forth parameter monetary is true.
(f, val, grouping=False, monetary=False)
| 211 | return formatted |
| 212 | |
| 213 | def format_string(f, val, grouping=False, monetary=False): |
| 214 | """Formats a string in the same way that the % formatting would use, |
| 215 | but takes the current locale into account. |
| 216 | |
| 217 | Grouping is applied if the third parameter is true. |
| 218 | Conversion uses monetary thousands separator and grouping strings if |
| 219 | forth parameter monetary is true.""" |
| 220 | percents = list(_percent_re.finditer(f)) |
| 221 | new_f = _percent_re.sub('%s', f) |
| 222 | |
| 223 | if isinstance(val, _collections_abc.Mapping): |
| 224 | new_val = [] |
| 225 | for perc in percents: |
| 226 | if perc.group()[-1]=='%': |
| 227 | new_val.append('%') |
| 228 | else: |
| 229 | new_val.append(_format(perc.group(), val, grouping, monetary)) |
| 230 | else: |
| 231 | if not isinstance(val, tuple): |
| 232 | val = (val,) |
| 233 | new_val = [] |
| 234 | i = 0 |
| 235 | for perc in percents: |
| 236 | if perc.group()[-1]=='%': |
| 237 | new_val.append('%') |
| 238 | else: |
| 239 | starcount = perc.group('modifiers').count('*') |
| 240 | new_val.append(_format(perc.group(), |
| 241 | val[i], |
| 242 | grouping, |
| 243 | monetary, |
| 244 | *val[i+1:i+1+starcount])) |
| 245 | i += (1 + starcount) |
| 246 | val = tuple(new_val) |
| 247 | |
| 248 | return new_f % val |
| 249 | |
| 250 | def format(percent, value, grouping=False, monetary=False, *additional): |
| 251 | """Deprecated, use format_string instead.""" |