A declarative parser and builder for string-based protocols
Project description
Overview
StrConstruct is declarative parser and builder for string-based data/protocols with a syntax similar to the Construct python package. It provides a powerful syntax for defining protocols and it takes care of parsing and building strings based on the defined protocols. This makes code more readable, concise and maintainable.
Here is an example.
>>> from strconstruct import StrInt, StrFloat, StrConst, StrStruct, StrDefault, StrSwitch
>>> protocol = StrStruct(
... StrConst(">"),
... "register" / StrDefault(StrInt("d"), 17),
... StrConst(","),
... "value1" / StrSwitch(
... lambda this: this["register"],
... {
... 1: StrFloat("0.1f"),
... 2: StrInt("d"),
... 3: StrInt("02X"),
... },
... default=StrInt("03X"),
... ),
... StrConst("\r"),
... )
>>> protocol.build(
... {
... "register": 3,
... "value1": 16,
... }
... )
'>3,10\r'
>>> protocol.build({"register": 1, "value1": 16})
'>1,16.0\r'
>>> protocol.parse(">4,020\r")
{'register': 4, 'value1': 32}
Documentation
Documentation can be found here
Running Unit Tests
Unit tests can be run using pytest
. Simply, navigate to the ./test/unit
folder and
run python -m pytest -vvvs
. Note that the package needs to be installed before running
the tests (cd StrConstruct && python -m pip install -e .
).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for strconstruct-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38d87d73c299551322c1f15e73f6f2d0b6224b60bed28b5ee17ab60f26d06969 |
|
MD5 | bcb55c5d80af28d510ce95ace99643eb |
|
BLAKE2b-256 | 5f2be2ab7f7dd2c9b14856aac4d2bd5b21226a48bf2d6ba4bdbf6adf34d1e133 |