https://github.com/guangchen2333/structovo
(Python 3.8+) A Python library offering a more concise syntax for data packing.
https://github.com/guangchen2333/structovo
network packet python-struct python3 python3-library serialization struct
Last synced: 20 days ago
JSON representation
(Python 3.8+) A Python library offering a more concise syntax for data packing.
- Host: GitHub
- URL: https://github.com/guangchen2333/structovo
- Owner: GuangChen2333
- License: mit
- Created: 2025-02-07T06:18:30.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2025-02-07T11:47:27.000Z (3 months ago)
- Last Synced: 2025-03-12T10:49:30.062Z (about 1 month ago)
- Topics: network, packet, python-struct, python3, python3-library, serialization, struct
- Language: Python
- Homepage: https://pypi.org/project/structovo/
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StructOvO




A Python library offering a more concise syntax for data packing.
## Installation
- From `pypi`
```shell
pip install structovo
```## Usage
```python
from structovo import *class PackA(Packet):
a: PADDING
b: BYTE = b'A'
c: CHAR = -1
d: UCHAR = 1
e: BOOL = False
f: SHORT = -2
g: USHORT = 2
h: INT = -3
i: UINT = 3
j: LONG = -4
k: ULONG = 4
l: LONGLONG = -5
m: ULONGLONG = 5
n: SIZET = 12
o: SSIZET = 34
p: BINARY16 = 3.14
q: FLOAT = 3.14
r: DOUBLE = 3.14
s: FixedString = (b"hello", 10)
t: LengthPrefixedString = b"world"
u: UnsignedPointer = 0x0d000721
v: bytes = b'raw_bytes'r = PackA.build(endianness=Endianness.NATIVE)
hex_list = [format(byte, '02x') for byte in r]
hex_str = ' '.join(hex_list)
print(hex_str)
```And you will get:
```text
00 41 ff 01 00 fe ff 02 00 fd ff ff ff 03 00 00 00 fc ff ff ff 04 00 00 00 fb ff ff ff ff ff ff ff 05 00 00 00 00 00 00 00 0c 00 00 00 00 00 00 00 22 00 00 00 00 00 00 00 48 42 c3 f5 48 40 1f 85 eb 51 b8 1e 09 40 68 65 6c 6c 6f 00 00 00 00 00 05 77 6f 72 6c 64 21 07 00 0d 00 00 00 00 72 61 77 5f 62 79 74 65 73
```## Advance
### Supported Endianness
- `Endianness.BIG`: Big-endian
- `Endianness.LITTLE`: Little-endian
- `Endianness.NETWORK`: Big-endian
- `Endianness.NATIVE`: Depend on your device (Default)### Custom Data Types
You can define your own data types by simply inheriting the `BaseType` class:
```python
from typing import Tuple, Type
from structovo import BaseType, Endiannessclass MyDataType(BaseType):
def check_validation(self) -> Tuple[bool, Type]:
# Implement the validation of whether the self.value is valid.
return ...def encode(self, endianness: Endianness) -> bytes:
# Implement the operation of converting self.value to bytes.
return ...
```Like this:
```python
from typing import Tuple, Type
from structovo import BaseType, Endiannessclass BYTE(BaseType):
def check_validation(self) -> Tuple[bool, Type]:
result = isinstance(self.value, bytes)
return result, bytesdef encode(self, endianness: Endianness) -> bytes:
return self._pack('c', endianness)
```