Read Protocol Buffers data in Python3

Today, I'm talking about Python + Protocol Buffers, which suddenly changed my mind from the usual statistics and machine learning.

Protocol Buffers is a convenient serialization format. If you want to use it in Python 3, you need to use the latest code on GitHub at the time of writing.

Python 2 support has increased until 2020 The news says that you should use the new 2 system, but the latest technology As an engineer to follow, I think that there are many cases where there is a strong will to never write anything other than Python 3.x.

Environment

As an environment, clone Protocol Buffers from GitHub, build it, and set it up with Python as follows.

git clone git://github.com/openx/python3-protobuf.git
cd python3-protobuf
./autogen.sh
./configure --prefix=$PREFIX #Specify the installation destination of protobuf
make
make check
sudo make install

cd python #Python binding
python setup.py build
python setup.py test
sudo python setup.py install

You can now use Protocol Buffers from Python3. It's easy.

File structure definition and conversion

Protocol Buffers defines the structure in a file named .proto. It is said that the key in JSON is converted to a numerical value and can be exchanged in a smaller data size.

protoc -I=. --python_out=. schema.prot

A file such as schemaXX.py will be generated, so you can import it from the script you want to use.

Use from Python

This is the image that actually reads the data.

import schema_pb2 #Import the generated Python file
import base64
import json

event = schema_pb2.nb_event()
event.ParseFromString(base64.b64decode(value))
ts = event.timestamp

#Try converting to JSON
obj = {}
obj['event_type'] = event.type
obj['seq'] = event.seq
obj['timestamp'] = event.timestamp
obj['op'] = event.op

#Convert data once stored in the dictionary to JSON
json_dump = json.dumps(obj, ensure_ascii=False)
print(json_dump)

Recommended Posts

Read Protocol Buffers data in Python3
[python] Read data
Read DXF in python
Read table data in PDF file with Python
Handle Ambient data in Python
Use Measurement Protocol in Python
Display UTM-30LX data in Python
Read Euler's formula in Python
Read Namespace-specified XML in Python
Read Outlook emails in Python
Read Fortran output in python
Read json data with python
Get data from Quandl in Python
Disable python2.6 ssl3 protocol in centos6
Handle NetCDF format data in Python
Read PNG chunks in Python (class)
Hashing data in R and Python
Read files in parallel with Python
Create and read messagepacks in Python
Get additional data in LDAP with python
Ant book in python: Sec. 2-4, data structures
Try working with binary data in Python
Put protocol buffers into sqlite with python
Get Google Fit API data in Python
Python: Preprocessing in machine learning: Data acquisition
Get Youtube data in Python using Youtube Data API
Read the file line by line in Python
Read the file line by line in Python
Read and write JSON files in Python
Easily graph data in shell and Python
[TensorFlow] Python ⇔ Protocol Buffers ⇔ GPU / Distributed Computing
Python: Preprocessing in machine learning: Data conversion
[Python] Read the specified line in the file
Read text in images with python OCR
Books on data science to read in 2020
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Data analysis python
Epoch in Python
Discord in Python
Read pandas data
gRPC-Methods used when dealing with protocol buffers types in Python CopyFrom, Extend
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
[SQLAlchemy] Read data
N-gram in python