Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eigenein/protobuf
Python implementation of Protocol Buffers with dataclass-based schemaʼs
https://github.com/eigenein/protobuf
dataclasses google-protocol-buffers library pip-package protobuf protocol-buffers pypi-package pypi-packages python python-3 python-data-classes python-library python-package type-annotations type-hints
Last synced: about 2 months ago
JSON representation
Python implementation of Protocol Buffers with dataclass-based schemaʼs
- Host: GitHub
- URL: https://github.com/eigenein/protobuf
- Owner: eigenein
- License: mit
- Created: 2011-06-13T18:26:55.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2024-04-30T21:36:04.000Z (5 months ago)
- Last Synced: 2024-05-02T01:27:18.428Z (5 months ago)
- Topics: dataclasses, google-protocol-buffers, library, pip-package, protobuf, protocol-buffers, pypi-package, pypi-packages, python, python-3, python-data-classes, python-library, python-package, type-annotations, type-hints
- Language: Python
- Homepage: https://eigenein.github.io/protobuf/
- Size: 681 KB
- Stars: 235
- Watchers: 11
- Forks: 19
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# `pure-protobuf`
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/eigenein/protobuf/check.yml?label=checks&logo=github)](https://github.com/eigenein/protobuf/actions/workflows/check.yml)
[![Code coverage](https://codecov.io/gh/eigenein/protobuf/branch/master/graph/badge.svg?token=bJarwbLlY7)](https://codecov.io/gh/eigenein/protobuf)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/pure-protobuf.svg)](https://pypistats.org/packages/pure-protobuf)
[![PyPI – Version](https://img.shields.io/pypi/v/pure-protobuf.svg)](https://pypi.org/project/pure-protobuf/#history)
[![PyPI – Python](https://img.shields.io/pypi/pyversions/pure-protobuf.svg?logo=python&logoColor=yellow)](https://pypi.org/project/pure-protobuf/#files)
[![License](https://img.shields.io/pypi/l/pure-protobuf.svg)](https://github.com/eigenein/protobuf/blob/master/LICENSE)Wow! Such annotated! Very buffers!
## 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:
```protobuf
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](https://docs.python.org/3/library/dataclasses.html)
```python title="dataclass_example.py"
from dataclasses import dataclass
from io import BytesIOfrom 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)] = 0request = 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`](https://docs.pydantic.dev/)
```python title="pydantic_example.py"
from io import BytesIOfrom pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from pydantic import BaseModel
from typing_extensions import Annotatedclass SearchRequest(BaseMessage, BaseModel):
query: Annotated[str, Field(1)] = ""
page_number: Annotated[int, Field(2)] = 0
result_per_page: Annotated[int, Field(3)] = 0request = 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
```