Python protobuf serialization repeated application

Source: Internet
Author: User

Here is the definition of the proto description file

Message person {
Required String name = 1;
Required Int32 id = 2;
Optional String email = 3;
Enum Phonetype {
MOBILE = 0;
HOME = 1;
Work = 2;
}
Message PhoneNumber {
Required String number = 1;
Optional Phonetype type = 2 [default = HOME];
}
Repeated PhoneNumber phone = 4;
}
Message AddressBook {
Repeated person person = 1;
}

When using GOOGLE.PROTOBUF serialized data for communication in Python, you must encounter how repeated data is created

Here I share with you:

  

Import ADDRESSBOOK_PB2
person = addressbook_pb2. Person ()
Person.id = 1234
Person.name = "John Doe"
Person.email = ""
Phone = Person.phone.add ()
Phone.number = "555-4321"
Phone.type = Addressbook_pb2. Person.home Person.no_such_field = 1 person.id = "1234"

Note the red Line of code, which is the point, if you want to add another word Phone2 = Person.phone.add (), and then assign a value to Phone2

Python protobuf serialization repeated application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.