簡體   English   中英

如何使用protobuf在python中設置oneof字段值

[英]How to set oneof field value in python using protobuf

我有一個proofbuf與oneof場。 “ Oneof”采用字段值之一還是全部取值? 不知道是否有辦法設置它。

我在運行"pb2.HasField('signal')時期望一個真實值

原始文件:

message Log {
  string id = 1;       
  Severity severity = 2; 
  string Uid = 3;        
oneof signal{
    GPS gps_log = 1;
    Ignition ignition_log = 2;
  }
enum Severity {
    info = 0;
    critical = 1
}
message GPS{
  EventType event_type = 1;
enum EventType {
    ON = 0;
    OFF = 1;
  }
}

message Ignition{
  EventType event_type = 1;
enum EventType {
    ON = 0;
    OFF = 1;
  }
}

因此,在python中導入pb2之后,如何設置“信號”的值。 當我嘗試:

message=pb2.Log()
res = message.DESCRIPTOR.fields
res = [field.name for field in message.DESCRIPTOR.fields]
The o/p is ['id','severity','Uid','gps_log','ignition']  - All the fields.

如何強制“一個”字段僅包含消息中的一個,例如GPS。 同樣pb2.WhichOneof('signal')返回任何值(空)。 我什至不確定,是否有可能。

好像我缺少主protobuf文件,該文件是我想要的信息。 字段值在prot_master文件中指定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM