4 ms·
idk just spitballing I would maybe do something like from parser import struct, packed, array, u8, u32, u64 @struct(packed) class ASIF: magic: a
by saagarjha 4mo ago
idk just spitballing I would maybe do something like
from parser import struct, packed, array, u8, u32, u64
@struct(packed)
class ASIF:
magic: array[u8, 4]
field4: u32
field8: u32
fieldC: u32
field10: u64
field18: u64
field20: array[u8, 16]
field30: u64
field38: u64
field40: u32
field44: u32
field48: u32
field4C: u32
let asif = ASIF.from_bytes(...)
print(asif.fieldC)
- quietbritishjim 4mo agoI'll admit I do really like that. I still think it proves my point: your original objection was about the syntax being C-like and, as I predicted, the differences in syntax in your idea (where the type goes, colon vs positional, etc.) are all trivialities that don't affect usability. What's better about your idea is that it's actual Python code rather than being embedded in a string. Maybe that was your point originally and I misunderstood. Looks like this package works like this: https://harrymander.xyz/dataclasses-struct/ https://harrymander.xyz/dataclasses-struct/
- saagarjha 4mo agoYeah I don't think this is too unusual, I'm not surprised that someone else has come up with it too. My point is that if you write it in Python you can get things like type hints, and use language features to make things nicer. Obviously there isn't much you can do to skip the part where you have to list fields and their types but using a string of C syntax seems kind of bad in any language that's not C work working with it directly.