Uoti Urpala added the comment:
This kind of formatting is needed quite often when working on network protocols
or file formats, and I think the replies here fail to address important issues.
In general you can't encode after formatting, as that doesn't work with binary
data, and often it's not appropriate for the low-level routines doing the
formatting to know what charset the data is in even if it is text (so it should
be fed in already encoded as bytes). The replies from Martin v. Löwis seem to
argue that you could use methods other than formatting; that would work almost
as well as an argument to remove formatting support from text strings, and IMO
cases where formatting is the best option are common.
Here's an example (based on real use but simplified):
template = b"""
stuff here
header1: {}
header2: {}
more stuff
"""
def lowlevel_send(s, b1, b2): # s socket, b1 and b2 bytes
s.send(template.format(b1, b2))
To clarify the requirements a bit, the issue is not so much about having a
.format method on byte string objects (that's just the most natural-looking way
of solving it); the core requirement is to have a formatting operator that can
take byte strings as *arguments* and produce byte string *output* where the
arguments can be placed unchanged.
--
nosy: +uau
___
Python tracker
<http://bugs.python.org/issue3982>
___
___
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com