Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Created December 12, 2023 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iTrooz/07200f67b3252015222e04044076596e to your computer and use it in GitHub Desktop.
Save iTrooz/07200f67b3252015222e04044076596e to your computer and use it in GitHub Desktop.
cleaner alternative syntax for python ctypes structures, allowing autocompletion
def binattrs(cls):
cls._fields_ = tuple(cls.__annotations__.items())
return cls
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