Tag: code-generation

  • How to Generate Python Code from Protobuf

    Protocol Buffers, which are protobuf for short, is much compact than XML and JSON and hence a great choice for designing efficient inter-service communication. In this post we will see how to generate Python code from protobuf code.

    An example protobuf for product recommendation

    syntax = "proto3";
    
    enum ProductCategory {
        DAIRY = 0;
        VEGETABLES = 1;
        PROCESSED_FOOD = 2;
    }
    
    message ProductRecommendationRequest {
        int32 user_id = 1;
        ProductCategory category = 2;
        int32 max_results = 3;
    }
    
    message ProductRecommendation {
        int32 id = 1;
        string name = 2;
    }
    
    message ProductRecommendationResponse {
        repeated ProductRecommendation recommendations = 1;
    }
    
    service Recommendations {
        rpc Recommend (ProductRecommendationRequest) returns (ProductRecommendationResponse);
    }

    Getting grpcio-tools library

    pip install grpcio-tools

    Generating Python code from protobuf

    python -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. products.proto

    Output

    This generates several Python files from the .proto file. Here’s a breakdown:

    • python -m grpc_tools.protoc invokes the protobuf compiler, which will generate Python code from the protobuf code.
    • -I . tells the compiler to look into current directory to find imported protobuf files. We are not importing any files but this option is needed still.
    • --python_out=. --grpc_python_out=. this options tells the compiler where to output the Python files. It will generate two files, and you could put each in a separate directory with these options if you wanted to.
    • products.proto is the path to the protobuf file

    Please let us know your questions in the comments below.