Skip to main content

Module pyformat

Module pyformat 

Source
Expand description

Python str.format for progress text columns.

Port of the subset of Python’s format mini-language that upstream TextColumn reaches through text_format.format(task=task): replacement fields that name a value ({task.completed}, {task.fields[name]}), {{ and }} escapes, and format specs [[fill]align][sign][z][#][0][width] [grouping][.precision][type] over strings, integers, floats, booleans and None. Values print as Python’s str() would, so 10.0 stays 10.0 and 1e16 prints 1e+16.

The spec handling follows CPython’s Python/formatter_unicode.c and PyOS_double_to_string (Python 3.11+, for the z flag). The n type uses the C locale, which is what a Python process that never calls setlocale formats with. tests/golden/pyformat.tsv pins a few thousand cases.

Enums§

FormatValue
A value a replacement field resolves to.

Functions§

float_repr
Python’s repr(float) (also its str()): the shortest round-tripping digits, in scientific notation when the exponent is below -4 or at least 16.
format
Expand template, resolving each field name through lookup.
format_value
format(value, spec), or None where Python would raise.