Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96453 > unrolled thread

string_formatter 1.0.1 released

Started byAnthon van der Neut <anthon@mnt.org>
First post2015-09-11 22:51 +0200
Last post2015-09-11 22:51 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  string_formatter 1.0.1 released Anthon van der Neut <anthon@mnt.org> - 2015-09-11 22:51 +0200

#96453 — string_formatter 1.0.1 released

FromAnthon van der Neut <anthon@mnt.org>
Date2015-09-11 22:51 +0200
Subjectstring_formatter 1.0.1 released
Message-ID<mailman.444.1442088163.8327.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web