MCPcopy Index your code
hub / github.com/RustPython/RustPython / _parse_format_specifier

Function _parse_format_specifier

Lib/_pydecimal.py:6156–6237  ·  view source on GitHub ↗

Parse and validate a format specifier. Turns a standard numeric format specifier into a dict, with the following entries: fill: fill character to pad field to minimum width align: alignment type, either '<', '>', '=' or '^' sign: either '+', '-' or ' ' minimumwidth:

(format_spec, _localeconv=None)

Source from the content-addressed store, hash-verified

6154 pass
6155
6156def _parse_format_specifier(format_spec, _localeconv=None):
6157 """Parse and validate a format specifier.
6158
6159 Turns a standard numeric format specifier into a dict, with the
6160 following entries:
6161
6162 fill: fill character to pad field to minimum width
6163 align: alignment type, either '<', '>', '=' or '^'
6164 sign: either '+', '-' or ' '
6165 minimumwidth: nonnegative integer giving minimum width
6166 zeropad: boolean, indicating whether to pad with zeros
6167 thousands_sep: string to use as thousands separator, or ''
6168 grouping: grouping for thousands separators, in format
6169 used by localeconv
6170 decimal_point: string to use for decimal point
6171 precision: nonnegative integer giving precision, or None
6172 type: one of the characters 'eEfFgG%', or None
6173
6174 """
6175 m = _parse_format_specifier_regex.match(format_spec)
6176 if m is None:
6177 raise ValueError("Invalid format specifier: " + format_spec)
6178
6179 # get the dictionary
6180 format_dict = m.groupdict()
6181
6182 # zeropad; defaults for fill and alignment. If zero padding
6183 # is requested, the fill and align fields should be absent.
6184 fill = format_dict['fill']
6185 align = format_dict['align']
6186 format_dict['zeropad'] = (format_dict['zeropad'] is not None)
6187 if format_dict['zeropad']:
6188 if fill is not None:
6189 raise ValueError("Fill character conflicts with '0'"
6190 " in format specifier: " + format_spec)
6191 if align is not None:
6192 raise ValueError("Alignment conflicts with '0' in "
6193 "format specifier: " + format_spec)
6194 format_dict['fill'] = fill or ' '
6195 # PEP 3101 originally specified that the default alignment should
6196 # be left; it was later agreed that right-aligned makes more sense
6197 # for numeric types. See http://bugs.python.org/issue6857.
6198 format_dict['align'] = align or '>'
6199
6200 # default sign handling: '-' for negative, '' for positive
6201 if format_dict['sign'] is None:
6202 format_dict['sign'] = '-'
6203
6204 # minimumwidth defaults to 0; precision remains None if not given
6205 format_dict['minimumwidth'] = int(format_dict['minimumwidth'] or '0')
6206 if format_dict['precision'] is not None:
6207 format_dict['precision'] = int(format_dict['precision'])
6208
6209 # if format type is 'g' or 'G' then a precision of 0 makes little
6210 # sense; convert it to 1. Same if format type is unspecified.
6211 if format_dict['precision'] == 0:
6212 if format_dict['type'] is None or format_dict['type'] in 'gGn':
6213 format_dict['precision'] = 1

Callers 1

__format__Method · 0.85

Calls 2

groupdictMethod · 0.80
matchMethod · 0.45

Tested by

no test coverage detected