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.

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.8.tar.gz (798.8 kB view details)

Uploaded Source

Built Distributions

cython_construct-2.5.8-py3.5-win-amd64.egg (499.7 kB view details)

Uploaded Source

cython_construct-2.5.8-cp35-cp35m-win_amd64.whl (501.1 kB view details)

Uploaded CPython 3.5m Windows x86-64

cython_construct-2.5.8-cp35-cp35m-macosx_10_11_x86_64.whl (681.0 kB view details)

Uploaded CPython 3.5m macOS 10.11+ x86-64

File details

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

File metadata

File hashes

Hashes for cython-construct-2.5.8.tar.gz
Algorithm Hash digest
SHA256 4de90d3ddd3cb7caa6acf67af74971d7a00168a0073319688b82dac535ccd374
MD5 dc86764d4f55f056bf9e0e46c2765e87
BLAKE2b-256 987e5d1635e7bc9a5e4aabbf3566f966ddff9026ce9724a733e580737ad8430b

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.8-py3.5-win-amd64.egg.

File metadata

File hashes

Hashes for cython_construct-2.5.8-py3.5-win-amd64.egg
Algorithm Hash digest
SHA256 83db7c8e19e9cfc816a856e509660cec31203591df55c9fc9cdf6942e193c52d
MD5 e6788154a9f900f9e3bc27829fc97a1c
BLAKE2b-256 eb81f27428601bfbf5f37893992a416f3892b03c3d845a7a0fe3b7b544e0b913

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.8-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for cython_construct-2.5.8-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 b014b5b1e4d9233b942af0f744d290fec325955389f155ee78f2b05ca8cde101
MD5 4f37ab57a18a0c533bf0a72fa8124697
BLAKE2b-256 41f6319ada77d400e5440cdf2f444c0ca847dd16d6a396ff478b3d59ef85dc95

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.8-cp35-cp35m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for cython_construct-2.5.8-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 d56ba05c88f6a6aac1c3fcdfd503b2174370d3e22edbdb9e50b4ec7cdcfc97ca
MD5 f5229cfb405ee04f57adb85fe413d0e8
BLAKE2b-256 73d66e845fe8b5c135a25b18e0f0d2d212148962b6d0382f61a156467644c502

See more details on using hashes here.

File details

Details for the file cython-construct-2.5.8.macosx-10.11-x86_64.tar.gz.

File metadata

File hashes

Hashes for cython-construct-2.5.8.macosx-10.11-x86_64.tar.gz
Algorithm Hash digest
SHA256 ac3f0bd553141dbf9389144463063ca7a150e8d290d60bc8e6f50a8c877bdfd5
MD5 68598c3dc86e9f736bbde2741a170272
BLAKE2b-256 8c96d56f5b04de31ea9eda847121ebc95e5fff47b518930772b0659ec256bc6a

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