Package: pyrobuf
Pyrobuf LibraryIntroductionPyrobuf is an alternative to Google's Python Protobuf library.It generates lightning-fast Cython code that's 2-4x faster than Google's Python
Protobuf library using their C++ backend and 20-40x faster than Google's pure-python
implementation.What's more, Pyrobuf is self-contained and easy to install.RequirementsPyrobuf requires Cython, and Jinja2. If you want to contribute to pyrobuf you
may also want to install pytest.Pyrobufdoes notrequire protoc.Pyrobuf has been tested with Python 2.7 and Python 3.5.Pyrobuf appears to be working on OSX, Linux and Windows (for the latter getting
Cython to work properly is the trickiest bit especially if you are still using
2.7).ContributingPeople use protobuf in many different ways. Pyrobuf handles the use cases of
AppNexus and other contributors, but is not yet a 100% drop-in replacement to
what protoc would generate.You can help make it so!Fork and clone the repository, then run:$ python setup.py developIt will generate the platform specificpyrobuf_listthen compile
thepyrobuf_listandpyrobuf_utilmodules.Unit TestingYou can run the test suite (a work in progress) using py.test directly:$ py.testOr using thetestcommand (which installs pytest if not already available):$ python setup.py testEither method will automatically build all the protobuf message specs intests/protoand point thePYTHONPATHto the built messages before running
the tests.Re-running thedeveloportestcommands will automatically re-build thepyrobuf_listandpyrobuf_utilmodules if necessary.Thecleancommand does the house keeping for you:$ python setup.py cleanIf you find that pyrobuf does not work for one of your proto files, add a minimal
proto file totests/protothat breaks before submitting a pull request.Pull requests including a breaking test are gold!Improving testing is in the cards.InstallationYou may very well be able to just use pyrobuf as is... just pip it!$ pip install pyrobufShould do the trick!To check, you may want to make sure the following command does not raise an
exception:$ python -c "import pyrobuf_list"If it does raise an exception try:$ pip install pyrobuf -v -v -v --upgrade --force --no-cacheCompilingWhen youpip install pyrobufyou get the pyrobuf CLI tool ...:$ pyrobuf --help
usage: pyrobuf [-h] [--out-dir OUT_DIR] [--build-dir BUILD_DIR] [--install] source

a Cython based protobuf compiler

positional arguments:
  source                filename.proto or directory containing proto files

optional arguments:
  -h, --help            show this help message and exit
  --out-dir OUT_DIR     cythonize output directory [default: out]
  --build-dir BUILD_DIR
                        C compiler build directory [default: build]
  --install             install the extension [default: False]
  --package             the name of the package to install toIf you do not want to have to deal with setuptools entry_points idiosyncrasies
you can also do:$ python -m pyrobuf --helpUseSuppose you have installedtest_message.protowhich contains a spec for the
messageTest. In Python, you can import your new message class by running:from test_message_proto import TestWith the message class imported, we can create a new message:test = Test()Now that we have instantiated a messagetest, we can fill individual fields:>>> test.field = 5
>>> test.req_field = 2
>>> test.string_field = "hello!"
>>> test.list_fieldx.append(12)
>>> test.test_ref.field2 = 3.14And access those same fields:>>> test.string_field
'hello!'Once we have at least filled out any "required" fields, we can serialize to a
byte array:bytearray(b'\x10\x05\x1a\x06hello! \x0c2\t\x19\x1f\x85\xebQ\xb8\x1e\t@P\x02')We can also deserialize a protobuf message to our message instance:>>> test.ParseFromString('\x10\x05\x1a\x06hello! \x0c2\t\x19\x1f\x85\xebQ\xb8\x1e\t@P\x02')
25Note that theParseFromStringmethod returns the number of bytes consumed.In addition to serializing and deserializing to and from protobuf messages,
Pyrobuf also allows us to serialize and deserialize to and from JSON and native
Python dictionaries:>>> test.SerializeToJson()
'{"field": 5, "req_field": 2, "list_fieldx": [12], "string_field": "hello!", "test_ref": {"field2": 3.14}}'

>>> test.ParseFromJson('{"field": 5, "req_field": 2, "list_fieldx": [12], "string_field": "hello!", "test_ref": {"field2": 3.14}}')

>>> test.SerializeToDict()
{'field': 5,
 'list_fieldx': [12],
 'req_field': 2,
 'string_field': 'hello!',
 'test_ref': {'field2': 3.14}}Finally, thepyrobuf_utilmodule contains functions for encoding and decoding integers.>>> import pyrobuf_util
>>> pyrobuf_util.to_varint(2**16-1)
bytearray(b'\xff\xff\x03')
>>> pyrobuf_util.from_varint(b'\xff\xff\x03', offset=0)
(65535L, 3)
>>> pyrobuf_util.to_signed_varint(-2**16)
bytearray(b'\xff\xff\x07')
>>> pyrobuf_util.from_signed_varint(b'\xff\xff\x07', offset=0)
(-65536L, 3)Thefrom_varintandfrom_signed_varintfunctions return both the decoded integer and
the offset of the first byte after the encoded integer in the source data.PackagingIf you are compiling multiple messages or a directory of messages and don't want them
all to be built to their own separate package but instead want a single namespace
containing all your messages, you can specify a package name:pyrobuf /path/to/proto/specs --install --package=my_messagesThen you can import your message classes from themy_messagespakcage:>>> from my_messages import MyMessage1, MyMessage2Distributing a Python Package with Pyrobuf ModulesSuppose you have a Python package called 'sample' arranged on disk as follows:sample/
    proto/
        my_message.proto
    sample/
        __init__.py
    setup.pyPyrobuf adds a new setup keywordpyrobuf_moduleswhich can be used to specify either
individual protobuf files or folders containing protobuf files. For example, thesetup.pyfile could look like this:from setuptools import setup, find_packagessetup(
    name="sample",
    version="0.1",
    packages=find_packages(),
    description="A sample package",
    install_requires=['pyrobuf'],
    setup_requires=['pyrobuf'],
    pyrobuf_modules="proto"
)In addition to the package "sample", setuptools will also build a package named
"sample_proto" which will contain the compiled Protobuf messages.Once installed this sample package can be used as follows:>>> from sample_proto import MyMessage
>>> my_message = MyMessage()PerformanceOn my development machine (Ubuntu 14.04), Pyrobuf is roughly 2.0x as fast as
Google's library for message serialization and 2.3x as fast for message
deserialization when using the C++ backend for Google's library:> python tests/perf_test.py
Google took 1.649168 seconds to serialize
Pyrobuf took 0.825525 seconds to serialize
Google took 1.113041 seconds to deserialize
Pyrobuf took 0.466113 seconds to deserializeWhen not using the C++ backend, Pyrobuf is roughly 25x as fast for serialization
and 55x as fast for deserialization:Google took 20.215662 seconds to serialize
Pyrobuf took 0.819555 seconds to serialize
Google took 24.990137 seconds to deserialize
Pyrobuf took 0.455732 seconds to deserializeDifferences from the Google libraryIf pyrobuf is missing a feature from protoc that you need, let us know! We are
trying to make it as easy as possible for you to help make pyrobuf better.For the most part, Pyrobuf should be a drag-and-drop replacement for the Google
protobuf library. There are a few differences, though. First, Pyrobuf does not
currently implement theListFields,WhichOneOf,HasExtension,ClearExtensionandByteSizemethods.Second, Pyrobuf simply assumes that the schema being used for a given message
is the same on the send and receive ends, so changing the type of a field on
one end without changing it on the other may cause bugs; adding or removing
fields will not break anything.