Core API: Abstract classes

class construct.Construct

The mother of all constructs.

This object is generally not directly instantiated, and it does not directly implement parsing and building, so it is largely only of interest to subclass implementors. There are also other abstract classes sitting on top of this one.

The external user API:

  • parse

  • parse_stream

  • parse_file

  • build

  • build_stream

  • build_file

  • sizeof

  • compile

  • benchmark

Subclass authors should not override the external methods. Instead, another API is available:

  • _parse

  • _build

  • _sizeof

  • _actualsize

  • _emitparse

  • _emitbuild

  • _emitseq

  • _emitprimitivetype

  • _emitfulltype

  • __getstate__

  • __setstate__

Attributes and Inheritance:

All constructs have a name and flags. The name is used for naming struct members and context dictionaries. Note that the name can be a string, or None by default. A single underscore “_” is a reserved name, used as up-level in nested containers. The name should be descriptive, short, and valid as a Python identifier, although these rules are not enforced. The flags specify additional behavioral information about this construct. Flags are used by enclosing constructs to determine a proper course of action. Flags are often inherited from inner subconstructs but that depends on each class.

class construct.Subconstruct(subcon)

Abstract subconstruct (wraps an inner construct, inheriting its name and flags). Parsing and building is by default deferred to subcon, same as sizeof.

Parameters:

subcon – Construct instance

class construct.Adapter(subcon)

Abstract adapter class.

Needs to implement _decode() for parsing and _encode() for building.

Parameters:

subcon – Construct instance

class construct.SymmetricAdapter(subcon)

Abstract adapter class.

Needs to implement _decode() only, for both parsing and building.

Parameters:

subcon – Construct instance

class construct.Validator(subcon)

Abstract class that validates a condition on the encoded/decoded object.

Needs to implement _validate() that returns a bool (or a truthy value)

Parameters:

subcon – Construct instance

class construct.Tunnel(subcon)

Abstract class that allows other constructs to read part of the stream as if they were reading the entire stream. See Prefixed for example.

Needs to implement _decode() for parsing and _encode() for building.

class construct.Compiled(parsefunc, buildfunc)

Used internally.