[Python-Dev] Python 3.x and bytes

Nick Coghlan ncoghlan at gmail.com
Mon Jun 13 06:42:33 CEST 2011


On Mon, Jun 13, 2011 at 3:18 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>
> This is not beautiful code.

Agreed, but:

EOH, CHAR, DATE, FLOAT, INT, LOGICAL, MEMO, NUMBER = b'\rCDFILMN'

is a shorter way to write the same thing.

Going two per line makes it easier to mentally map the characters:

EOH, CHAR = b'\rC'
DATE, FLOAT = b'DF'
INT, LOGICAL = b'IL'
MEMO, NUMBER = b'MN'

Or, as a variant on Martin's solution:

FORMAT_CHARS = dict(
  EOH = '\r',
  CHAR= 'C',
  DATE = 'D',
  FLOAT = 'F',
  INT = 'I',
  LOGICAL = 'L',
  MEMO = 'M',
  NUMBER = 'N'
)

FORMAT_CODES = {name : char.encode('ascii') for name, char in FORMAT_CHARS}
globals().update(FORMAT_CODES)

Sure, there's no "one obvious way" at this stage, but that's because
we don't know yet if there even *should* be an obvious way to do this
(as conflating text and binary data is a bad idea in principle). By
not blessing any one way of handling the situation, we give
alternative solutions time to evolve naturally. If one turns out to be
clearly superior to the decode/process/encode cycle then hopefully
that will become clear at some point in the future.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list