Macros and Structs - the same API?

94 views
Skip to first unread message

Robert Gomulka

unread,
Dec 11, 2014, 4:30:30 PM12/11/14
to const...@googlegroups.com
Hi all,
I'm just trying out very promising construct library, which I've just discovered.

I'm reading the docs, but can't find some pieces.

I'm going to define some "data types". Like (example):
STRING = Struct("STRING",
                          UBInt8("length"),
                          Bytes("data", lambda ctx: ctx.length),
)

I am going to receive list of formats and list of data - I will then build them together.
Like:
data = [1, "str", 2]
formats = [UBInt8, STRING, UBInt8]

I'm not interested in the name, so for 1st and 3rd element it would be:
formats[n]('foo').build(data[n])
But the Struct is already initialized (right?) and I can't "call" it.
What's the way to achieve that?

Additionally, when composing Struct of smaller Structs I don't know how to do that:
BIGGER = Struct("name",
  UBInt8("something"),
  STRING,
  STRING)

^^ that will fail
How can I put several structs in one struct?

Regards,
Robert

ceri...@gmail.com

unread,
Jan 9, 2015, 7:08:57 PM1/9/15
to const...@googlegroups.com

I am going to receive list of formats and list of data - I will then build them together.
Like:
data = [1, "str", 2]
formats = [UBInt8, STRING, UBInt8]

I'm not interested in the name, so for 1st and 3rd element it would be:
formats[n]('foo').build(data[n])
But the Struct is already initialized (right?) and I can't "call" it.
What's the way to achieve that?

Just pre-initialize an unsigned integer construct.

ubint8 = UBInt8("foo")

formats[n].build(data[n])
 
Additionally, when composing Struct of smaller Structs I don't know how to do that:
BIGGER = Struct("name",
  UBInt8("something"),
  STRING,
  STRING)

This code works for me.  What error are you seeing? 

Robert Gomulka

unread,
Jan 15, 2015, 4:13:47 AM1/15/15
to const...@googlegroups.com
Thank you for your input.
I've been able to restructure the code to fix the issue in my code.
I've tried to use two strings with the same name:

STRING = Struct("STRING",
                          UBInt8("length"),
                          Bytes("data", lambda ctx: ctx.length),
)
BIGGER = Struct("name",
  UBInt8("something"),
  STRING,
  STRING)
BIGGER.parse('\x01' * 1000)
exception:
OverwriteError: 'STRING' would be overwritten but allow_overwrite is False

As said above, I was able to fix that by providing unique names. Sometimes they just look artificial, when I'm using the same structs alone, but I can live with that.

Regards,
Robert

ceri...@gmail.com

unread,
Jan 21, 2015, 10:39:56 PM1/21/15
to const...@googlegroups.com
In general if you have unstructured data, you may want to use Sequence rather than Struct.  Sequence creates a ListContainer and doesn't require unique names for its subconstructs.

Arek Bulski

unread,
Sep 26, 2016, 11:44:15 AM9/26/16
to Construct
Gosh, you are reinventing PascalString.
Reply all
Reply to author
Forward
0 new messages