Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bessman/datastructclass
https://github.com/bessman/datastructclass
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/bessman/datastructclass
- Owner: bessman
- License: mit
- Created: 2024-12-22T10:06:24.000Z (18 days ago)
- Default Branch: main
- Last Pushed: 2024-12-22T10:11:29.000Z (18 days ago)
- Last Synced: 2024-12-22T10:15:14.194Z (18 days ago)
- Language: Python
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# datastructclass
A `DataStructClass` is a `dataclass` with struct-like semantics. It can be
serialized/deserialized to/from `bytes`.A subclass of `DataStructClass` is a `dataclass`, with the following additional
features:```python
.format
.size
.unpack
.pack```
Each member of a `DataStructClass` must provide struct metadata via `typing.Annotated`.
## Example
```python
from struct import Struct
from typing import Annotated
from datastructclass import DataStructClassclass MyDSC(DataStructClass):
# Unsigned long:
member_a: Annotated[int, Struct("=L")]
# Double:
member_b: Annotated[float, Struct("=d")]
# Array of signed long with length 4.
member_c: Annotated[list[int], Struct("=4I")]
# Array of char, aka. a string, with length 8:
member_d: Annotated[bytes, Struct("=8s")]data = MyDSC(4000, 3.14, [-2, -1, 0, 1], b"charlie ")
# 'send_over_wire' could a network socket, a serial port, etc.
send_over_wire(data.pack())
```## Limitations
`DataStructClass` cannot contain variable-length members.