gRPC model definitions for Python microservies
Project description
PyMicro Models
This repository contains the gRPC model definitions (protobuf files) and generated Python code for Python microservices.
Overview
This package provides:
- Protocol Buffer definitions for gRPC services
- Generated Python gRPC stubs and type hints
- Code generation scripts for easy updates
Directory Structure
py-micro-model/
├── proto/ # Protocol Buffer definitions
├── src/py_micro/model # Generated Python code (auto-generated)
├── scripts/ # Code generation scripts
│ └── generate.py # Main generation script
├── Makefile # Build automation
├── pyproject.toml # Poetry configuration
└── README.md # This file
Getting Started
Prerequisites
- Python 3.8+
- Poetry
Installation
- Install dependencies:
make install
# or
poetry install
- Generate gRPC code:
make generate
# or
poetry run python scripts/generate.py
Usage
Adding New Services
- Create a new
.protofile in theproto/directory - Define your service and message types following Protocol Buffer v3 syntax
- Run code generation:
make generate
Code Generation
The code generation script (scripts/generate.py) will:
- Clean the
src/directory - Generate Python gRPC stubs from all
.protofiles - Create proper Python package structure with
__init__.pyfiles - Fix import statements for proper module resolution
- Generate type stubs (
.pyifiles) for better IDE support
Example Proto Definition
syntax = "proto3";
package my_service.v1;
service MyService {
rpc GetItem(GetItemRequest) returns (GetItemResponse);
}
message GetItemRequest {
string id = 1;
}
message GetItemResponse {
string id = 1;
string name = 2;
}
Development
Code Formatting
make format
# or
poetry run black scripts/
poetry run isort scripts/
Linting
make lint
# or
poetry run flake8 scripts/
poetry run mypy scripts/
Integration with Services
To use the generated models in your service:
- Add this package as a dependency in your service's
pyproject.toml:
[tool.poetry.dependencies]
py-micro-models = "^1.0.0"
- Import and use the generated classes:
from py_micro.model.template_service_pb2 import HealthCheckRequest
from py_micro.model.template_service_pb2_grpc import TemplateServiceServicer
Best Practices
- Versioning: Use package versioning in your proto definitions (e.g.,
my_service.v1) - Backwards Compatibility: Follow protobuf best practices for field numbering and evolution
- Documentation: Document your services and messages with comments
- Validation: Consider adding validation rules using proto annotations
- Testing: Write tests for your proto definitions and generated code
Troubleshooting
Common Issues
- Import Errors: Make sure you've run
make generateafter modifying proto files - Module Not Found: Ensure the
src/py_micro/modeldirectory has proper__init__.pyfiles - Type Checking: Use the generated
.pyifiles for better IDE support
Regenerating Code
If you encounter issues with generated code:
make clean
make generate
Contributing
- Add or modify
.protofiles in theproto/directory - Run tests to ensure everything works
- Update documentation if needed
- Regenerate code before committing
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
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 py_micro_models-1.0.0.tar.gz.
File metadata
- Download URL: py_micro_models-1.0.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.8.0-59-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d82def3afbdc5077346619748416dd46cec03a4a756664756398bf24c71c0029
|
|
| MD5 |
2de6eda753934a436ac699c3138b5f57
|
|
| BLAKE2b-256 |
1a79a8df60d1655af9d56b3876280934402f436b84ba9bed4f6ef155385518e8
|
File details
Details for the file py_micro_models-1.0.0-py3-none-any.whl.
File metadata
- Download URL: py_micro_models-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.8.0-59-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4468d211f24145e16273f6e71b4398b28d22dea13e2243e1c0700a94efbd198
|
|
| MD5 |
f534222018b2cadbcd0a039f89e07130
|
|
| BLAKE2b-256 |
4b5f7e8027840e45fa36c29949226adaac6346db09033fb2dbe4c7e6ae92dea4
|