Skip to main content

Caterpillar - 🐛

python Latest Version Build and Deploy Docs Run Tests GitHub issues GitHub License

Caterpillar is a Python 3.12+ library to pack and unpack structurized binary data (with support for 3.10+). It enhances the capabilities of Python Struct by enabling direct class declaration. More information about the different configuration options will be added in the future. Documentation is here >.

Caterpillar is able to:

  • Pack and unpack data just from processing Python class definitions (including support for powerful bitfields, c++-like templates and c-like unions!),
  • apply a wide range of data types (with endianess and architecture configuration),
  • dynamically adapt structs based on their inheritance layout,
  • reduce the used memory space using __slots__,
  • allowing you to place conditional statements into class definitions,
  • insert proper types into the class definition to support documentation and
  • it helps you to create cleaner and more compact code.
  • There is also a feature that lets you dynamically change the endian within a struct!
  • You can even extend Caterpillar and write your parsing logic in C or C++
  • All struct definitions can be typing compliant!!! (tested with pyright)

Give me some code!

The following code is typing compliant, meaning your static type checker won't scream at you when developing with this code.

If you want to check out the default syntax, open this block.
from caterpillar.py import *
from caterpillar.types import *

@bitfield(order=LittleEndian)
class Header:
    version : 4                   # 4bit integer
    valid   : 1                   # 1bit flag (boolean)
    ident   : (8, CharFactory)    # 8bit char
    # automatic alignment to 16bits

THE_KEY = b"ITS MAGIC"

@struct(order=LittleEndian, kw_only=True)
class Format:
    magic  : THE_KEY                      # Supports string and byte constants directly
    header : Header
    a      : uint8                        # Primitive data types
    b      : Dynamic + int32              # dynamic endian based on global config
    length : uint8                        # String fields with computed lengths
    name   : String(this.length)          #  -> you can also use Prefixed(uint8)

    # custom actions, e.g. for hashes
    _hash_begin : DigestField.begin("hash", Md5_Algo)
    # Sequences with prefixed, computed lengths    -+ part of the MD5 hash
    names       : CString[uint8::]               #  |
    #                                              -+
    # automatic hash creation and verification + default value
    hash        : Md5_Field("hash", verify=True)

# Creation, packing and unpacking remains the same
from caterpillar.py import *
from caterpillar.types import *

@bitfield(order=LittleEndian)
class Header:
    version : int4_t                   # 4bit integer
    valid   : int1_t                   # 1bit flag (boolean)
    ident   : f[str, (8, CharFactory)] # 8bit char
    # automatic alignment to 16bits

THE_KEY = b"ITS MAGIC"

@struct(order=LittleEndian, kw_only=True)
class Format:
    magic  : f[bytes, THE_KEY] = THE_KEY  # Supports string and byte constants directly
    header : Header
    a      : uint8_t                      # Primitive data types
    b      : f[int, Dynamic + int32]      # dynamic endian based on global config
    length : uint8_t                      # String fields with computed lengths
    name   : f[str, String(this.length)]  #  -> you can also use Prefixed(uint8)

    # custom actions, e.g. for hashes
    _hash_begin : f[None, DigestField.begin("hash", Md5_Algo)] = None
    # Sequences with prefixed, computed lengths    -+ part of the MD5 hash
    names       : f[list[str], CString[uint8::]] #  |
    #                                              -+
    # automatic hash creation and verification + default value
    hash        : f[bytes, Md5_Field("hash", verify=True)] = b""

# Creation (keyword-only arguments, magic is auto-inferred):
obj = Format(
    header=Header(version=2, valid=True, ident="F"),
    a=1,
    b=2,
    length=3,
    name="foo",
    names=["a", "b"]
)

# Packing the object; reads as 'PACK obj FROM Format'
# objects of struct classes can be packed right away
data_le = pack(obj, Format)
# results in: b'ITS MAGIC0*\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00)\x9a...'

# Unpacking the binary data, reads as 'UNPACK Format FROM blob'
obj2 = unpack(Format, data_le)
assert obj2.names == obj.names

# to pack with a different endian for fields 'a' and 'b', use 'order'
data_be = pack(obj, Format, order=BigEndian)
assert data_le != data_be

[!NOTE] Python 3.14 changes when class-body __annotations__ are available. Digest context managers still need the explicit DigestField form on Python 3.14+, but conditional with blocks are supported through explicit metadata: with If(condition) as when: plus f[..., when] for one field or Start(when) / End(when) for a block. For conditional variants of one attribute, use Branch(When(...), Otherwise(...)).

This library offers extensive functionality beyond basic struct handling. For further details on its powerful features, explore the official documentation, examples, and test cases.

Installation

[!NOTE] As of Caterpillar v2.1.2 it is possible to install the library without the need of compiling the C extension.

PIP installation (Python-only)

pip install caterpillar-py

Python-only installation

pip install "caterpillar[all]@git+https://github.com/MatrixEditor/caterpillar"

Installation + C-extension

pip install "caterpillar[all]@git+https://github.com/MatrixEditor/caterpillar/#subdirectory=src/ccaterpillar"

Starting Point

Please visit the Documentation, it contains a complete tutorial on how to use this library.

Other Approaches

A list of similar approaches to parsing structured binary data with Python can be taken from below:

The documentation also provides a Comparison to these approaches.

License

Distributed under the GNU General Public License (V3). See License for more information.

Metadata

Release files for caterpillar-py 2.10.0

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

Source distribution (sdist)

Source distribution for caterpillar-py 2.10.0
File Size Uploaded
caterpillar_py-2.10.0.tar.gz 137.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for caterpillar-py 2.10.0
File Interpreter ABI Platform
caterpillar_py-2.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 308.1 kB

Release files / caterpillar_py-2.10.0.tar.gz

Download URL caterpillar_py-2.10.0.tar.gz
Size 137.3 kB
Tags Source
SHA-256 checksum
How to use checksums
167c0c9b946d922e2a0897ddc8babb0d2a208ef4df49cbddeec71fc89bcbf21a
BLAKE2b-256 checksum
How to use checksums
b14d2d55d88203922b582faef1281e3aedab5e32a582143f4feda51c898a5fa9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / caterpillar_py-2.10.0-py3-none-any.whl

Download URL caterpillar_py-2.10.0-py3-none-any.whl
Size 170.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e11972c06c1544f5f3d23322211f9cfbdd0722232858d1919543dca7f177f06
BLAKE2b-256 checksum
How to use checksums
2e9a0379748903b678263833862761155739f6282393a81b287fc17e27fbdd38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

2.10.0 This release

2 release files

2.9.1

2 release files

2.9.0

2 release files

2.8.2

2 release files

2.8.1

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.3

1 release file

2.6.1

1 release file

2.6.0

1 release file

2.5.1

1 release file

2.5.0

1 release file

2.4.5

1 release file

2.4.4

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