introducing Construct

tomerfiliba at gmail.com tomerfiliba at gmail.com
Wed Mar 29 01:00:23 CEST 2006


"Construct -- parsing made fun"

info, demos, and download at:
http://pyconstruct.sourceforge.net

------------------------
about:
------------------------
Construct is a library for declaratively defining parsers and builders
for arbitrary data structures. Constructs are symmetrical: they can do
both parsing and building. Using the vast number of provided
primitives, you can easily define your data structures, including
advanced concepts like meta-constructs, as shown below. And it's
declarative -- so you don't need to write any code for the common
cases. You can also easily subclass Construct and write user-defined
constructs.

Unlike most parsers, Construct works at the bit-level, which means you
can easily parse unaligned fields, or work with bit fields of arbitrary
lengths. The library supports Fields, Structs, Unions, and Repeaters;
Adapters and Validators;  Switches, Pointers and other Meta-constructs.

To show its power, I provided a fully functional ELF32 file parser,
WITHOUT ONE LINE OF PROCEDURAL CODE. It's all declerative. I used it to
parse python23.o (2.1MB), in 2.88 sec on my machine.

The library comes with a demos folder, an "inventory" of useful
ready-made protocols, and is fully documented with doc-strings, for
easy viewing with pydoc and the like.

------------------------
examples:
------------------------
from Construct import *

#
# simple structures
#
ethernet_header = Struct("ethernet_header",
    Bytes("destination", 6),
    Bytes("source", 6),
    UInt16("type"),
)
print ethernet_header.parse("123456ABCDEF\x08\x00")

#
# meta constructs -- uses meta data
#
tlv = Struct("tlv",
    Byte("type"),
    Byte("length"),
    MetaBytes("value", "_.length"),
)
print ethernet_header.parse("\x01\x05ABCDE")


------------------------

-tomer



More information about the Python-announce-list mailing list