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

Method _format_general

Lib/fractions.py:461–493  ·  view source on GitHub ↗

Helper method for __format__. Handles fill, alignment, signs, and thousands separators in the case of no presentation type.

(self, match)

Source from the content-addressed store, hash-verified

459 return '%s/%s' % (self._numerator, self._denominator)
460
461 def _format_general(self, match):
462 """Helper method for __format__.
463
464 Handles fill, alignment, signs, and thousands separators in the
465 case of no presentation type.
466 """
467 # Validate and parse the format specifier.
468 fill = match["fill"] or " "
469 align = match["align"] or ">"
470 pos_sign = "" if match["sign"] == "-" else match["sign"]
471 alternate_form = bool(match["alt"])
472 minimumwidth = int(match["minimumwidth"] or "0")
473 thousands_sep = match["thousands_sep"] or ''
474
475 # Determine the body and sign representation.
476 n, d = self._numerator, self._denominator
477 if d > 1 or alternate_form:
478 body = f"{abs(n):{thousands_sep}}/{d:{thousands_sep}}"
479 else:
480 body = f"{abs(n):{thousands_sep}}"
481 sign = '-' if n < 0 else pos_sign
482
483 # Pad with fill character if necessary and return.
484 padding = fill * (minimumwidth - len(sign) - len(body))
485 if align == ">":
486 return padding + sign + body
487 elif align == "<":
488 return sign + body + padding
489 elif align == "^":
490 half = len(padding) // 2
491 return padding[:half] + sign + body + padding[half:]
492 else: # align == "="
493 return sign + padding + body
494
495 def _format_float_style(self, match):
496 """Helper method for __format__; handles float presentation types."""

Callers 1

__format__Method · 0.95

Calls 1

absFunction · 0.85

Tested by

no test coverage detected