Install protocol buffer under Mac and use Python to parse

Source: Internet
Author: User
Tags naming convention

Install protocol buffer under Mac and use Python to parse

Due to business needs, a highly efficient and extensible structured data storage Method protocol buffer (PB) is needed in the project. A lot of people may be unfamiliar with this new thing, we all know Xml,json, really rarely heard Pb, below I will summarize the PB in the MAC environment and the simple process of using Python parsing.

First download the latest PB source code, put in the corresponding folder decompression.

The installation steps are as follows (emphasis: if the installation is unsuccessful, install as an administrator with sudo in front of each command):

If it's done, you don't need a tar-xzf protobuf-2.1.0.tar.gz.

CD protobuf-2.1.0./configure--prefix= $INSTALL _dir (the directory to be installed can be omitted as an administrator, the system will default to a location) make a check make INSTALL

If all goes well, you can use the following statement to detect:

Protoc--version (Will output version number, if the elevation command not found, then it bothers you to reinstall, remember to run as Administrator)

After this is done, we can get a simple look at PB:

First we need to write a proto file that defines the structured data that we need to process in our program, and in the PROTOBUF terminology, structured data is called a Message. Proto files are very similar to Java or C-language data definitions. Listing 1 shows the contents of the proto file in the example application. Listing 1. proto file

Package LM; Message person {required Int32 id = 1;//id Required String str = 2;//str optional Int32 opt = 3;//optional field}

A good habit is to seriously treat the file name of the proto file. For example, the naming convention is set as follows:

PackageName.MessageName.proto

In the example above, the package name is LM, which defines a message helloworld that has three members, the ID of type int32, and the other is a member of type string str. Opt is an optional member, that is, the message may not contain the member.

Next we compile this proto file to try and see if we can succeed:

Suppose your proto file is stored under the $SRC _dir, and you want to place the resulting file in the same directory, you can use the following command:

protoc-i= $SRC _dir--python_out= $DST _dir $SRC _dir/a.proto

If your address is right, and the file name does not start with illegal characters such as numbers, you should be happy. You should see a file in your

$DST _dir directory, a_pb2.py, open to see, which part of you should have no problem,

At least you write on the PB in the few properties can find it, and class, do not say, note, The following is the key:

After compiling how to use, if not to use, it does not work on it?

We customize a text.py file with the following code:

Import a_pb2

p = a_pb2. Person ()

#print p.__doc__

P.name = "Gtts" # name should be familiar.

Print p.name

Run this code, the tragic error is: No MoD Ule named Google.protobuf

How to fix it, as if we are missing something, the workaround is as follows:

Execute python setup.py install for installation, you may generally need such as " Setuptools-0.6c11-py2.6.egg "file, copy it to the above Python directory,

Confirm that the installed command is sudo python./setup.py Test (don't forget sudo, or you won't succeed)

If the configuration succeeds, the prompt is as follows:

---------------------------------------------------

....... Omit a lot of words ...

Ran 193 tests in 0.520s

OK

----------------------------------------------------

All right, celebrate!

Install protocol buffer under Mac and parse it with Python

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.