Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96453
| Date | 2015-09-11 22:51 +0200 |
|---|---|
| From | Anthon van der Neut <anthon@mnt.org> |
| Subject | string_formatter 1.0.1 released |
| References | <CAFrp1vp4KSmH3-O4ewU3DwkrrZ+WUO6J-aExWc2GoppSSs_oEQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.444.1442088163.8327.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
string_formatter 1.0.1 released Anthon van der Neut <anthon@mnt.org> - 2015-09-11 22:51 +0200
csiph-web