A list comprehension is exactly what I’m trying to avoid.
If you are going to work with ASCII characters, you really need a prebuilt ASCII table.
I do have one, but using that to test itself would be silly.
The problem is not that I don’t know of anyway to turn a bytes object into multiple single-byte ones, but a little bit of convenience that would have been nice to have.
The package is here, the testcases here and the prebuilt map there. If you have something to complain about my code, I would be glad if you could open an issue.
Your question should be “How do I encode ‘\x80’ to a single byte?”.
Byte 128 (‘\x80’) is encoded as two characters in Unicode. You should use the ‘latin-1’ character set to be able to represent bytes in the range(0, 256).
def _to_str(value):
return value if isinstance(value, str) else value.decode('latin-1')
def _to_bytes(value):
return value if isinstance(value, bytes) else value.encode('latin-1')
Your question should be “How do I encode ‘\x80’ to a single byte?”.
No. As I said:
The problem is not that I don’t know of anyway to turn a bytes object into multiple single-byte ones, but a little bit of convenience that would have been nice to have.
While I do appreciate you opening an issue for that at the GitHub repo, “encod[ing] '\x80' to a single byte” is a completely irrelevant to the matter at hand, “get single-byte bytes objects from a bytes object”. I assure you this is purely a feature request and not an XY problem; the post will not be closed even if the package works wonder.
I didn’t see where the to_list_of_single_bytes method would be helpful in your library; that would be the root problem (Y or Why?). So, it is not an XY problem.
This brings us back to post 6:
Karl Knechtel, post:6
The real question is what subsequent problem you hope to solve by getting this result, and how common that need is.
That’s the cause, yes, but considering that the library works for 3.10+ and not just 3.13 or 3.14 (if PEP 467 gets approved), I don’t think that would help a lot with the current implementation of the library.
It is a nice-to-have feature that I’m requesting. This StackOverflow question has ~83k views and ~70 upvotes as of writing, so I can safely say that the problem is somewhat common.
I have a similar use case. I want to compare bytes sent from an Arduino with byte literals. For example, reading 2 consecutive bytes that represent a command from the Arduino:
ser = serial.Serial(...)
com, data = tuple(map(int.to_bytes, ser.read(2)))
assert com == b'A'
or
com, data = ser.read(2)
assert com == int.from_bytes(b'A')
After looking at what others have done in similar applications, I ended up giving up on the idea of equating to byte literals and turned everything to integers. Then used integer constants to make the code readable.
E.g.
COMMAND1 = ord('A')
ser = serial.Serial(...)
com, data = ser.read(2)
assert com == COMMAND1