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, to 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 and sequential 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

  • Bitwise: splitting bytes into bit-grained fields

  • 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 and parse only what you require

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

Example

A Struct is a collection of ordered, named fields:

>>> format = Struct(
...     "signature" / Const(b"BMP"),
...     "width" / Int8ub,
...     "height" / Int8ub,
...     "pixels" / Array(this.width * this.height, Byte),
... )
>>> format.build(dict(width=3, height=2, pixels=[7,8,9,11,12,13]))
b'BMP\x03\x02\x07\x08\t\x0b\x0c\r'
>>> format.parse(b'BMP\x03\x02\x07\x08\t\x0b\x0c\r')
Container(signature=b'BMP', width=3, height=2, pixels=[7, 8, 9, 11, 12, 13])

A Sequence is a collection of ordered fields, and differs from a Range in that latter is homogenous:

>>> format = Sequence(PascalString(Byte, encoding="utf8"), GreedyRange(Byte))
>>> format.build([u"lalaland", [255,1,2]])
b'\nlalaland\xff\x01\x02'
>>> format.parse(b"\x004361789432197")
['', [52, 51, 54, 49, 55, 56, 57, 52, 51, 50, 49, 57, 55]]

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

Sticky

Version 2.5.5 is legacy. If you are maintaining a project that depended on this library for a long time, you should probably use this version. This branch is not actively maintained, not even bugfixes.

Version 2.8 was released on September, 2016. There are significant API and implementation changes. Fields are now name-less and operators / >> [] are used to create Structs Sequences and Ranges. Most classes changed interface and behavior. You should read entire documentation again.

Development and support

Please use the github issues to ask general questions, make feature requests, report issues and bugs, and to send in patches. Good quality extensions to test suite are highly welcomed. There is also a mailing list that was used for years but github issues should be preffered.

Main documentation is at construct.readthedocs.org, where you can find all kinds of examples. Source is on github. Releases are available on pypi.

Construct3 is a different project. It is a rewrite from scratch and belongs to another (previous) developer, it diverged from this project years ago. As far as I can tell, it was never released and abandoned.

Requirements

Construct should run on any Python 2.7 3.3 3.4 3.5 3.6 and pypy pypy3 implementation.

Best should be 3.6 because it supports ordered keyword arguments which comes handy when declaring Struct members or crafting Containers.

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 Distribution

construct-2.8.21.tar.gz (63.1 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for construct-2.8.21.tar.gz
Algorithm Hash digest
SHA256 c38d3600547930e49d5ca9210d72371fa67edbf13f5633295a613e3b80b77073
MD5 019ec7e9c5d839acf3bef61ae2f9ba37
BLAKE2b-256 26dc1a2f590fea5201eaac871c746fcc09e1e0c36d114a28f329ca1198ba4177

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