Skip to main content

Construct2

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.

Metadata

Release files for cython-construct 2.5.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cython-construct 2.5.4
File Size Uploaded Provenance Actions
cython-construct-2.5.4.tar.gz 780.8 kB Without provenance Details for cython-construct-2.5.4.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for cython-construct 2.5.4
File Interpreter ABI Platform Actions
cython_construct-2.5.4-cp34-cp34m-macosx_10_10_intel.whl CPython 3.4 CPython 3.4 pymalloc macOS 10.10+ Intel (x86-64, i386) Details for cython_construct-2.5.4-cp34-cp34m-macosx_10_10_intel.whl
cython_construct-2.5.4-cp27-none-macosx_10_10_intel.whl CPython 2.7 none macOS 10.10+ Intel (x86-64, i386) Details for cython_construct-2.5.4-cp27-none-macosx_10_10_intel.whl

Total release size: 3.2 MB

Release files / cython-construct-2.5.4.tar.gz

Download URL cython-construct-2.5.4.tar.gz
Size 780.8 kB
Tags Source
SHA-256 checksum
How to use checksums
eb3e38e25ceb864aee86ff57c3f6c964e0b5730342187c077fc4cb08f572d585
BLAKE2b-256 checksum
How to use checksums
1c6f35ee6d665bd246eb9a1c653c9730857b601f0c064bc1919dd0fc343036a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / cython_construct-2.5.4-cp34-cp34m-macosx_10_10_intel.whl

Download URL cython_construct-2.5.4-cp34-cp34m-macosx_10_10_intel.whl
Size 1.3 MB
Tags CPython 3.4 CPython 3.4 pymalloc macOS 10.10+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
6b8b9e499f98a667c1ab4d641bda856ca5129078df775b6b8e7847724dc9470f
BLAKE2b-256 checksum
How to use checksums
0487def6f8e91e164407e2383310d81278760e96ef382f67f06f633cbc9d2c21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / cython_construct-2.5.4-cp27-none-macosx_10_10_intel.whl

Download URL cython_construct-2.5.4-cp27-none-macosx_10_10_intel.whl
Size 1.2 MB
Tags CPython 2.7 macOS 10.10+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
d9cdd3500f39d08c188613fd9a131c4d07718a5fbd8c8d77877ee19a89fa65ad
BLAKE2b-256 checksum
How to use checksums
9b7c0924c95be68bcb83711bbf45aba33fbcdb0b2ad100bfe8494d4f61997d29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for cython-construct 2.5.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page