Skip to main content

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 StructureEnum is just a subclass of Python's built-in Enum, 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 a Terminator length 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 called CStr located at structure.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

bytex-0.2.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bytex-0.2.0-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

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

Hashes for bytex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cbf8b2d297f8f427ee22ccd6659437b0adfd10a0f945133769c898d0908b1f9b
MD5 837866bc9024c54d3ec75b0b2488c295
BLAKE2b-256 4b94f7c82cc9047473914745d742e771affebadc6ebeae9f914465ffd6942559

See more details on using hashes here.

Provenance

The following attestation bundles were made for bytex-0.2.0.tar.gz:

Publisher: publish.yml on ArielAlon24/bytex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for bytex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b85bab7a4e69eabf1a5c491881fae334079b623a8678fca4fbec53dee95f2c5f
MD5 b5f0ec4df89ead0d8ca1e8e59b97d089
BLAKE2b-256 8031060f4689770e5cdcc87eea7642b74b9cf0e69052050bd5c7394124003b4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for bytex-0.2.0-py3-none-any.whl:

Publisher: publish.yml on ArielAlon24/bytex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page