[Python-ideas] Bytes formatting (was Re: Adding 'bytes' as alias for 'latin_1' codec)

Terry Reedy tjreedy at udel.edu
Tue May 31 20:18:03 CEST 2011


On 5/30/2011 10:11 PM, MRAB wrote:
> On 30/05/2011 21:04, Terry Reedy wrote:

>> Option 3. Combine options 1 and 2. This might best be done by replacing
>> the omitted 'conversion' field with a 'number-encoding' field, b'!a' or
>> b'!b', to indicate ascii or binary conversion and corresponding
>> interpretation of the format spec. (In other words, do not try to
>> combine the number to text and number to binary mini-languages, but add
>> a 'prefix' to specify which is being used.)

Unless someone has a better idea of how to combine than I do ;-).

> Perhaps something like this:
>
> # Format int as byte.
> b"{:b}".format(128) returns b"\x80"
>
> # Format int as double-byte.
> b"{:2b}".format(0x100) returns b"\x00\x01" or b"\x01\x00"
>
> # Format int as double-byte, little-endian.
> b"{:<2b}".format(0x100) returns b"\x00\x01"
>
> # Format int as double-byte, big-endian.
> b"{:>2b}".format(0x100) returns b"\x01\x00"
>
> # Format list of ints as signed bytes.
> b"{:s}".format([1, -2, 3]) returns b"\x01\xFE\x03"
>
> # Format list of ints as unsigned bytes.
> b"{:u}".format([1, 254, 3]) returns b"\x01\xFE\x03"
>
> # Format ASCII-only string as bytes.
> b"{:a}".format("abc") returns b"abc"

Interesting. The core ideas of my proposal are

* There are bytes construction cases not sensibly handled by test 
interpolation followed by encoding. Bytes concatenation and bytearray 
manipulation may be awkward, or follow patterns that can usefully be 
captures in a new function.

* Bytes interpolation should only deal with bytes and maybe ints and 
have nothing to do with text encoding.

* Design details should be based on use cases and experimentation with 
suggestions such as the above by people who would be the users of such a 
function.

Experimental functions should be uploaded to pypi.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list