Get single-byte bytes objects from a bytes object

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).

Replace both _to_str and _to_bytes with:

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')
1 Like

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.

(Discourse doesn’t allow editing “Reply to”)

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.

1 Like