Skip to main content

Pydantic-like binary validation

Project description

PyStruct

Like Pydantic, but for binary formats.

Installation

(Still not available on PyPI)

$ pip install pystruct

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: bytex-0.1.0.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for bytex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2937dd1841e146f1fe36f94b88ea90cac6b8e456945e9045c3da66e00b91883f
MD5 edaa21a1034f02c93b311f4aa1755ef1
BLAKE2b-256 1b30c6e0eef80c1278b65959316abc37c3ffdac5cfbd3cb020002829e84ededf

See more details on using hashes here.

File details

Details for the file bytex-0.1.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for bytex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9df1606bd5dc31ac2f4de70eb048724d15126ebcab30e44c3dbefc5a788028f7
MD5 05b439cdacfe4a5f0183cf8553efa061
BLAKE2b-256 b4ac7a496b5f2b90f7092e0f59a1f3e9a40c85ab9bf271bf64659524ebfeaea4

See more details on using hashes here.

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