[Python-ideas] Python 3.x and bytes
Terry Reedy
tjreedy at udel.edu
Fri May 20 21:05:17 CEST 2011
On 5/20/2011 9:05 AM, Ethan Furman wrote:
> The header of a .dbf file details the field composition such as name,
> size, type, etc. The type is C for character, L for logical, etc, and
> the end of the field definition block is signaled by a CR byte.
At the level of bytes, these are small int codes. For English speakers,
it is convenient that most map to ascii chars that are the first letters
of an English name of the type. This convinience is somewhat lost for
non-English non-latin-alphabet speakers who cannot do the same.
> So in one spot of my code I (used to) have a comparison
>
> if hdr[0] == b'\x0d': # end of fields
>
> which I have changed to
>
> if hdr[0] == 0x0d:
Some people dislike magic constants in code and would suggest defining
them at the top of the file (or even in a separate module) with comment
that define and explain the protocol.
# Field type codes
T_log = ... # Logical field with T or F <or whatever>
T_char= ... # Variable length char field <or whatever>
T_efdb= 0x0d # End of field definition block
Take your pick of how to define the constants:
>>> 0x0d == 13 == 0o15 == 0b1101 == ord(b'\r') == ord('\r') == b'\r'[0]
True
In 3.x, the identifies and comments can use any characters and language,
so this works for everyone.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list