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.3.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.3-py3-none-any.whl (33.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bytex-0.1.3.tar.gz
  • Upload date:
  • Size: 17.5 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.3.tar.gz
Algorithm Hash digest
SHA256 38706a03e1e190c346b77c2c056caf42fb5757fdac5c5ffc3d9a21f9e138d371
MD5 0c84c54f440535342e0ac3cc919971b7
BLAKE2b-256 b99afa914ee1164f37761eb5050c31fb6df2e41ad66514ede969ff848218eb01

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: bytex-0.1.3-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.12.9

File hashes

Hashes for bytex-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 73efa6a6d3ae7bd92ba217216de0cffdb7f0b287b2e635d79c363f0643a0f12b
MD5 d5fa0726f9fe3cfdfaf49e0c5f8d3d67
BLAKE2b-256 9c7530b50eac613a247f02b5f446d7e33511f4f25dbb2b5646bf7d434b96c7d8

See more details on using hashes here.

Provenance

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