Skip to main content

Library to pack and unpack structurized binary data.

Project description

Caterpillar - 🐛

python Build and Deploy Docs Run Tests GitHub issues GitHub License

[!WARNING] This project is still in beta/testing phase. Expect bugs, naming changes and errors while using this library. C API Reference is WIP, C extensions are supported since v2.1.0.

[!NOTE] Python 3.14 breaks with statements in class definitions since __annotations__ are added at the end of a class definition. Therefore, Digest and conditional statements ARE NOT SUPPORTED in using the with syntax Python 3.14+. As of version 2.4.5 the Digest class has a counterpart (DigestField), which can be used to manually specify a digest without the need of a ẁith statement.

Caterpillar is a Python 3.12+ library to pack and unpack structurized binary data. 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 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.
  • You can even extend Caterpillar and write your parsing logic in C or C++!!

Give me some code!

from caterpillar.py import *

@struct(order=LittleEndian)
class Format:
    magic: b"ITS MAGIC"       # Supports string and byte constants directly
    a: uint8                  # Primitive data types
    b: int32
    length: uint8             # String fields with computed lengths
    name: String(this.length) #  -> you can also use Prefixed(uint8)

    # wraps all following fields and creates a new attr
    # only for Python <= 3.13
    with Md5(name="hash", verify=True):
        # Sequences with prefixed, computed lengths
        names: CString[uint8::]


# Instantiation (keyword-only arguments, magic is auto-inferred):
obj = Format(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
blob = pack(obj, Format)
# results in: b'ITS MAGIC\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00\xf55...

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

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"

C-extension installation

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

caterpillar_py-2.4.5-py3-none-any.whl (92.1 kB view details)

Uploaded Python 3

File details

Details for the file caterpillar_py-2.4.5-py3-none-any.whl.

File metadata

  • Download URL: caterpillar_py-2.4.5-py3-none-any.whl
  • Upload date:
  • Size: 92.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for caterpillar_py-2.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f59872d51b323f4a9fd0c16150bcb0dba29166abe092b70927ef450bcf503b44
MD5 21c20cc6b0c53555313e45362e1a8b4e
BLAKE2b-256 2c5a634a831aa453b8f2e3c7e7bfe38c760c9e8d6dc531ae91f797055a6eece5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page