Skip to main content

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

  1. Install dependencies:
make install
# or
poetry install
  1. Generate gRPC code:
make generate
# or
poetry run python scripts/generate.py

Usage

Adding New Services

  1. Create a new .proto file in the proto/ directory
  2. Define your service and message types following Protocol Buffer v3 syntax
  3. 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 .proto files
  • Create proper Python package structure with __init__.py files
  • Fix import statements for proper module resolution
  • Generate type stubs (.pyi files) 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:

  1. Add this package as a dependency in your service's pyproject.toml:
[tool.poetry.dependencies]
py-micro-models = "^1.0.0"
  1. 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

  1. Versioning: Use package versioning in your proto definitions (e.g., my_service.v1)
  2. Backwards Compatibility: Follow protobuf best practices for field numbering and evolution
  3. Documentation: Document your services and messages with comments
  4. Validation: Consider adding validation rules using proto annotations
  5. Testing: Write tests for your proto definitions and generated code

Troubleshooting

Common Issues

  1. Import Errors: Make sure you've run make generate after modifying proto files
  2. Module Not Found: Ensure the src/py_micro/model directory has proper __init__.py files
  3. Type Checking: Use the generated .pyi files for better IDE support

Regenerating Code

If you encounter issues with generated code:

make clean
make generate

Contributing

  1. Add or modify .proto files in the proto/ directory
  2. Run tests to ensure everything works
  3. Update documentation if needed
  4. Regenerate code before committing

Metadata

Release files for py-micro-models 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-micro-models 1.0.0
File Size Uploaded
py_micro_models-1.0.0.tar.gz 14.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-micro-models 1.0.0
File Interpreter ABI Platform
py_micro_models-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.2 kB

Release files / py_micro_models-1.0.0.tar.gz

Download URL py_micro_models-1.0.0.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d82def3afbdc5077346619748416dd46cec03a4a756664756398bf24c71c0029
BLAKE2b-256 checksum
How to use checksums
1a79a8df60d1655af9d56b3876280934402f436b84ba9bed4f6ef155385518e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.11 Linux/6.8.0-59-generic

Release files / py_micro_models-1.0.0-py3-none-any.whl

Download URL py_micro_models-1.0.0-py3-none-any.whl
Size 18.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4468d211f24145e16273f6e71b4398b28d22dea13e2243e1c0700a94efbd198
BLAKE2b-256 checksum
How to use checksums
4b5f7e8027840e45fa36c29949226adaac6346db09033fb2dbe4c7e6ae92dea4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.11 Linux/6.8.0-59-generic

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page