Python read-write Protobuf

Source: Internet
Author: User

0. Pre-preparation Official PROTOBUF definition

https://code.google.com/p/protobuf/

Python Usage Guide https://developers.google.com/protocol-buffers/docs/pythontutorialhttp://blog.csdn.net/love_newzai/ article/details/6906459 installing Python support for protobuf wget Https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.bz2tar -VXJF PROTOBUF-2.5.0.TAR.BZ2CD protobuf-2.5.0./configure--prefix=/home/admin/mypython/make; Make install1 preparing the. proto file Struct_oss_pb.proto
1 message entity_attr2 {3Required Int32 attr_id = 1; Attribute type identification, for example: the Caption property is 1, the Body property is 2, the Picture property is 3, the Discovery Time property is 4, the original URL property is 5, the parent page property is 6;4Required bytes attribute = 2; //attribute type description, such as "title", "Body", "picture", "Discovery Time", "Original URL", "parent page", etc.5Repeated bytes value = 3; //property value, except that "picture" retains only osskey, and the other retains the original text. Considering that many images are retained in the article, repeated is used. 6 };7 8 message Entity_desc9 {TenRequired Int32 entity_id = 1; Entity type identification, for example: News 1, the novel is 2.  OneRequired bytes Entity_name = 2; //entity name, such as: News Theme Event keywords, novel name and so on.  ARepeated entity_attr attributes = 3; //attribute description, format see entity_attr.  -};
2. Convert proto to xxx_pb2.py, then import the py in your program Protoc--python_out=.//struct_oss_pb.proto Get struct_oss_pb_pb2.py3. Read and write protobuf example python test_pb.py
01#CODING:GBK02ImportSTRUCT_OSS_PB_PB2Entitydesc=Struct_oss_pb_pb2.entity_desc ()entitydesc.entity_id=105 entitydesc.entity_name='haha'06 07#Create ProtoEntityattr=entitydesc.attributes.add ()#Nested Messageentityattr.attr_id = 1110 Entityattr.attribute ='title'. Decode ('GBK'). Encode ('Utf-8')Entityattr.value.append ("title ADFADF")  Entity_attr_str=entityattr. Serializetostring ()14PrintEntity_attr_strEntitydesc_str=Entitydesc. Serializetostring ()16PrintEntitydesc_str17Print '----'18#ReadENTITYATTR2 =struct_oss_pb_pb2.entity_attr ()20ENTITYATTR2. Parsefromstring (ENTITY_ATTR_STR)21stPrintentityattr2.attr_id22PrintEntityattr2.attribute.decode ('Utf-8'). Encode ('GBK')23 forIinchEntityattr2.value:24PrintI25 26Print '----'Entitydesc2=Struct_oss_pb_pb2.entity_desc ()28ENTITYDESC2. Parsefromstring (ENTITYDESC_STR)29Printentitydesc2.entity_id30#repeated entity_attr attributes, because it is repeated need to traverse31 forOneattinchentitydesc2.attributes:32Printoneatt.attr_id33 forIinchOneatt.value:34PrintI

PROTOBUF defines a set of basic data types. Almost all can be mapped to the underlying data types of languages such as C++\java.

PROTOBUF Data types

Describe

Packaged

C + + language mapping

bool

Boolean type

1 bytes

bool

Double

64-bit floating point number

N

Double

Float

32 is a floating-point number

N

Float

Int32

32-bit integers,

N

Int

UInt32

Unsigned 32-bit integer

N

unsigned int

Int64

64-bit integer

N

__int64

UInt64

64 for unsigned integer

N

unsigned __int64

Sint32

32-bit integers for more efficient handling of negative numbers

N

Int32

Sing64

64-bit integers handle negative numbers more efficiently

N

__int64

Fixed32

32-bit unsigned integer

4

unsigned int32

Fixed64

64-bit unsigned integer

8

unsigned __int64

Sfixed32

32-bit integers, capable of handling negative numbers at higher efficiency

4

unsigned int32

Sfixed64

64 is an integer

8

unsigned __int64

String

Only ASCII characters can be processed

N

std::string

bytes

Used to process multibyte language characters, such as Chinese

N

std::string

Enum

Can contain a user-defined enumeration type UInt32

N (UInt32)

Enum

Message

Can contain a user-defined message type

N

Object of Class

Python read-write Protobuf

Related Article

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.