Pydantic-like binary validation
Project description
bytex
Like Pydantic, but for binary formats.
Installation
$ pip install bytex
Example
Let’s say you want to represent a user profile in your application using a compact binary format.
Start by defining the user type as a StructureEnum:
from bytex import StructureEnum
from bytex.types import U8
from enum import auto
class UserType(StructureEnum(U8)):
"""
An enum representing a UserType in a single byte.
"""
ADMIN = auto()
MEMBER = auto()
GUEST = auto()
A
StructureEnumis just a subclass of Python's built-inEnum, so you can use it as you normally would.
Then define a structure for a date:
from bytex import Structure
from bytex.types import U16, U8
class Date(Structure):
year: U16
month: U8
day: U8
Now bring it all together into a UserProfile structure:
from bytex import Structure
from bytex.length_encodings import Terminator
from typing import Annotated
class UserProfile(Structure):
user_type: UserType
joined_at: Date
name: Annotated[str, Terminator("\0")]
The
Annotated[str, Terminator("\0")]represents a string with aTerminatorlength encoding, meaning the string is serialized with a"\0"at the end, and deserialized until a"\0"is encountered (There is a pre-made type for this exact scenario calledCStrlocated atstructure.types, which is a null-terminated string).
And you're done!
You can now serialize and parse binary data with ease:
from bytex import Endianness
profile = UserProfile(
user_type=UserType.ADMIN,
joined_at=Date(year=2024, month=6, day=25),
name="admin"
)
binary: bytes = profile.dump(endianness=Endianness.LITTLE)
parsed: UserProfile = UserProfile.parse(binary, endianness=Endianness.LITTLE)
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
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 bytex-0.2.0.tar.gz.
File metadata
- Download URL: bytex-0.2.0.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbf8b2d297f8f427ee22ccd6659437b0adfd10a0f945133769c898d0908b1f9b
|
|
| MD5 |
837866bc9024c54d3ec75b0b2488c295
|
|
| BLAKE2b-256 |
4b94f7c82cc9047473914745d742e771affebadc6ebeae9f914465ffd6942559
|
Provenance
The following attestation bundles were made for bytex-0.2.0.tar.gz:
Publisher:
publish.yml on ArielAlon24/bytex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bytex-0.2.0.tar.gz -
Subject digest:
cbf8b2d297f8f427ee22ccd6659437b0adfd10a0f945133769c898d0908b1f9b - Sigstore transparency entry: 475159846
- Sigstore integration time:
-
Permalink:
ArielAlon24/bytex@991cba77feb6a11ce70e0f33e67e6fd55b851e40 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/ArielAlon24
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@991cba77feb6a11ce70e0f33e67e6fd55b851e40 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bytex-0.2.0-py3-none-any.whl.
File metadata
- Download URL: bytex-0.2.0-py3-none-any.whl
- Upload date:
- Size: 34.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b85bab7a4e69eabf1a5c491881fae334079b623a8678fca4fbec53dee95f2c5f
|
|
| MD5 |
b5f0ec4df89ead0d8ca1e8e59b97d089
|
|
| BLAKE2b-256 |
8031060f4689770e5cdcc87eea7642b74b9cf0e69052050bd5c7394124003b4c
|
Provenance
The following attestation bundles were made for bytex-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on ArielAlon24/bytex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bytex-0.2.0-py3-none-any.whl -
Subject digest:
b85bab7a4e69eabf1a5c491881fae334079b623a8678fca4fbec53dee95f2c5f - Sigstore transparency entry: 475159880
- Sigstore integration time:
-
Permalink:
ArielAlon24/bytex@991cba77feb6a11ce70e0f33e67e6fd55b851e40 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/ArielAlon24
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@991cba77feb6a11ce70e0f33e67e6fd55b851e40 -
Trigger Event:
push
-
Statement type: