Python Library to generate proto buffer code from python using type annotations
Project description
Protomodel
Python Library to generate proto buffer code from python using type annotations
Installation
Create a virtual enviroment (Optional)
python -m venv venv
source venv/bin/activate # venv/Scripts/activate on Windows
Install package (require python ^3.10 version)
pip install protomodel
Example
Create a python file and add the following code:
from protomodel import message, service, rpc
@message
class HelloRequest:
name: str
@message
class HelloReply:
message: str
@service
class Greeter:
@rpc
def say_hello(hello_request: HelloRequest) -> HelloReply:
pass
Generate file
Run the following command to generate a proto buffer file:
python -m protomodel generate --python_file=hello.py --proto_name=hello.proto
And then you will get the following result:
syntax = "proto3";
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
Add a package name:
python -m protomodel generate --python_file=hello.py --proto_name=hello.proto --package_name=my_package_name
syntax = "proto3";
package my_package_name;
...
Adding another types
Python Code:
@message
class Item:
id: int
name: str
description: str
price: float
enabled: bool
@message
class ItemsList:
items: list[Item]
Result:
message Item {
int32 id = 1;
string name = 2;
string description = 3;
double price = 4;
bool enabled = 5;
}
message ItemsList {
repeated Item items = 1;
}
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
protomodel-0.0.12.tar.gz
(8.1 kB
view hashes)
Built Distribution
Close
Hashes for protomodel-0.0.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 813f3f29e6a928f17477ef18afcb9aedcb47ece17da18304faadb410d73b72fc |
|
MD5 | 295a94b362f14e20b02f410ef9f79d6d |
|
BLAKE2b-256 | a96a38a2bd2cee9d5e153cffa88bdd4fe91a8b05f015d8d4b6fa2c03a2a14d74 |