Skip to main content

PyByntic extends Pydantic with binary-typed fields and automatic byte-level serialization. Define models using familiar Pydantic syntax and turn them into compact binary payloads with full control over layout and numeric precision.

Project description

PyByntic

PyByntic extends Pydantic with binary-typed fields and automatic byte-level serialization. Define models using familiar Pydantic syntax and turn them into compact binary payloads with full control over layout and numeric precision.

Features

  • Binary Serialization: Convert Pydantic models to compact binary format
  • Type Safety: Full type annotations with custom binary types
  • Nested Models: Support for nested models and lists
  • Custom Encoders: Support for compression and custom encoding
  • Size Efficiency: Significantly smaller than JSON serialization

Installation

poetry install

Development

# Install dev dependencies
poetry install --with dev

# Run tests
poetry run pytest

# Run tests with coverage
poetry run pytest --cov=byto

# Format code
poetry run black .

# Run linting
poetry run poe lint

Usage

from pybyntic import AnnotatedBaseModel
from pybyntic.types import UInt32, String, Bool, Date
from typing import Annotated
import datetime


class User(AnnotatedBaseModel):
	user_id: Annotated[int, UInt32]
	username: Annotated[str, String]
	is_active: Annotated[bool, Bool]
	join_date: Annotated[datetime.date, Date]


# Create and serialize
user = User(
	user_id=123,
	username='admin',
	is_active=True,
	join_date=datetime.date.today()
)

# Serialize to bytes
serialized = user.serialize()

# Deserialize from bytes
deserialized = User.deserialize(serialized)

Examples

See the examples/ directory for more comprehensive examples including:

  • Basic serialization
  • List handling
  • Nested models
  • Compression
  • Benchmarking

Types

Type Name Format / Encoding Python Type Returned Notes
Skip — (no-op) None Skips field
Bool b (1 byte) bool Writes 1 or 0
Int8 b int Signed 8-bit integer
Int16 h int Signed 16-bit integer
Int32 i int Signed 32-bit integer
Int64 q int Signed 64-bit integer
UInt8 B int Unsigned 8-bit integer
UInt16 H int Unsigned 16-bit integer
UInt32 I int Unsigned 32-bit integer
UInt64 Q int Unsigned 64-bit integer
UInt128 Q (hi/lo) int 128-bit unsigned integer, stored as 2×64-bit
Float32 f float IEEE754 single precision
Float64 d float IEEE754 double precision
String varint + bytes str UTF-8 string, with varint length prefix
StringJson varint + bytes dict UTF-8 JSON, decoded via json.loads
DateTime32 I datetime (UTC) Seconds since epoch
DateTime64[p] Q datetime (UTC) Timestamp in 10^-p seconds precision (default p=3)
Date H date Days since 1970-01-01

Implementing Custom Types

Example of a type implementation:

from pybyntic.buffer import Buffer


class Bool:
	@classmethod
	def read(cls, buf: Buffer):
		return bool(buf.read_formated("b"))

	@classmethod
	def write(cls, buf: Buffer, value):
		buf.write_formated("b", 1 if value else 0)

Type classes must implement read and write class methods to handle serialization and deserialization. Read methods receive a Buffer instance for byte operations, and write methods receive a Buffer and the value to serialize.

Testing

The project includes comprehensive tests covering:

  • Basic serialization/deserialization
  • List handling
  • Nested models and lists
  • Dictionary serialization with JSON
  • Compression functionality
  • Individual type testing

Run tests with:

poetry run pytest

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

pybyntic-0.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

pybyntic-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pybyntic-0.1.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.15 Darwin/23.5.0

File hashes

Hashes for pybyntic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 046aca23328ebd13c8f235510205efcfd56dec24849f4a07549ce440f5800ccf
MD5 6a592a98598bdc5a9179bc30e3901584
BLAKE2b-256 e433d44f7277534ab9acc44d28d44aebf23cb19bc5da381219b3db4758ed76ec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pybyntic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.15 Darwin/23.5.0

File hashes

Hashes for pybyntic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb1d5e21fca5a1becb7e2fa8529acab00c809f502534b2c5d53e6fc296755b34
MD5 894165e258e39a0bf689d232f993037a
BLAKE2b-256 9a876d2370dbc2cb40fc06a3c30f7cdfca78f6bfc904b183545b2286ff2e5718

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