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

Uploaded Source

Built Distributions

cython_construct-2.5.7-py3.5-win-amd64.egg (563.6 kB view details)

Uploaded Source

cython_construct-2.5.7-py2.7-win-amd64.egg (501.7 kB view details)

Uploaded Source

cython_construct-2.5.7-cp35-cp35m-win_amd64.whl (501.0 kB view details)

Uploaded CPython 3.5m Windows x86-64

cython_construct-2.5.7-cp27-none-win_amd64.whl (503.2 kB view details)

Uploaded CPython 2.7 Windows x86-64

cython_construct-2.5.7-cp27-none-macosx_10_11_x86_64.whl (679.0 kB view details)

Uploaded CPython 2.7 macOS 10.11+ x86-64

cython-construct-2.5.7.win-amd64.zip (561.0 kB view details)

Uploaded Source

cython-construct-2.5.7.win-amd64-py2.7.msi (589.8 kB view details)

Uploaded Source

cython-construct-2.5.7.win-amd64-py2.7.exe (727.5 kB view details)

Uploaded Source

cython-construct-2.5.7.linux-x86_64.tar.gz (2.2 MB view details)

Uploaded Source

cython-construct-2.5.7.darwin.tar.gz (701.4 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for cython-construct-2.5.7.tar.gz
Algorithm Hash digest
SHA256 9d4aa20e8ca3fa9dc9ddc79907e0052c77a2bd9eb8ba3f0094f8f305d9845871
MD5 22ad90191d96ab37a29af7c0783965f9
BLAKE2b-256 1a1c17667b098a7ecc21dbd4baf187176bd28b75b83680648ca3cf7e3cc8cca5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_construct-2.5.7-py3.5-win-amd64.egg
Algorithm Hash digest
SHA256 b06c2d6d3845d5b3f73c7ac5d031e4466a0159d97166cfc8f4ecbd572a78bf07
MD5 03eae821d50819730f45be7a8ea72a46
BLAKE2b-256 d4b9bf0a59c262cb20945054c50441a5e5d237e063146a41213d22d091845a78

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.7-py3.5-macosx-10.11-x86_64.egg.

File metadata

File hashes

Hashes for cython_construct-2.5.7-py3.5-macosx-10.11-x86_64.egg
Algorithm Hash digest
SHA256 a621af767edac52e77a7e48a0deb4af0ccf9635dee6ba5f0be12f96b0b3b84fb
MD5 79f30b003281c8fa9bfac20a0a2c1c2e
BLAKE2b-256 d4678e2ff65cf5f9c40e65d115d0c9f024e5186500b43265fdb48d2ed41a0653

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.7-py3.4-linux-x86_64.egg.

File metadata

File hashes

Hashes for cython_construct-2.5.7-py3.4-linux-x86_64.egg
Algorithm Hash digest
SHA256 0026f125e75ce5018dbf650f3834b36b7bc751af2931788238bbebf787364a66
MD5 d59f3f4370c2cf4104201cabc296cce3
BLAKE2b-256 8eaaea588674534dc3254fa9ec36f67df968b59ff718ea25667f4da29d2b6e18

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.7-py2.7-win-amd64.egg.

File metadata

File hashes

Hashes for cython_construct-2.5.7-py2.7-win-amd64.egg
Algorithm Hash digest
SHA256 57d7326a0a5b8b167c615ce965235464b1d786ef35951d2e40445c77422af3e4
MD5 7ed793768c2e69f2ec29cde8c7960e63
BLAKE2b-256 b828bc49214f4a0cd750f082a06b5a2894abf7ab3b743a6326c1a14c74104bd6

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.7-py2.7-macosx-10.11-x86_64.egg.

File metadata

File hashes

Hashes for cython_construct-2.5.7-py2.7-macosx-10.11-x86_64.egg
Algorithm Hash digest
SHA256 f6eaac9e9893e1f1ed553a6a5e9f713f2e1c9e6db49c8b741458030cae7c2a33
MD5 a9c35187f57a0dfbd9ec4a3ce8d7884e
BLAKE2b-256 e57755bfaee4169f467e5fc906386b133db7606f1440f699e1d30d135e6e57ca

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.7-py2.7-linux-x86_64.egg.

File metadata

File hashes

Hashes for cython_construct-2.5.7-py2.7-linux-x86_64.egg
Algorithm Hash digest
SHA256 200abec3bb96485337c8474422e7bdcd13883cd12e26c55e97786054ff8abbf6
MD5 5761884c8781edc58b4a62264dbde0ee
BLAKE2b-256 eaa40c8d26323bd986cd0121ba5a8d01504ecdf03dd4d8f80701df5ce2200f41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_construct-2.5.7-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 fac2bc812f338ba673c8b90e19929080621405aaa0840616104acf5e5d3f1517
MD5 ba582f347accc569327335dfe09f746d
BLAKE2b-256 7b34e3fb512e8d8630c9177d98181f7c5a1bf59ff36c981e3fd3327ff414ddbf

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.7-cp27-none-win_amd64.whl.

File metadata

File hashes

Hashes for cython_construct-2.5.7-cp27-none-win_amd64.whl
Algorithm Hash digest
SHA256 182e6ca2eb3ff337420d4aa68e6cfb7ce6d1dacbd76b5b8e4e2a2b6bbbbeae81
MD5 392127be47e24b5104ae2abcec799101
BLAKE2b-256 cac5054fca00e5e8aeabdbba0f48a235d368407a11e14717484c54bc68e567bb

See more details on using hashes here.

File details

Details for the file cython_construct-2.5.7-cp27-none-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for cython_construct-2.5.7-cp27-none-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 710061ae8ffb5d7fe67b7296be03cc9bf82908863256ed7f06b545ba6eb240c8
MD5 59380c191cb566bafee31c32a884c483
BLAKE2b-256 a43031b57f5bd209b82b421b25064cd535ac8ee5d98479c615bf21e123131851

See more details on using hashes here.

File details

Details for the file cython-construct-2.5.7.win-amd64.zip.

File metadata

File hashes

Hashes for cython-construct-2.5.7.win-amd64.zip
Algorithm Hash digest
SHA256 80a9bec0fb2a07c414e146cb3303f217425aac951f5de1c39b116fa7e661e8c7
MD5 1244d4f63d4c4edf694447b9b7b7205e
BLAKE2b-256 324b07f2af562cbf1f69edc7398b6b51c83549db285b46aa44a1cb8182218d97

See more details on using hashes here.

File details

Details for the file cython-construct-2.5.7.win-amd64-py2.7.msi.

File metadata

File hashes

Hashes for cython-construct-2.5.7.win-amd64-py2.7.msi
Algorithm Hash digest
SHA256 b3f258b4a4e6edea47d7fcd18dfd3d3b68a4d05708d529a7aa330d0ec1154866
MD5 82941ebc31df52b6617759e039a315a6
BLAKE2b-256 bd745143d08e49ad575efe79c7c65ae43a1d370626ea3bf8ce09fce1659fe737

See more details on using hashes here.

File details

Details for the file cython-construct-2.5.7.win-amd64-py2.7.exe.

File metadata

File hashes

Hashes for cython-construct-2.5.7.win-amd64-py2.7.exe
Algorithm Hash digest
SHA256 40ec8958bf5de6dd32aa2bd743a5337227adcb6299abb3a00383045fb4d2cbf6
MD5 c6f15ade229687ec1767a1bf75543a85
BLAKE2b-256 b35ad5ce6feeec03ed59ae8df28e711938d899d6879b1ef4b480d20b1a1e9713

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython-construct-2.5.7.macosx-10.11-x86_64.tar.gz
Algorithm Hash digest
SHA256 4727bf29184ba9a15cf6cf39c5f8018b665d5ebeffafcce3682d8ad7c76ebe46
MD5 b5db28ef1562c108541fa76fa1bb7b24
BLAKE2b-256 ea5d3bf29e83f3f300916679c14d14d12ac8af864db31fa4fc9ac3e1c44dd56d

See more details on using hashes here.

File details

Details for the file cython-construct-2.5.7.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for cython-construct-2.5.7.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 7efa1f065970eaff7e7174b7f91b10539de589b4c3556937a9e018145a42bfa5
MD5 f8f945376ab6e3f9b4e4c818ce92572a
BLAKE2b-256 c0b3111d172cdf159084f3ad8ddd62ff6ebd6b908cb7db99f095e81924ebe916

See more details on using hashes here.

File details

Details for the file cython-construct-2.5.7.darwin.tar.gz.

File metadata

File hashes

Hashes for cython-construct-2.5.7.darwin.tar.gz
Algorithm Hash digest
SHA256 5ef49c4942df109b50fea6240af3e0d161f65c908588308ab8d39009c7ca798c
MD5 02456d3617f23f5829812c83a32688ff
BLAKE2b-256 169f79ca6addff9c4a76fc9da93495aac849677b83b2df74bf0bac5be6f7db66

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