Protocol Buffers using Python type annotations
Project description
pure-protobuf
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
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
Release history Release notifications | RSS feed
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.0a4.tar.gz
(18.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pure_protobuf-3.0.0a4.tar.gz.
File metadata
- Download URL: pure_protobuf-3.0.0a4.tar.gz
- Upload date:
- Size: 18.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-1036-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fb7229046dfe05345a9743f4bb944db72164bab233577d8bdb8aed8c6db13d7
|
|
| MD5 |
e28d228a63bd003eafa1dd1d81abd32d
|
|
| BLAKE2b-256 |
50fed06dece2f5dcea58496d81d86435815b7d650f599d38675c902c26ee95ae
|
File details
Details for the file pure_protobuf-3.0.0a4-py3-none-any.whl.
File metadata
- Download URL: pure_protobuf-3.0.0a4-py3-none-any.whl
- Upload date:
- Size: 27.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-1036-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8c0f8180338e38617ea0d5f32b9816c183f8b9a6553b8bb2853bee2869fbfaa
|
|
| MD5 |
c127acd04248c9fa3dfc75cd971bfd7e
|
|
| BLAKE2b-256 |
0f7ea669846831a14e3c49dba706dd2ca74938cf47fd3246ed841162b45164f7
|