Package: protolite
Lightweight implementation of Google’s Protocol Buffers in Python.In benchmarks, the protolite encoder ran twice as fast as
Google’s. Using Python’stimeitmodule, the same data for both APIs was
encoded and decoded 10000 times. The lowest time of three attempts was
picked for each:protobuf:  3.6064529418945312 seconds
protolite: 1.7224960327148438 secondsIf we take the ratio of these two times we see that protolite was
about two times faster than its counterpart.Similarly, using Pypy we get about twice the speed:protobuf:  0.807873010635376 seconds
protolite: 0.4414529800415039 secondsThebenchmarkdirectory in thegithubrepository contains the files
needed to re-run the tests . In addition, you will need the protobufPythonlibrary. Try it on your platform, but, keep your machine as quite
as possible so as to not skew the results:PYTHONPATH=$PYTHONPATH:$(pwd) python benchmark/benchmark.pyPass the –pypy flag if you want to use Pypy in order to warm up the
Pypy JIT compiler and get a more accurate result:PYTHONPATH=$PYTHONPATH:$(pwd) pypy benchmark/benchmark.py --pypyYou can also make changes to thebenchmark/messages.protofile to create
your own tests. You’ll need to re-compile themessages.pyandmessages_pb2.pyfiles in thebenchmarkdirectory afterwards by running
themakecommand inside the same directory. Of course, you will needprotocto compile Google’s version.descriptionProtocol Buffers (protobuf) is a data interchange format created by
Google. protolite is a rewrite of its encoder and file generator
specifically created and optimized for Python. The encoder is
optimized for speed taking the language’s properties in mind. The
generator aims to provide ease-of-use and compatibility with the
language. For example, messages are implemented using only
dicts. Familiarity with protobuf is required in order to use protolite
effectively.installationYou can download and install protolite frompypiwith pip:pip install python-protoliteAlternatively, you can clone the repository containing the source code
fromgithuband install protolite via setuptools:git clone https://github.com/thelinuxkid/python-protolite.git
cd python-protolite
python setup.py installusagegenerating filesprotolite comes with a utility that generates Python files structured
for efficiency and readbility. After the installation you will have an
executable file calledpython-protolitec. Its most simple usage
takes two positional arguments. The first is a list of the protobuf
definition files and the second a directory where to write the Python
version of those files:python-protolitec proto/*.proto pythonThe output files will retain the same file name as the source; only the
extension will be changed. For example, the fileproto/messages.protowill produce the filepython/messages.py. You can use the--helpflag to view the other options offered bypython-protolitec.encodingLet’s say you have a protobuf file calledmessages.protocontaining:message User {
    optional uint32 userID = 1;
    enum UserType {
        STANDARD = 0;
        ADMIN = 1;
    }
    optional UserType type = 2;
}python-protolitewill create a Python modulemessageswith auserobject which has adecodeand  anencodemethod. To encode a
message you would do something like:import messages

msg_enc = {'user_id': 123, 'type': messages.user_type.STANDARD}
data = messages.user.encode(msg_enc)As you can see,python-protolitechanges camel-case variable names to
underscore. On the other end, to decode the message you would do
something similar:import messages

msg_dec = messages.user.decode(data)The variable msg_dec will be equal to msg_enc.printingThe message objects also contain a pretty print method. Callingmessage.user.pprint(msg_enc)would produce:{
    "type": "STANDARD",
    "user_id": 123
}You can pass the keyword argumentstreamtopprintto write to
a stream different than sys.stdout.parserIf you download the source code fromgithubyou will see agrammardirectory at the root level. This directory contains all
the files used to create the parser and lexer inprotolite.parser,
the module used bypython-protolitecto parse the protobuf
definition files. If you are familiar withAntlryou can edit theproto_lexer.gandproto_parser.gfiles in this directory to create a
new Python parser and/or lexer using the Antlr jar in the same directory:cd grammar
java -jar antlr-3.1.3.jar -fo . proto_lexer.g
java -jar antlr-3.1.3.jar -fo . proto_parser.gThis will create four files:proto_lexer.py,proto_lexer.tokens,proto_parser.pyandproto_parser.tokens. You can leave the *.tokens files where they
are but move the *.py files to protolite/parser to use your new parser
withpython-protolitec. If you want to use a different version of
Antlr do so at your own risk. You will likely need the new Antlr
version to match the Python runtime version in setup.py.