Skip to main content

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.

Release files for construct 2.5.1

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

Source distributions (sdists)

Source distribution for construct 2.5.1
File Size Uploaded
construct-2.5.1.zip 75.9 kB Details
construct-2.5.1.tar.gz 56.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for construct 2.5.1
File Interpreter ABI Platform
construct-2.5.1.win32.exe Details

Total release size: 401.9 kB

Release files / construct-2.5.1.zip

Download URL construct-2.5.1.zip
Size 75.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9d123b2590a5645385cf881f25960a2e2a262337e4697dfdbcac4b4496da44b3
BLAKE2b-256 checksum
How to use checksums
30fb555abffe84e89c0a464809ba3fd49a0af1d64e58d507b69709259045b939
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / construct-2.5.1.tar.gz

Download URL construct-2.5.1.tar.gz
Size 56.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5ae387cfde856f9ce3b1f4b1fc58750b6dc30994d96bde2517cfe47d10d51323
BLAKE2b-256 checksum
How to use checksums
a30c228d95226ee8a09ee6e3ba329008286612628dfac3bea4ab33cadb189256
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / construct-2.5.1.win32.exe

Download URL construct-2.5.1.win32.exe
Size 269.7 kB
Tags Source
SHA-256 checksum
How to use checksums
14eee8ff1d30bc75b8024796693ec8bc25a61b3323f6bfd4c0768f08acae19df
BLAKE2b-256 checksum
How to use checksums
7be3b622c56268b6f458e5f8f19b70025ce520fc56be5c14881742e7eadce74d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

2.10.68

1 release file

2.10.67

1 release file

2.10.64

1 release file

2.10.63

1 release file

2.10.61

1 release file

2.10.60

1 release file

2.10.58

1 release file

2.10.57

1 release file

2.10.56

1 release file

2.10.54

1 release file

2.10.53

1 release file

2.9.52

1 release file

2.9.51

1 release file

2.9.50

1 release file

2.9.49

1 release file

2.9.48

1 release file

2.9.47

1 release file

2.9.46

1 release file

2.9.45

1 release file

2.9.44

1 release file

2.9.43

1 release file

2.9.42

1 release file

2.9.41

1 release file

2.9.40

1 release file

2.9.39

1 release file

2.9.38

1 release file

2.9.37

1 release file

2.9.36

1 release file

2.9.35

1 release file

2.9.34

1 release file

2.9.33

1 release file

2.9.32

1 release file

2.9.31

1 release file

2.9.30

1 release file

2.9.29

1 release file

2.9.28

1 release file

2.9.27

1 release file

2.9.26

1 release file

2.9.25

1 release file

2.9.24

1 release file

2.9.23

1 release file

2.8.22

1 release file

2.8.21

1 release file

2.8.20

1 release file

2.8.19

1 release file

2.8.18

1 release file

2.8.17

1 release file

2.8.16

1 release file

2.8.15

1 release file

2.8.14

1 release file

2.8.13

1 release file

2.8.12

1 release file

2.8.11

1 release file

2.8.10

2 release files

2.8.8

1 release file

2.8.7

1 release file

2.8.6

1 release file

2.8.5

1 release file

2.8.4

1 release file

2.8.3

1 release file

2.8.2

1 release file

2.5.5

1 release file

2.5.4

1 release file

2.5.3

1 release file

2.5.2

3 release files

This release

2.5.1 This release

3 release files

2.5.0

3 release files

2.0.6

1 release file

2.0.5

1 release file

2.0.4

1 release file

2.0.0

1 release file

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