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


Release history Release notifications | RSS feed

Download files

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

Source Distributions

construct-2.5.2.zip (76.1 kB view details)

Uploaded Source

construct-2.5.2.tar.gz (58.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

construct-2.5.2-py2.py3-none-any.whl (73.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file construct-2.5.2.zip.

File metadata

  • Download URL: construct-2.5.2.zip
  • Upload date:
  • Size: 76.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for construct-2.5.2.zip
Algorithm Hash digest
SHA256 4b750b5410e137cdcd167c52de290ac7212bf4d2b8968743fc3f4dbaadce4241
MD5 a0ae23ca1d6a42e09cd1afa2484b61b6
BLAKE2b-256 b3c3d5a2cc64a2044a77def737499b6b273a9481a3540e85d43ae11e72c6c241

See more details on using hashes here.

File details

Details for the file construct-2.5.2.tar.gz.

File metadata

  • Download URL: construct-2.5.2.tar.gz
  • Upload date:
  • Size: 58.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for construct-2.5.2.tar.gz
Algorithm Hash digest
SHA256 665b6271eeadf15219c726b180c8d7a641d026784d72ca3dad90a20aae009020
MD5 75fc5e311ee5624e63268067e4b0d97b
BLAKE2b-256 fd5ae3105c79b3b0bbf7a80dc8218d7416df6551f9f2fe389c9ce6690a621c00

See more details on using hashes here.

File details

Details for the file construct-2.5.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for construct-2.5.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 528022ced585c2694e4523d6a4c1e90d979b4a9f8b758402c1ff74bf86e32a89
MD5 ea5e648645f07d275405f98623a67484
BLAKE2b-256 14d7f94299cc597f74c558907aa76c635966b029d5c58d8cda7ecc4035fb8ab4

See more details on using hashes here.

Supported by

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