import newstruct as ns
...
x = ns.long()
> Cocoa classes begin with the acronym "NS" (standing either for the
> NeXT-Sun creation of OpenStep, or for the original proprietary term
> for the OpenStep framework, NeXTSTEP)
http://en.wikipedia.org/wiki/Cocoa_(API)#Cocoa_historyYou could have those fields know their own sizes, and then you could have Struct subclasses know the size of their instances, so you don't have to call sizeof. E.g.:
class DataHeader(Struct):
address = newstruct.long()
typ = newstruct.byte()
tag = newstruct.short()
data = newstruct.byte(length=40) # 40 bytes
>>> fp = open('mydata', 'rb')
>>> dh = DataHeader.read(fp)
# or perhaps: DataHeader.load(fp.read(len(dh)))
# this way the side-effect of moving fp's current position is more explicit
>>> fp.close()
>>> len(dh)
12345 # or whatever, sum of sizes for long, 41 bytes, and short[0]: http://construct.readthedocs.org/en/latest/
[1]: https://github.com/construct/construct
[2]: https://github.com/construct/construct/blob/master/construct...
Struct needs it because it is interpreting data. unpack needs to know the endianness to correctly interpret numeric types, and pack needs to know the endianness to know how to serialize.
FYI: the C way of doing this is byteswapping.