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!

Documentation

Documentation

Quick examples

.proto definition

It's not needed for pure-protobuf, but for the sake of an example, let's consider the following definition:

syntax = "proto3";

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

And here's the same via pure-protobuf:

With dataclasses

from dataclasses import dataclass
from io import BytesIO

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


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


request = SearchRequest(query="hello", page_number=1, result_per_page=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
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[int, Field(2)] = 0
    result_per_page: Annotated[int, Field(3)] = 0


request = SearchRequest(query="hello", page_number=1, result_per_page=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.1.3.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

pure_protobuf-3.1.3-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

Details for the file pure_protobuf-3.1.3.tar.gz.

File metadata

  • Download URL: pure_protobuf-3.1.3.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.3 Linux/6.8.0-1015-azure

File hashes

Hashes for pure_protobuf-3.1.3.tar.gz
Algorithm Hash digest
SHA256 ae439e1920e9c0e6bf65bf892948085e021cb4a7dc3f2ec399b0ee735422f30d
MD5 af5cd709ae86544ffa932eb40c77d76c
BLAKE2b-256 76951ccc83c6e64d78da96899611043799b221918f3c30e3086dabc78dffc58a

See more details on using hashes here.

File details

Details for the file pure_protobuf-3.1.3-py3-none-any.whl.

File metadata

  • Download URL: pure_protobuf-3.1.3-py3-none-any.whl
  • Upload date:
  • Size: 28.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.3 Linux/6.8.0-1015-azure

File hashes

Hashes for pure_protobuf-3.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d28e57d35f60c98695a4c1f809738143c41cc94e19f63603b38a04e64f6622a4
MD5 0b67f418df8d4075e6b9ab1c08616e7e
BLAKE2b-256 494a6959c42ebef4fd42ee36e993f5789da3ba5ae64ac760541c227c782c125f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page