[Python-ideas] Add an option for delimiters in bytes.hex()
Steven D'Aprano
steve at pearwood.info
Tue May 2 07:31:48 EDT 2017
On Mon, May 01, 2017 at 11:38:20PM +1000, Nick Coghlan wrote:
> We're definitely open to offering better formatting options for bytes.hex().
>
> My proposal in https://bugs.python.org/issue22385 was to define a new
> formatting mini-language (akin to the way strftime works, but with a
> much simpler formatting mini-language):
> http://bugs.python.org/issue22385#msg292663
>
> However, a much simpler alternative would be to just support two
> keyword arguments to hex(): "delimiter" (as you suggest) and
> "chunk_size" (defaulting to 1, so you get per-byte chunking by
> default)
I disagree with this approach. There's nothing special about bytes.hex()
here, perhaps we want to format the output of hex() or bin() or oct(),
or for that matter "%x" and any of the other string templates?
In fact, this is a string operation that could apply to any character
string, including decimal digits.
Rather than duplicate the API and logic everywhere, I suggest we add a
new string method. My suggestion is str.chunk(size, delimiter=' ') and
str.rchunk() with the same arguments:
"1234ABCDEF".chunk(4)
=> returns "1234 ABCD EF"
rchunk will be useful for money or other situations where we group from
the right rather than from the left:
"$" + str(10**6).rchunk(3, ',')
=> returns "$1,000,000"
And if we want to add bells and whistles, we could accept a tuple for
the size argument:
# Format mobile phone number in the Australian style
"04123456".rchunk((4, 3))
=> returns "0412 345 678"
# Format an integer in the Indian style
str(123456789).rchunk((3, 2), ",")
=> returns "12,34,56,789"
In the OP's use-case:
bytes("abcde", "ascii").hex().chunk(2)
=> returns '61 62 63 64 65'
bytes("abcde", "ascii").hex().chunk(4)
=> returns '6162 6364 65'
I don't see any advantage to adding this to bytes.hex(), hex(), oct(),
bin(), and I really don't think it is helpful to be grouping the
characters by the number of bits. Its a string formatting operation, not
a bit operation.
--
Steve
More information about the Python-ideas
mailing list