ConfigParser: special handling for bytes

A project I’m working on is using config files to store dictionaries of commands & associated bytes for various plotter devices.

Currently, ConfigParser stores bytes as a string representation, so it translates our bytes like so:

b’\x01’ → b"b’\\x01’"

It’s definitely not something we can just call bytes() on to convert back to bytes, and it’s not easy to store a raw byte in arbitrary encodings, but we can define a byte representation which ConfigParser can use. The easiest I can think of is converting the byte(s) to hex and then adding a simple structure around the value to help us parse bytes while reading the file. I think the following would work well:

Bytes

bytes → bytes(hex)

Example:

b’\x01’ → bytes(01)

bytearrays

bytearray → bytearray(hex)

Example:

bytearray(b’\x01\x02’) → bytearray(0102)

I’m probably going to wind up hacking around ConfigParser to implement this for my own project, but I’m curious if this might be something worth implementing in ConfigParser directly