Bytex
Like Pydantic but for binary formats
Install / Use
/learn @ArielAlon24/BytexREADME
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)
Related Skills
node-connect
351.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
