(original) (raw)

On 1/17/2014 2:49 AM, Neil Schemenauer wrote:
As I see it, there are two separate goals in adding formatting  
methods to bytes. One is to make it easier to write new programs  
that manipulate byte data. Another is to make it easier to upgrade  
Python 2.x programs to Python 3.x. Here is an idea to better  
address these separate goals.

Introduce %-interpolation for bytes. Support the following format
codes to aid in writing new code:

%b: insert arbitrary bytes (via __bytes__ or Py_buffer)

%[dox]: insert an integer, encoded as ASCII

%[eEfFgG]: insert a float, encoded as ASCII

%a: call ascii(), insert result

Add a command-line option, disabled by default, that enables the
following format codes:

%s: if the object has __bytes__ or Py_buffer then insert it.
Otherwise, call str() and encode with the 'ascii' codec

%r: call repr(), encode with the 'ascii' codec

%[iuX]: as per Python 2.x, for backwards compatibility

Introducing these extra codes and the command-line option will
provide a more gradual upgrade path. The next step in porting could
be to examine each %s inside bytes literals and decide if they
should either be converted to %b or if the literal should be
converted to a unicode literal. Any %r codes could likely be safely
changed to %a.





-1 overall.



Not worth the extra complexity in documentation and command line
parameters.



%s, since it cannot be used for strings of characters (str) anyway,
might as well be used for strings of bytes, and of necessity for
single-code-base porting, must be usable in that manner.



I would give +.5 to the idea of supporting %a in Python 3

I would give +.2 for %r as a synonym for %a in Python 3.



%r and %a don't produce fixed-width fields, so are likely used in
places where the exact length in bytes is flexible, and in ASCII
segments of the byte stream... supporting them both with the
semantics of %a might be useful.