construct 2.10.70


pip install construct

  Latest version

Released: Nov 29, 2023


Meta
Author: Arkadiusz Bulski, Tomer Filiba, Corbin Simpson
Requires Python: >=3.6

Classifiers

Development Status
  • 5 - Production/Stable

License
  • OSI Approved :: MIT License

Intended Audience
  • Developers

Topic
  • Software Development :: Libraries :: Python Modules
  • Software Development :: Build Tools
  • Software Development :: Code Generators

Programming Language
  • Python :: 3
  • Python :: 3.6
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: Implementation :: CPython
  • Python :: Implementation :: PyPy

Construct is a powerful declarative and symmetrical 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

  • Tunneling: prefix data with a byte count or compress it

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 Array and GreedyRange in that those two are homogenous:

>>> format = Sequence(PascalString(Byte, "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]]
2.10.70 Nov 29, 2023
2.10.69 Oct 22, 2023
2.10.68 Feb 21, 2022
2.10.67 Apr 21, 2021
2.10.66 Apr 03, 2021
2.10.65 Apr 03, 2021
2.10.64 Mar 30, 2021
2.10.63 Mar 24, 2021
2.10.61 Feb 20, 2021
2.10.60 Feb 12, 2021
2.10.59 Feb 06, 2021
2.10.58 Jan 27, 2021
2.10.57 Jan 26, 2021
2.10.56 Jan 28, 2020
2.10.54 Jan 24, 2020
2.10.53 Jan 19, 2020
2.9.52 Jan 19, 2020
2.9.51 Jan 18, 2020
2.9.50 Jan 17, 2020
2.9.49 Jan 17, 2020
2.9.48 Jan 17, 2020
2.9.47 Jan 16, 2020
2.9.46 Jan 16, 2020
2.9.45 Apr 09, 2018
2.9.44 Apr 06, 2018
2.9.43 Mar 31, 2018
2.9.42 Mar 28, 2018
2.9.41 Mar 26, 2018
2.9.40 Mar 23, 2018
2.9.39 Mar 16, 2018
2.9.38 Mar 12, 2018
2.9.37 Mar 10, 2018
2.9.36 Mar 05, 2018
2.9.35 Feb 28, 2018
2.9.34 Feb 22, 2018
2.9.33 Feb 20, 2018
2.9.32 Feb 18, 2018
2.9.31 Feb 17, 2018
2.9.30 Feb 12, 2018
2.9.29 Feb 11, 2018
2.9.28 Feb 08, 2018
2.9.27 Feb 03, 2018
2.9.26 Feb 01, 2018
2.9.25 Jan 30, 2018
2.9.24 Jan 29, 2018
2.9.23 Jan 28, 2018
2.8.22 Jan 18, 2018
2.8.21 Jan 13, 2018
2.8.20 Jan 03, 2018
2.8.19 Jan 03, 2018
2.8.18 Jan 03, 2018
2.8.17 Dec 08, 2017
2.8.16 Oct 01, 2017
2.8.15 Oct 01, 2017
2.8.14 Aug 29, 2017
2.8.13 Aug 28, 2017
2.8.12 Apr 24, 2017
2.8.11 Apr 05, 2017
2.8.10 Dec 31, 2016
2.8.8 Oct 20, 2016
2.8.7 Oct 07, 2016
2.8.6 Oct 04, 2016
2.8.5 Sep 30, 2016
2.8.4 Sep 25, 2016
2.8.3 Sep 23, 2016
2.8.2 Sep 23, 2016
2.5.5 Sep 10, 2016
2.5.4 Sep 08, 2016
2.5.3 Aug 22, 2016
2.5.2 Apr 28, 2014
2.5.1 May 04, 2013
2.5.0 Jan 17, 2013
2.0.6 May 05, 2013
2.0.5 May 05, 2013
2.0.4 May 05, 2013
2.0.0 May 04, 2013
2.5.1.win32 May 04, 2013
2.5.0.win32 Jan 17, 2013

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies: