Created
December 12, 2023 12:07
-
-
Save iTrooz/07200f67b3252015222e04044076596e to your computer and use it in GitHub Desktop.
cleaner alternative syntax for python ctypes structures, allowing autocompletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binattrs(cls): | |
cls._fields_ = tuple(cls.__annotations__.items()) | |
return cls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes as ct | |
from binattrs import binattrs | |
@binattrs | |
class Hello(ct.Structure): | |
field1: ct.c_uint16 | |
field2: ct.c_uint16 | |
h = Hello.from_buffer_copy(bytes([0x01, 0x02, 0x03, 0x04])) | |
print(h.field1, h.field2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment