Skip to main content

A powerful declarative parser/builder for binary data

Project description

Construct is a powerful declarative parser (and builder) for binary data.

Instead of writing imperative code to parse a piece of data, you declaratively define a data structure that describes your data. As this data structure is not code, you can use it in one direction to parse data into Pythonic objects, and in the other direction, convert (“build”) objects into binary data.

The library provides both simple, atomic constructs (such as integers of various sizes), as well as composite ones which allow you form hierarchical structures of increasing complexity. Construct features bit and byte granularity, easy debugging and testing, an easy-to-extend subclass system, and lots of primitive constructs to make your work easier:

  • Fields: raw bytes or numerical types

  • Structs and Sequences: combine simpler constructs into more complex ones

  • Adapters: change how data is represented

  • Arrays/Ranges: duplicate constructs

  • Meta-constructs: use the context (history) to compute the size of data

  • If/Switch: branch the computational path based on the context

  • On-demand (lazy) parsing: read only what you require

  • Pointers: jump from here to there in the data stream

Example

A PascalString is a string prefixed by its length:

>>> from construct import *
>>>
>>> PascalString = Struct("PascalString",
...     UBInt8("length"),
...     Bytes("data", lambda ctx: ctx.length),
... )
>>>
>>> PascalString.parse("\x05helloXXX")
Container({'length': 5, 'data': 'hello'})
>>> PascalString.build(Container(length = 6, data = "foobar"))
'\x06foobar'

Instead of specifying the length manually, let’s use an adapter:

>>> PascalString2 = ExprAdapter(PascalString,
...     encoder = lambda obj, ctx: Container(length = len(obj), data = obj),
...     decoder = lambda obj, ctx: obj.data
... )
>>> PascalString2.parse("\x05hello")
'hello'
>>> PascalString2.build("i'm a long string")
"\x11i'm a long string"

See more examples of file formats and network protocols in the repository.

Resources

Construct’s homepage is http://construct.readthedocs.org, where you can find all kinds of docs and resources. The library itself is developed on github; please use github issues to report bugs, and github pull-requests to send in patches. For general discussion or questions, please use the new discussion group.

Requirements

Construct should run on any Python 2.5-3.3 implementation.

Its only requirement is six, which is used to overcome the differences between Python 2 and 3.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cython-construct-2.5.4.tar.gz (780.8 kB view details)

Uploaded Source

Built Distributions

cython_construct-2.5.4-cp34-cp34m-macosx_10_10_intel.whl (1.3 MB view details)

Uploaded CPython 3.4m macOS 10.10+ intel

cython_construct-2.5.4-cp27-none-macosx_10_10_intel.whl (1.2 MB view details)

Uploaded CPython 2.7 macOS 10.10+ intel

File details

Details for the file cython-construct-2.5.4.tar.gz.

File metadata

File hashes

Hashes for cython-construct-2.5.4.tar.gz
Algorithm Hash digest
SHA256 eb3e38e25ceb864aee86ff57c3f6c964e0b5730342187c077fc4cb08f572d585
MD5 d150654432002a4d16ee5e1592e0ced3
BLAKE2b-256 1c6f35ee6d665bd246eb9a1c653c9730857b601f0c064bc1919dd0fc343036a3

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.4-cp34-cp34m-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for cython_construct-2.5.4-cp34-cp34m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 6b8b9e499f98a667c1ab4d641bda856ca5129078df775b6b8e7847724dc9470f
MD5 bbb7cf129c47658abecd7fcbafcf1c25
BLAKE2b-256 0487def6f8e91e164407e2383310d81278760e96ef382f67f06f633cbc9d2c21

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.4-cp27-none-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for cython_construct-2.5.4-cp27-none-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 d9cdd3500f39d08c188613fd9a131c4d07718a5fbd8c8d77877ee19a89fa65ad
MD5 66cb4e7e4c99ca6306c3d68113130c22
BLAKE2b-256 9b7c0924c95be68bcb83711bbf45aba33fbcdb0b2ad100bfe8494d4f61997d29

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page