Index: struct.py =================================================================== --- struct.py (revision 63456) +++ struct.py (working copy) @@ -1,2 +1,21 @@ from _struct import * from _struct import _clearcache +from _struct import Struct as _Struct +import collections, re + +_fmt_regex = re.compile(r'([a-zA-Z_][a-zA-Z_0-9]*)?\((\S+)\)') +class Struct(_Struct): + def __init__(self, format, name=None): + if name is None: + name = self.__class__.__name__ + attr_names, attr_formats = zip(*(m.groups() for m in + _fmt_regex.finditer(format))) + _Struct.__init__(self, ''.join(attr_formats)) + self._namedtuple = collections.namedtuple(name, ' '.join( + attr for attr in attr_names if attr)) + def unpack(self, data): + return self._namedtuple(*_Struct.unpack(self, data)) + +__all__ = [ + 'Struct', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from' +]