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

Function _parse_format_specifier

Lib/_pydecimal.py:6159–6240  ·  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

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

Callers 1

__format__Method · 0.85

Calls 1

matchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…