Arista Protobuf / Python gRPC bindings generator & library

0.1.4 · active · verified Thu Apr 16

Arista Protobuf / Python gRPC bindings generator & library (version 0.1.4). It provides tools to compile `.proto` files into Python classes, offering features like JSON serialization/deserialization, one-of support, and customized handling of Google Well-known Types. Forked from `python-betterproto`, it maintains compatibility with `grpclib` for async gRPC support. The project is actively maintained by Arista Networks, with development ongoing, though official PyPI releases have an irregular cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates compiling a simple `.proto` file and then importing and using the generated Python message classes for creation, serialization to JSON, and deserialization from JSON. It requires `protoc` to be installed and accessible in your PATH, or `grpcio-tools` if using the Python `grpc_tools.protoc` module for compilation.

# 1. Create a .proto file (e.g., example.proto)
# syntax = "proto3";
# package hello;
# message Greeting {
#     string message = 1;
# }

# 2. Compile the .proto file (assuming 'example.proto' is in current dir)
# mkdir -p lib
# protoc -I . --python_aristaproto_out=lib example.proto
# OR using grpcio-tools (after pip install grpcio-tools):
# python -m grpc_tools.protoc -I . --python_aristaproto_out=lib example.proto

# 3. Use the generated code (e.g., from lib/hello/__init__.py)
import os
import sys
# Add the 'lib' directory to Python path if not already added
# In a real project, 'lib' would likely be part of your package structure
# or installed.
if os.path.exists('lib') and 'lib' not in sys.path:
    sys.path.insert(0, 'lib')

from hello import Greeting

g = Greeting(message="Hello, Arista!")
print(f"Created message: {g.message}")

# Example of serialization
json_output = g.to_json()
print(f"JSON representation: {json_output}")

# Example of deserialization
g_from_json = Greeting.from_json(json_output)
print(f"Message from JSON: {g_from_json.message}")

view raw JSON →