构建Python protobuf消息实例时,我不断收到位置参数错误。

huangapple go评论103阅读模式
英文:

When constructing an instance of a Python protobuf message, I keep getting positional argument error

问题

  1. 我正在使用pb_2并想构建消息的实例但一直遇到位置参数错误我可以从所有其他消息中构建实例但当涉及到以下示例时我卡住了
  2. 我有以下三条消息
  3. message Foo {
  4. int32 num = 1;
  5. repeated KeyValuePair features = 2;
  6. }
  7. message KeyValuePair {
  8. string key = 1;
  9. Value value = 2;
  10. }
  11. message Value {
  12. oneof oneOfValue {
  13. string strValue = 1;
  14. int64 intValue = 2;
  15. }
  16. }
  17. 我想构建一个类似于以下的Foo实例
  18. foo {
  19. num: 123
  20. features: [KeyValuePair {key: 'key1', value: 'value1'},
  21. KeyValuePair {key: 'key2', value: 'value2'}]
  22. }
  23. 我尝试了以下方法但一直收到位置参数错误'不允许位置参数'):
  24. my_foo = Foo(num=123,
  25. features=[KeyValuePair(key='key1', value='value1'),
  26. KeyValuePair(key='key2', value='value2')])
  27. 我没有使用任何位置参数有人能帮忙吗
  28. 谢谢
英文:

I'm using pb_2 and want to consturct instances of a message but keep facing positional argument error. I can construct instances from all other messages but when it comes to the following example, I get stcuk.

I have the following three messages:

  1. message Foo {
  2. int32 num = 1;
  3. repeated KeyValuePair features = 2;
  4. }
  5. message KeyValuePair {
  6. string key = 1;
  7. Value value = 2;
  8. }
  9. message Value {
  10. oneof oneOfValue {
  11. string strValue = 1;
  12. int64 intValue = 2;
  13. }
  14. }

I want to construct an instance of Foo that looks like this:

  1. foo {
  2. num: 123
  3. features: [KeyValuePair {key: 'key1', value: 'value1'},
  4. KeyValuePair {key: 'key2', value: 'value2'}]
  5. }

I tried the following and keep getting positional argument error ('no positional arguments allowed'):

  1. my_foo = Foo(num=123,
  2. features=[KeyValuePair(key='key1', value='value1'),
  3. KeyValuePair(key='key2', value='value2')])

I am not using any positional arguments. Can anyone help?

Thanks!

答案1

得分: 1

你似乎错误地实例化了 Foo,请注意我对 Protocol Buffers 不太熟悉... 你已经声明了 KeyValuePair 如下:

  1. message KeyValuePair {
  2. string key = 1;
  3. Value value = 2;
  4. }

这意味着它将一个字符串值映射到一个 Value 实例。所以你需要这样做:

  1. my_foo = Foo(
  2. num=123,
  3. features=[
  4. KeyValuePair(key="key1", value=Value(strValue="value1")),
  5. KeyValuePair(key="key2", value=Value(strValue="value2")),
  6. ],
  7. )

在我的系统上,使用你的示例 .proto 文件(在顶部添加 syntax = "proto3";),这将不会产生错误。

英文:

With the caveat that I am not familiar with protobuf...aren't you instantiating Foo incorrectly? You've declared that a KeyValuePair looks like:

  1. message KeyValuePair {
  2. string key = 1;
  3. Value value = 2;
  4. }

So it maps a string value to a Value instance. That means you would need:

  1. my_foo = Foo(
  2. num=123,
  3. features=[
  4. KeyValuePair(key="key1", value=Value(strValue="value1")),
  5. KeyValuePair(key="key2", value=Value(strValue="value2")),
  6. ],
  7. )

On my system, using your example .proto file (adding syntax = "proto3"; at the top), that runs without errors.

huangapple
  • 本文由 发表于 2023年7月10日 21:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定