微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Google 的开发者网站上使用 Python 制作基本的 protobuf 程序?

如何解决如何在 Google 的开发者网站上使用 Python 制作基本的 protobuf 程序?

我正在关注 the tutorial of protobuf using Python(没有针对 JavaScript 的)。它不起作用......我认为它可能作为 proto2 和 Python 2 程序已经过时。如何让它发挥作用?

所以我开始创建一个文件 address.proto

Syntax = "proto2";

package tutorial;

message Person {
  optional string name = 1;
  optional int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    optional string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}
  1. 然后我在 Mac 上安装了 protoc

  2. 然后我创建了两个文件InOut,并将address.proto移动到In中,然后运行:

protoc -I=In --python_out=Out In/address.proto 

然后创建了一个文件Out/address_pb2.py,然后我去了Out,并添加文件run.py

#! /usr/bin/python

import addressbook_pb2
import sys

# This function fills in a Person message based on user input.
def PromptForAddress(person):
  person.id = int(raw_input("Enter person ID number: "))
  person.name = raw_input("Enter name: ")

  email = raw_input("Enter email address (blank for none): ")
  if email != "":
    person.email = email

  while True:
    number = raw_input("Enter a phone number (or leave blank to finish): ")
    if number == "":
      break

    phone_number = person.phones.add()
    phone_number.number = number

    type = raw_input("Is this a mobile,home,or work phone? ")
    if type == "mobile":
      phone_number.type = addressbook_pb2.Person.PhoneType.MOBILE
    elif type == "home":
      phone_number.type = addressbook_pb2.Person.PhoneType.HOME
    elif type == "work":
      phone_number.type = addressbook_pb2.Person.PhoneType.WORK
    else:
      print "UnkNown phone type; leaving as default value."

# Main procedure:  Reads the entire address book from a file,#   adds one person based on user input,then writes it back out to the same
#   file.
if len(sys.argv) != 2:
  print "Usage:",sys.argv[0],"ADDRESS_BOOK_FILE"
  sys.exit(-1)

address_book = addressbook_pb2.AddressBook()

# Read the existing address book.
try:
  f = open(sys.argv[1],"rb")
  address_book.ParseFromString(f.read())
  f.close()
except IOError:
  print sys.argv[1] + ": Could not open file.  Creating a new one."

# Add an address.
PromptForAddress(address_book.people.add())

# Write the new address book back to disk.
f = open(sys.argv[1],"wb")
f.write(address_book.SerializetoString())
f.close()

然后我安装并运行:

pip3 install protobuf --user
pip3 install google --user
pip3 install google-cloud --user

python3 run.py addr.dat

看起来我必须将 run.py 中的代码print 123 转换为 print(123),因为它是 python3,而不是 Python2。它给了:

Traceback (most recent call last):
  File "run.py",line 40,in <module>
    address_book = addressbook_pb2.AddressBook()
NameError: name 'addressbook_pb2' is not defined

我也将文件 addressbook_pb2.py 复制到 foo.py,然后改用 import foo,它给出了:

Traceback (most recent call last):
  File "run.py",line 3,in <module>
    import foo
  File "/Users/peter/code/TryProtobuf_Unzipped/TryIt/Out/foo.py",line 34,in <module>
    _descriptor.EnumValueDescriptor(
  File "/Users/peter/Library/Python/3.8/lib/python/site-packages/google/protobuf/descriptor.py",line 732,in __new__
    _message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors should not be created directly,but only retrieved from their parent.

如何让它发挥作用?

解决方法

您的 address.proto 声明了 tutorial 包。我认为您可以通过 address_book = tutorial.addressbook_pb2.AddressBook()

访问它

至于为什么将文件重命名为 foo.py 后它会崩溃,我不知道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?