Library for parsing binary structures
On 30Mar2019 10:29, Paul Moore <p.f.moore at gmail.com> wrote:
>On Fri, 29 Mar 2019 at 23:21, Cameron Simpson <cs at cskk.id.au> wrote:
>> On 27Mar2019 18:41, Paul Moore <p.f.moore at gmail.com> wrote:
>> >I'm looking for a library that lets me parse binary data structures.
>> >The stdlib struct module is fine for simple structures, but when it
>> >gets to more complicated cases, you end up doing a lot of the work by
>> >hand (which isn't that hard, and is generally perfectly viable, but
>> >I'm feeling lazy ;-))
>> I wrote my own: cs.binary, available on PyPI. The PyPI page has is
>> module docs, which I think are ok:
>Nice, thanks - that's exactly the sort of pointer I was looking for.
>I'll take a look and see how it works for my use case.
I'd be happy to consider adapting some stuff for your use cases; as you
may imagine it is written to my use cases.
Also, I should point you at the cs.binary.structtuple factory, which
makes a class for those structures trivially defined with a struct
format string. As it uses struct for the parse step and transcribe
steps, so it should be performant. Here's an example from the
PDInfo = structtuple('PDInfo', '>LL', 'rate initial_delay')
which makes a PDInfo class for 2 big endian unsigned longs with .rate
and .initial_delay attributes.
Cameron Simpson <cs at cskk.id.au>