Skip to main content

Protocol Buffers using Python type annotations

Project description

pure-protobuf

GitHub Workflow Status Code coverage PyPI - Downloads PyPI – Version PyPI – Python License

Wow! Such annotated! Very buffers!

⚠️ Note
This README describes the upcoming major version update. For 2.x please refer to: https://github.com/eigenein/protobuf/tree/2.2.3

Documentation

Documentation

Quick examples

.proto definition

syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

With dataclasses

from dataclasses import dataclass
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.message import BaseMessage
from typing_extensions import Annotated


@dataclass
class SearchRequest(BaseMessage):
    query: Annotated[str, Field(1)] = ""
    page_number: Annotated[uint, Field(2)] = 0
    result_per_page: Annotated[uint, Field(3)] = 0


request = SearchRequest(query="hello", page_number=uint(1), result_per_page=uint(10))
buffer = bytes(request)
assert buffer == b"\x0A\x05hello\x10\x01\x18\x0A"
assert SearchRequest.read_from(BytesIO(buffer)) == request

With pydantic

from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.message import BaseMessage
from pydantic import BaseModel
from typing_extensions import Annotated


class SearchRequest(BaseMessage, BaseModel):
    query: Annotated[str, Field(1)] = ""
    page_number: Annotated[uint, Field(2)] = 0
    result_per_page: Annotated[uint, Field(3)] = 0


request = SearchRequest(query="hello", page_number=uint(1), result_per_page=uint(10))
buffer = bytes(request)
assert buffer == b"\x0A\x05hello\x10\x01\x18\x0A"
assert SearchRequest.read_from(BytesIO(buffer)) == request

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

pure_protobuf-3.0.0a1.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

pure_protobuf-3.0.0a1-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file pure_protobuf-3.0.0a1.tar.gz.

File metadata

  • Download URL: pure_protobuf-3.0.0a1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.6 Linux/5.15.0-1033-azure

File hashes

Hashes for pure_protobuf-3.0.0a1.tar.gz
Algorithm Hash digest
SHA256 83082287aa66494a2880c1e3b46f090bfebe3db10f91ca6a4eaa43fd84e84c23
MD5 108be404b51212c7fbe5ac4157fe7a8a
BLAKE2b-256 ef914f1176f17eb3f9831f68d9aed27a78bf1806206f88e39b40b2db81a4c48e

See more details on using hashes here.

File details

Details for the file pure_protobuf-3.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: pure_protobuf-3.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.6 Linux/5.15.0-1033-azure

File hashes

Hashes for pure_protobuf-3.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 74d060d7439c2d31aaf0a9ce4a5c9c10b8e7503d90f518308a485b521eeae404
MD5 ffdc23ffa2db451fbb996a947739057c
BLAKE2b-256 7fbd59f03060267b78f878736ba9767d03d9ea5406eda037bf318dc26be2922e

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