string_formatter 1.0.1 released

Anthon van der Neut anthon at mnt.org
Fri Sep 11 16:51:22 EDT 2015


string_formatter is a backport of the 3.4.1+ string.Formatter class, to
2.7, 3.3 and 3.4.0. This allows the use of empty keys {} in its format
strings.

At the same time it solves an existing (at least until 3.5.0.rc3) bug in
string.Formatter, breaking with the use of nested empty keys.
Python 3.4.3:

    >>> import string
    >>> string.Formatter().format("|{:<{}} {}|", 'a', 3, 5)
    '|a   3|'


In addition (that is how it all started) it provides TrailingFormatter
which allows a type specification "t" with a single character parameter.
That character will be added to the (stringified) value before applying
(left-aligned) formatting:

    import string_formatter as string

    fmt = string.TrailingFormatter()
    d = dict(a=1, bc=2, xyz=18)
    for key in sorted(d):
        print(fmt.format("{:t{}<{}} {:>3}", key, ':', 15, d[key]))

giving:

    a:                1
    bc:               2
    xyz:             18




More information about the Python-list mailing list