Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider replacing binascii and struct with native Python methods #2327

Open
fariss opened this issue Aug 23, 2024 · 0 comments
Open

consider replacing binascii and struct with native Python methods #2327

fariss opened this issue Aug 23, 2024 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@fariss
Copy link
Collaborator

fariss commented Aug 23, 2024

Python offers native methods for string-byte conversion, eliminating the need for binascii. We should replace binascii.hexlify() with bytes.hex() and binascii.unhexlify() with bytes.fromhex().
https://docs.python.org/3/library/stdtypes.html#bytes.hex

also consider, replacing struct.pack / stuct.unpack() with native to_bytes / from_bytes methods:

struct.pack("<B", op_val) ⟶ op_val.to_bytes(1, byteorder='little', signed=False)
struct.pack("<H", op_val) ⟶ op_val.to_bytes(2, byteorder='little', signed=False)
struct.pack("<I", op_val) ⟶ op_val.to_bytes(4, byteorder='little', signed=False)
struct.pack("<Q", op_val) ⟶ op_val.to_bytes(8, byteorder='little', signed=False)

# similarly
struct.unpack(">I", x)[0] ⟶ int.from_bytes(x, 'little')

https://docs.python.org/3/library/stdtypes.html#int.to_bytes

@fariss fariss added enhancement New feature or request good first issue Good for newcomers labels Aug 23, 2024
@williballenthin williballenthin mentioned this issue Aug 23, 2024
24 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant