-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamedstruct.py
More file actions
30 lines (23 loc) · 834 Bytes
/
Copy pathnamedstruct.py
File metadata and controls
30 lines (23 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from collections import namedtuple
from struct import Struct, calcsize
class NamedStruct():
def __init__(self, decl, name='UnnamedStruct'):
fmt = ''
fields = []
for field in decl:
fmt += field[1]
if field[0]:
fields.append(field[0])
else:
fields.append(f'field_{calcsize(fmt)}')
self.nt = namedtuple(name, fields)
self.strukt = Struct(fmt)
self.size = self.strukt.size
def __repr__(self):
return f'{self.strukt.__repr__()}: {self.nt._fields}'
def unpack(self, data):
return self.nt._make(self.strukt.unpack(data))
def pack(self, *s):
return self.strukt.pack(*s)
def unpack_from(self, data, offset):
return self.nt._make(self.strukt.unpack_from(data, offset))