Skip to content

Instantly share code, notes, and snippets.

@tchebb
Last active September 13, 2025 22:45
Show Gist options
  • Save tchebb/680e929a931c80208a83ae7e19002d74 to your computer and use it in GitHub Desktop.
Save tchebb/680e929a931c80208a83ae7e19002d74 to your computer and use it in GitHub Desktop.
from collections import namedtuple
from struct import Struct
class StructTuple:
def _pack(self):
return self._struct.pack(*self)
@classmethod
def _unpack(cls, data):
return cls._make(cls._struct.unpack(data))
@classmethod
def _unpack_start(cls, data):
parsed = cls._unpack(data[:cls._struct.size])
remainder = data[cls._struct.size:]
return parsed, remainder
def structtuple(typename, field_names, struct_format):
cls = namedtuple(typename, field_names)
cls._struct = Struct(struct_format)
cls.__bases__ += (StructTuple,)
return cls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment