jea*_*ean 5 python protocol-buffers
我想要在python中生成的protobuf类的智能感知。但是生成的protobuf类的实现是特殊的,代码类似于:
class X(_message.Message):
__metaclass__ = _reflection.GeneratedProtocolMessageType
DESCRIPTOR = _X
Run Code Online (Sandbox Code Playgroud)
大多数的Python IDE只能智能感知__metaclass__和DESCRIPTOR两个成员而不是成员.proto文件中定义。
怎么做?
如果您使用的是最新的 Python 版本 (3.7+),那么您可以尝试我的https://github.com/danielgtaylor/python-betterproto项目。它生成具有正确类型的数据类,VSCode、PyCharm 和其他可使用这些数据类来提供类型提示和智能感知。
例如,给定以下输入:
syntax = "proto3";
// Some documentation about the Test message.
message Test {
// Some documentation about the count.
int32 count = 1;
}
Run Code Online (Sandbox Code Playgroud)
你会得到这样的输出:
# Generated by the protocol buffer compiler. DO NOT EDIT!
# sources: int32.proto
# plugin: python-betterproto
from dataclasses import dataclass
import betterproto
@dataclass
class Test(betterproto.Message):
"""Some documentation about the Test message."""
# Some documentation about the count.
count: int = betterproto.int32_field(1)
Run Code Online (Sandbox Code Playgroud)
它比官方生成的描述符类更容易阅读。
| 归档时间: |
|
| 查看次数: |
272 次 |
| 最近记录: |