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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strconstruct-0.0.1.tar.gz.
File metadata
- Download URL: strconstruct-0.0.1.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ca6e060f90a6d02447e05e7b48c9b7b52f302973b636070e8199d3c5e069a01
|
|
| MD5 |
ab97453f776a7d3547c1399b912273a1
|
|
| BLAKE2b-256 |
c4179d723675b4b30d015b234bfc67e1bec34591d070ca851d921b05ef18aca4
|
File details
Details for the file strconstruct-0.0.1-py3-none-any.whl.
File metadata
- Download URL: strconstruct-0.0.1-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38d87d73c299551322c1f15e73f6f2d0b6224b60bed28b5ee17ab60f26d06969
|
|
| MD5 |
bcb55c5d80af28d510ce95ace99643eb
|
|
| BLAKE2b-256 |
5f2be2ab7f7dd2c9b14856aac4d2bd5b21226a48bf2d6ba4bdbf6adf34d1e133
|