vermouth.truncating_formatter module

Provides a string formatter that can not only pad strings to a specified length if they’re too short, but also truncate them if they’re too long.

class vermouth.truncating_formatter.FormatSpec(fill, align, sign, alt, zero_padding, width, comma, decimal, precision, type)

Bases: tuple

Create new instance of FormatSpec(fill, align, sign, alt, zero_padding, width, comma, decimal, precision, type)

align

Alias for field number 1

alt

Alias for field number 3

comma

Alias for field number 6

decimal

Alias for field number 7

fill

Alias for field number 0

precision

Alias for field number 8

sign

Alias for field number 2

type

Alias for field number 9

width

Alias for field number 5

zero_padding

Alias for field number 4

class vermouth.truncating_formatter.TruncFormatter[source]

Bases: Formatter

Adds the ‘t’ option to the format specification mini-language at the end of the format string. If provided, the produced formatted string will be truncated to the specified length.

format_field(value, format_spec)[source]

Implements the ‘t’ option to truncate strings that are too long to the required width.

Parameters:
  • value – The object to format.

  • format_spec (str) – The format_spec describing how value should be formatted

  • Returns

  • strvalue formatted as per format_spec

format_spec_re = re.compile('(([\\s\\S])?([<>=\\^]))?([\\+\\- ])?(#)?(0)?(\\d*)?(,)?((\\.)(\\d*))?([sbcdoxXneEfFgGn%])?')