pyppin.text.formatter

[View Source]

Tools to format numbers, times, etc., using the fancy functions in pyppin.text.

Classes

Formatter()

A string.Formatter that includes several new formatting options.

class pyppin.text.formatter.Formatter[source]

Bases: Formatter

A string.Formatter that includes several new formatting options.

Provides a Python string Formatter which, in addition to all of the standard Python formatting, supports a few new formats:

Type

Format specification

Renders as

int, float

:si

SI (decimal scale)

int, float

:sib

SI (binary scale)

int, float

:iec

SI (binary scale, IEC)

timedelta

:td

time_delta_string

timedelta

:rd

relative_time_string

For example, you might format a timedelta as {delta:rd} to get a string like “3 days from now”, or a number of bytes as {size:sib}B to get a string like “1.2GB”.

To use this class, simply create one of these objects and use its format() method the way you would normally use str.format().

A wide range of options are available for each of these formats.

  • si, sib, and iec accept [[fill]align][sign][width][.precision][(threshold)] as options. (e.g., {variable:>+30.2(1.2)si} would print variable as a decimal-scale SI number, right-aligned in a 30-character wide block, showing a plus or minus sign, two points after the decimal, and using a threshold of 1.2.) The fill, align, sign, and width arguments are identical to the standard ones defined in Python’s custom string formatting rules; the precision and threshold are defined by the si_prefix function.

  • td accepts [[fill]align][sign][width], using the standard Python meanings for each; e.g., {delta:+30td} would print delta in a 30-character wide box with a plus or minus sign.

  • rd accepts [[fill]align][width], using the standard Python meanings for each.

format_field(value: Any, format_spec: str) str[source]