Protobuf是Google开发的序列化结构数据的一套工具,适合用于数据存储,以及不同语言不同应用之间进行通信的数据交换格式。目前Google提供了C++,Python,Java,Go等语言的支持。
Protobuf的安装
在Protobuf的github主页上,google/protobuf,可以找到不同语言的安装方法。
定义Protobuf格式
syntax = "proto3";// package 在python中没用,但是在其他语言的工程中可以避免命名冲突package tutorial;import "google/protobuf/timestamp.proto";// [START messages]// message 是包含多个值的集合,支持bool,int32,float,double,string等message Person { /** =1,=2,是每个值唯一对应的tag* 1-15因为使用少于1 byte,通常用来表示repeated的值* >16的数用来表示其他值*/ string name = 1; int32 id = 2; // Unique ID number for this person. string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { string number = 1; PhoneType type = 2; } /** 每个值都需要一个标记,有三种类型的标记* required: 必须赋值,否则会出现未初始化的错误* optional: 可以赋值,可以为空,赋默认值* repeated: 赋任意个数的值*/ repeated PhoneNumber phones = 4; google.protobuf.Timestamp last_updated = 5;}// Our address book file is just one of these.message AddressBook { repeated Person people = 1;}// [END messages]
定义好protobuf的格式之后,就需要编译,得到读写该protobuf的类文件。
protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto
编译之后将会生成 addressbook_pb2.py。
Protobuf的使用:写信息
#! /usr/bin/env python# 根据提示,用户输入address信息,然后写入AddressBook中。import addressbook_pb2import systry: raw_input # Python 2except NameError: raw_input = input # Python 3# 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.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.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: with open(sys.argv[1], "rb") as f: address_book.ParseFromString(f.read())except IOError: print(sys.argv[1] + ": File not found. Creating a new file.")# Add an address.PromptForAddress(address_book.people.add())# Write the new address book back to disk.# SerializeToString 用来序列化message,并返回PB化之后的二进制string值。with open(sys.argv[1], "wb") as f: f.write(address_book.SerializeToString())
调用该python文件, 进行符合protobuf格式的数据输入,例如
$ python add_person.py address_book_test
Enter person ID number: 123
Enter name: test
Enter email address (blank for none): test@google.com
Enter a phone number (or leave blank to finish): 0001112222
Is this a mobile, home, or work phone? work
Enter a phone number (or leave blank to finish): 1110003333
Is this a mobile, home, or work phone? work
Enter a phone number (or leave blank to finish):
然后将会生成一个 address_book_test 的PB化后的二进制文件,类似
test{test@google.com"
0001112222"
1110003333
Protobuf的使用:读信息
读信息,也叫做解析PB化的数据。
#! /usr/bin/env pythonfrom __future__ import print_functionimport addressbook_pb2import sys# Iterates though all people in the AddressBook and prints info about them.def ListPeople(address_book): for person in address_book.people: print("Person ID:", person.id) print(" Name:", person.name) if person.email != "": print(" E-mail address:", person.email) for phone_number in person.phones: if phone_number.type == addressbook_pb2.Person.MOBILE: print(" Mobile phone #:", end=" ") elif phone_number.type == addressbook_pb2.Person.HOME: print(" Home phone #:", end=" ") elif phone_number.type == addressbook_pb2.Person.WORK: print(" Work phone #:", end=" ") print(phone_number.number)# Main procedure: Reads the entire address book from a file and prints all# the information inside.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.with open(sys.argv[1], "rb") as f: address_book.ParseFromString(f.read())ListPeople(address_book)
输出结果类似于
Person ID: 123
Name: test
E-mail address: test@google.com
Work phone #: 0001112222
Work phone #: 1110003333
Message常用的方法:IsInitialized(): 检查所有required的值是否已经赋值
_str_(): 用于debug,返回人类可读的数据
CopyFrom(other_msg): 根据已知message对新message赋值
Clear(): 将所有元素清空
SerializedToString(): 将message数据进行序列化,返回PB化之后的二进制string
ParseFromString(): 将二进制string,解析PB数据,返回message数据