Skip to main content

A declarative parser and builder for string-based protocols

Project description

Documentation Status License PyPi Version

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


Download files

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

Source Distribution

strconstruct-0.0.1.tar.gz (16.4 kB view hashes)

Uploaded Source

Built Distribution

strconstruct-0.0.1-py3-none-any.whl (23.1 kB view hashes)

Uploaded Python 3

Supported by

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