Skip to main content

Pydantic-like binary validation

Project description

bytex

Like Pydantic, but for binary formats.

Installation

(Still not available on PyPI)

$ 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.2.tar.gz (16.3 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.2-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for bytex-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c9689ca61476db5a70a515c4a0bbd2e98738528cdb35803da7bb0410fa0c1047
MD5 b5964d0ce7777e17ac68c81e25401e3c
BLAKE2b-256 a2368dfc7c088031730b50f0f249b6508fa59e9196d1167e39a92bfb5609e8cf

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: bytex-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bytex-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e3f8014f5d89a1eeed659ef09ce56b1813180694ba1002561e3b20b9224199c7
MD5 744628712982ad3cde1389a818a1a1db
BLAKE2b-256 9f93f8eb93e43aad042a1f26c88bc96c39c74fc8191904e5ac4def96ef5af8e7

See more details on using hashes here.

Provenance

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