Package: protox
ProtoxProtocol buffers implementation for Python 3 that generates human-readable code with type hinting supportQuick examplefromenumimportIntEnumfromprotoximportMessagefromprotoximportUInt32,String,EnumFieldclassUser(Message):classRole(IntEnum):USER=1ADMIN=2id:int=UInt32(number=1,required=True)email:str=String(number=2,required=True)role:Role=EnumField(Role,number=3,required=True,default=Role.USER)user=User(id=1,email="john@doe.com",role=User.Role.USER)user_data=user.to_bytes()print(User.from_bytes(user_data))# Outputs:# message User#     id = 1#     email = 'john@doe.com'#     role = Role.USERMore examplesEXAMPLES.mdGrpclib exampleCode generationCode generator implemented as a protoc plugin so you have to install it first:Install protoc (Ubuntu)sudoaptinstallprotobuf-compilerInstall protoc (Mac OS)brewinstallprotobufInstall protoxpython3-mpipinstallprotoxGenerate messagesprotoc\--proto_path=protobuf_src\--protox_out=.\./protobuf_src/user.protoGenerate grpclib service with all required dependenciesThe following code generates serviceping_pong.protofrom theprotobuf_srcdirectory into{$PROJECT_ROOT}/app/protobuf.The protox plugin supports 3 options:--base-package=path/to/protobuf/out--grpclib  # generates grpclib services--with-dependencies  # generates dependencies for the given protobuf fileThe options to the plugin are passed using the--protox_opt="{plugin options here}"commandprotoc\--proto_path=protobuf_src\--protox_out=.\--protox_opt="--base-package=app/protobuf --grpclib --with-dependencies"\./protobuf_src/ping_pong.protoCore conceptsHuman-readable python3.6+ generated code with type hintingSupport protobuf 2 and 3 at the same timeNonevalues instead of zero values in fields for both proto2 and proto3Message.has_field()in both proto2 and proto3Protocols are easy to describe without code generationMessages implemented in more pythonic way: to_bytes() instead of SerializeToString()Enums are just enums python int enumsFeaturesMessagesEnumsNested messagesMapsWell-known typesRepeated fieldsRepeated messagesRepeated enumsCustom Message.to_python() / from_python() functionsGroup fields (Deprecated by protobuf)Code generator featuresProtobufGrpclibGrpc.ioCustom python package for protobuf out messagesCompile protobuf file with dependenciesNames mangling to avoid reserved names collisionsRecursive messages/enums supportField names to_snake_case supportDifference with google's protobuf implementationBinary protocol works exactly as google's implementation does.The difference is in the way messages behave:Fields that were not explicitly set are None rather than zero-valuesMethods like SerializeToString() were changed to more pythonic alternatives like to_bytes() / from_bytes()Enums are just python int enumsGenerated code exampleGrpclib service examplefromenumimportIntEnumimportprotoxclassUser(protox.Message):classStatus(IntEnum):USER=1ADMIN=2id:intemail:strstatus:'User.Status'def__init__(self,*,id:int=None,email:str=None,status:'User.Status'=None,):super().__init__(id=id,email=email,status=status,)protox.define_fields(User,id=protox.Int32(number=1,required=True),email=protox.String(number=2,required=True),status=protox.EnumField(number=3,py_enum=User.Status,default=User.Status.USER,required=True),)