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 Endianes

profile = UserProfile(
    user_type=UserType.ADMIN,
    joined_at=Date(year=2024, month=6, day=25),
    name="admin"
)

binary: bytes = profile.dump(endianes=Endianes.LITTLE)
parsed: UserProfile = UserProfile.parse(binary, endianes=Endianes.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.1.4.tar.gz (17.5 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.1.4-py3-none-any.whl (33.3 kB view details)

Uploaded Python 3

File details

Details for the file bytex-0.1.4.tar.gz.

File metadata

  • Download URL: bytex-0.1.4.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bytex-0.1.4.tar.gz
Algorithm Hash digest
SHA256 78e8e22d4943f6d696b01840915bf60504f13ba1a9fc732fce5a7a0316a59933
MD5 d05aeecb645c3b7936bc9a11b8a93a0b
BLAKE2b-256 b8bd43bde03abfd9071459ac7fae5c36fd7d37c776e0f5c2e053b1f9e30b0567

See more details on using hashes here.

Provenance

The following attestation bundles were made for bytex-0.1.4.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.1.4-py3-none-any.whl.

File metadata

  • Download URL: bytex-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 33.3 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.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e7dc27feff3c43229c3f0b14bc8a37ce1e3817dec656f17459efc666f306089a
MD5 7bf26e16e8c34aaf85e0f0cc14a38a3e
BLAKE2b-256 6d8e0d5202194c27695c45c2e5c942e3ce449873467a36db30b1076698e0bfb6

See more details on using hashes here.

Provenance

The following attestation bundles were made for bytex-0.1.4-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