Ordered keyword arguments for subcon naming

36 views
Skip to first unread message

Sark Flynn

unread,
Apr 2, 2015, 7:54:36 AM4/2/15
to const...@googlegroups.com
After reading Survey-of-Construct3 I started thinking of a way to provide a better mechanism for naming sub-constructs.

After some tinkering, I came to using ordered-keyword-arguments. While this definitely violates basic Python concepts,
it provides an easy-to-use naming mechanism, only requiring every object created to have a serial number (that only
increases.)

To provide a usable demo, I wrote a wrapper module around construct. Here is a simple example showing a construct struct,
and my new equivalent:

from easy_construct import cs, struct
from construct import *

MyStruct = Struct("MyStruct",
                  Magic("EZConstruct"),
                  UBInt32("variable"),
                  UBInt16("another_var"),
                  Padding(0x4),
                  Bytes("array", 13),
                  Magic("MagicEndsHere"),
                  )

MyEasyStruct = struct("MyStruct",
                  _0=cs.Magic("EZConstruct"),
                  variable=cs.UBInt32,
                  another_var=cs.UBInt16,
                  _1=cs.Padding(0x4),
                  array=cs.Bytes(13),
                  _2=cs.Magic("MagicEndsHere"),
                  )

sample_data = 'EZConstruct\xde\xad\xbe\xef\x137\x00\x00\x00\x00Hello, World!MagicEndsHere'

if __name__ == '__main__':
    print MyEasyStruct.parse(sample_data)
    print MyEasyStruct.parse(sample_data) == MyStruct.parse(sample_data)



The easy_construct module is included in the attached files to keep this post short.

What do you think?

easy_construct.py
demo.py

Arek Bulski

unread,
Sep 26, 2016, 11:09:41 AM9/26/16
to Construct
You should look at 2.8 where / operator is used for naming and all fields are name-less. 

I shall change the ctor of Struct to take both a list and kw members in. Note however that kw is unordered before Python 3.6 or so, which is still in beta.

Arek Bulski

unread,
Sep 26, 2016, 11:11:30 AM9/26/16
to Construct
Reply all
Reply to author
Forward
0 new messages