PyStructs
A convenient
dataclass
version of python's
struct library
for packing and unpacking objects to their equivalent byte representations.
Installation
pip install pystructs3
Examples
Simple Example
from pystructs import *
class Bar(Struct):
z: U32
class Foo(Struct):
x: U8
y: U16
bar: Bar
# pack and unpack structs with their builtin methods
foo1 = Foo(230, 65000, Bar(2147483648))
packed = foo1.pack()
foo2 = Foo.unpack(packed)
print('original', foo1)
print('packed', packed)
print('unpacked', foo2)
# equivalent functional version for additional utility
packed = pack((U8, U16, U32), 230, 65000, 2147483648)
unpacked = unpack((U8, U16, U32), packed)
print('packed', packed)
print('unpacked', unpacked)
More Complex Example
from typing import List
from ipaddress import IPv4Address, IPv6Address
from typing_extensions import Annotated
from pystructs import *
class Bar(Struct):
mac: MACAddr
ip4: IPv4
ip6: IPv6
domain: Domain
class Foo(Struct):
signed: I8 # I8/I16/I32/I48/I64/I128
unsigned: U32 # U8/U16/U32/U48/U64/U128
custom: Annotated[int, IntField(1, 'little', False)] # custom integer
b_hinted: Annotated[bytes, HintedBytes(U16)] # dynamic with size hint
b_static: bytes = field(field=StaticBytes(16)) # static sized bytes
l_hinted: Annotated[List[int], HintedList(U8, U8)] # dynamic with size hint
l_static: List[int] = field(field=StaticList(2, U8)) # static sized list
# b_greedy: bytes = field(field=GreedyBytes()) # consumes rest of message
# l_list: List[int] = field(field=GreedyList(U8)) # consumes rest of message
bar = Bar('01:02:03:04:05:06', IPv4Address('1.2.3.4'), IPv6Address('::1'), b'example.com')
foo = Foo(1, 2, 3, b'hinted', b'static', [4, 5, 6], [7, 8])
# use a context object when packing/unpacking multiple objects in sequence
ctx = Context()
packed = bar.pack(ctx) + foo.pack(ctx)
ctx.reset() # context uses index to track place in buffer (must be reset)
bar2 = bar.unpack(packed, ctx)
foo2 = foo.unpack(packed, ctx)
print('foo1', foo)
print('bar1', bar)
print('packed', packed)
print('foo2', foo2)
print('bar2', bar2)
Metadata
Release files for pystructs3 0.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pystructs3-0.0.8.tar.gz | 12.4 kB | Without provenance | Details for pystructs3-0.0.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pystructs3-0.0.8-py3-none-any.whl | Python 3 | none | any | Details for pystructs3-0.0.8-py3-none-any.whl |
Total release size: 26.7 kB
Release files / pystructs3-0.0.8.tar.gz
| Download URL | pystructs3-0.0.8.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b0bda4f6258c118441dab2f5d87af076bd87b087bac3d170e7f66e76917f928
|
|
BLAKE2b-256 checksum How to use checksums |
b9fb90102ce0d1de7ee86c299a1723bcf0f7628ba83434d16f89e39bdd77b4da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.8.20
|
Release files / pystructs3-0.0.8-py3-none-any.whl
| Download URL | pystructs3-0.0.8-py3-none-any.whl |
|---|---|
| Size | 14.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cc2fddff4bbe3656e452c2f2c9e3549139618084b828e8f4eeba1c81fef1c078
|
|
BLAKE2b-256 checksum How to use checksums |
f39afdefc2bff9c7af6ffb9d27d250c8475781003ceaca1d71a22ab277230e3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.8.20
|
Security for pystructs3 0.0.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.