py-packed-struct


Namepy-packed-struct JSON
Version 0.5 PyPI version JSON
download
home_pagehttps://github.com/lu-maca/py-packed-struct
SummaryAn implementation of C-like packed structures in Python
upload_time2023-10-27 08:14:26
maintainerLuca Macavero
docs_urlNone
authorLuca Macavero
requires_python>=3.5
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # py-packed-struct
An implementation of C-like packed structures in Python based on the [`bitstruct`](https://bitstruct.readthedocs.io/en/latest/index.html) 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`:
```python
# 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](https://github.com/lu-maca/py-packed-struct/tree/main/examples)).

Installation
----
```bash
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`](https://github.com/lu-maca/py-packed-struct/tree/main/examples/mqtt). `publisher.py` publishes a message on a MQTT topic and `subscriber.c` is subscribed to that topic. The publisher publishes the following structure:
```python
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`:
```c
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](https://github.com/lu-maca/py-packed-struct/assets/65252677/997cadce-d79d-4117-b693-dc025957ebf9)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/lu-maca/py-packed-struct",
    "name": "py-packed-struct",
    "maintainer": "Luca Macavero",
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": "luca.macavero@gmail.com",
    "keywords": "",
    "author": "Luca Macavero",
    "author_email": "luca.macavero@gmail.com",
    "download_url": "",
    "platform": null,
    "description": "# py-packed-struct\nAn implementation of C-like packed structures in Python based on the [`bitstruct`](https://bitstruct.readthedocs.io/en/latest/index.html) package\n\n**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`:\n```python\n# with struct\n>>> from struct import *\n>>> one, two, three = 1, 2, 3\n>>> pack(\">bhl\", one, two, three)\nb'\\x01\\x00\\x02\\x00\\x00\\x00\\x03'\n\n# with py-packed-struct\n>>> from packed_struct import *\n>>> s = Struct({\"one\": c_signed_int(8), \"two\": c_signed_int(16), \"three\": c_signed_int(32) })\n>>> s.set_data(one = 1, two = 2, three = 3)\n>>> serialized = s.pack(byte_endianness=\"big\")\n>>> print(serialized)\nb'\\x01\\x00\\x02\\x00\\x00\\x00\\x03'\n>>>\n>>> s.unpack(serialized)\n>>> {'one': 1, 'two': 2, 'three': 3}\n```\nWho needs to remember struct format strings? :)\n\nIn addition, **py-packed-struct** allows to work with bit-fields and nested structures (see [examples](https://github.com/lu-maca/py-packed-struct/tree/main/examples)).\n\nInstallation\n----\n```bash\npip install py-packed-struct\n```\n\nSupported features\n----\n- C-like struct\n- bit-fields handling\n- byte endianess\n- (TODO) bit endianness\n\n\nExample\n----\nThis example can be found in [`example/mqtt`](https://github.com/lu-maca/py-packed-struct/tree/main/examples/mqtt). `publisher.py` publishes a message on a MQTT topic and `subscriber.c` is subscribed to that topic. The publisher publishes the following structure:\n```python\nperson = Struct(\n    {\n        \"name\": c_char(10*8),\n        \"age\": c_unsigned_int(8),\n        \"weight\": c_float(32),\n        \"dresses\": Struct(\n            {\n                \"tshirt\": c_char(10*8),\n                \"shorts\": c_char(10*8),\n                \"shoes\": Struct(\n                    {\n                        \"number\": c_unsigned_int(8),\n                        \"brand\": c_char(10*8)\n                    }\n                )\n            }\n        )\n    }\n)\n# set data values\nperson.set_data(name=\"Luca\", age=29, weight=76.9)\nperson.dresses.set_data(tshirt=\"foo\", shorts=\"boo\")\nperson.dresses.shoes.set_data(number=42, brand=\"bar\")\n```\nThe subscriber copies the incoming buffer in the following `struct`:\n```c\ntypedef struct __attribute__((packed)) {\n\tuint8_t number;\n\tchar brand[10];\n} shoes_t;\n\ntypedef struct __attribute__((packed)) {\n\tchar tshirt[10];\n\tchar shorts[10];\n\tshoes_t shoes;\n} clothes_t;\n\ntypedef struct __attribute__((packed)) {\n\tchar name[10];\n\tuint8_t age;\n\tfloat weight;\n\tclothes_t clothes;\n} person;\n```\n\nThe result is the following:\n\n![example mqtt](https://github.com/lu-maca/py-packed-struct/assets/65252677/997cadce-d79d-4117-b693-dc025957ebf9)\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "An implementation of C-like packed structures in Python",
    "version": "0.5",
    "project_urls": {
        "Homepage": "https://github.com/lu-maca/py-packed-struct"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "960e631274ddf3e0b829fc8e7946b9ba8b14009beba0f5d80e6cc0be1c4c96d3",
                "md5": "5c031ebd649e50ca1e2e077eb7a012e8",
                "sha256": "4e386f4ea5ad2b9b0fb00345ff1a3c563cc01537d49b5d69b519b8acac9eab37"
            },
            "downloads": -1,
            "filename": "py_packed_struct-0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5c031ebd649e50ca1e2e077eb7a012e8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 7387,
            "upload_time": "2023-10-27T08:14:26",
            "upload_time_iso_8601": "2023-10-27T08:14:26.306234Z",
            "url": "https://files.pythonhosted.org/packages/96/0e/631274ddf3e0b829fc8e7946b9ba8b14009beba0f5d80e6cc0be1c4c96d3/py_packed_struct-0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-27 08:14:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lu-maca",
    "github_project": "py-packed-struct",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "py-packed-struct"
}
        
Elapsed time: 0.18118s