Last active
September 13, 2025 22:45
-
-
Save tchebb/680e929a931c80208a83ae7e19002d74 to your computer and use it in GitHub Desktop.
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
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