Skip to main content

An implementation of C-like packed structures in Python

Project description

py-packed-struct

An implementation of C-like packed structures in Python based on the bitstruct package

py-packed-struct allows to define C-like structures in an elegant way and to convert them into bytes objects without having to specify the format as required by struct:

# with struct
>>> from struct import *
>>> one, two, three = 1, 2, 3
>>> pack(">bhl", one, two, three)
b'\x01\x00\x02\x00\x00\x00\x03'

# with py-packed-struct
>>> from packed_struct import *
>>> s = Struct({"one": c_signed_int(8), "two": c_signed_int(16), "three": c_signed_int(32) })
>>> s.set_data(one = 1, two = 2, three = 3)
>>> serialized = s.pack(byte_endianness="big")
>>> print(serialized)
b'\x01\x00\x02\x00\x00\x00\x03'
>>>
>>> s.unpack(serialized)
>>> {'one': 1, 'two': 2, 'three': 3}

Who needs to remember struct format strings? :)

In addition, py-packed-struct allows to work with bit-fields and nested structures (see examples).

Installation

pip install py-packed-struct

Supported features

  • C-like struct
  • bit-fields handling
  • byte endianess
  • (TODO) bit endianness

Example

This example can be found in example/mqtt. publisher.py publishes a message on a MQTT topic and subscriber.c is subscribed to that topic. The publisher publishes the following structure:

person = Struct(
    {
        "name": c_char(10*8),
        "age": c_unsigned_int(8),
        "weight": c_float(32),
        "dresses": Struct(
            {
                "tshirt": c_char(10*8),
                "shorts": c_char(10*8),
                "shoes": Struct(
                    {
                        "number": c_unsigned_int(8),
                        "brand": c_char(10*8)
                    }
                )
            }
        )
    }
)
# set data values
person.set_data(name="Luca", age=29, weight=76.9)
person.dresses.set_data(tshirt="foo", shorts="boo")
person.dresses.shoes.set_data(number=42, brand="bar")

The subscriber copies the incoming buffer in the following struct:

typedef struct __attribute__((packed)) {
	uint8_t number;
	char brand[10];
} shoes_t;

typedef struct __attribute__((packed)) {
	char tshirt[10];
	char shorts[10];
	shoes_t shoes;
} clothes_t;

typedef struct __attribute__((packed)) {
	char name[10];
	uint8_t age;
	float weight;
	clothes_t clothes;
} person;

The result is the following:

example mqtt

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.

py_packed_struct-0.6-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file py_packed_struct-0.6-py3-none-any.whl.

File metadata

  • Download URL: py_packed_struct-0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for py_packed_struct-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f0be017ee2c26b651075b768896d928d1f6ce628c5e4b5ed5d50b8e98729b74e
MD5 e88f65753170b538da933150eebe4ca3
BLAKE2b-256 0385ef6b50c855189d2db5ca3aa42b171737367448a42d117076fc65a48c76f6

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