Skip to main content

Compile protobuf strings into Python module objects at runtime.

Project description

test codecov version wheel python version python implementation

Compile protobuf strings into Python module objects at runtime. — no files, no code generation step:

from proto_topy import ProtoModule

module = ProtoModule(
    source="message Hello { optional string name = 1; }",
).compiled()

msg = module.py.Hello(name="world")
assert msg.name == "world"

It is useful for programs needing to en/decode protobuf messages for which the definition is provided as a string at runtime.

Installation

pip install proto-topy

Prerequisite: proto-topy needs protoc to be installed. On macOS, a simple brew install protobuf shall suffice.

single proto example: address book

Adaptation of the protocolbuffers example from Google tutorial:

from io import BytesIO
import requests
from proto_topy import ProtoModule

example_source = requests.get(
    "https://raw.githubusercontent.com/protocolbuffers/protobuf/main/"
    "examples/addressbook.proto").text

module = ProtoModule(source=example_source).compiled()

# Serialize an address book
buffer = BytesIO()
address_book = module.py.AddressBook()
person = address_book.people.add()
person.id = 111
person.name = "A Name"
person.email = "a.name@mail.com"
phone_number = person.phones.add()
phone_number.number = "+1234567"
phone_number.type = module.py.Person.MOBILE
buffer.write(address_book.SerializeToString())

# Deserialize it back
buffer.seek(0)
address_book = module.py.AddressBook()
address_book.ParseFromString(buffer.read())
for person in address_book.people:
    assert person.id == 111
    assert person.name == "A Name"
    assert person.email == "a.name@mail.com"

multiple protos example

When several definition strings need to be considered, use a ProtoCollection:

from proto_topy import ProtoModule, ProtoCollection
from google.protobuf.timestamp_pb2 import Timestamp

module1 = ProtoModule(
    file_path="p1/p2/other2.proto",
    source="""
    syntax = "proto3";
    import "google/protobuf/timestamp.proto";
    message OtherThing2 {
        google.protobuf.Timestamp created = 1;
    }"""
)

module2 = ProtoModule(
    file_path="p3/p4/test6.proto",
    source="""
    syntax = "proto3";
    import "p1/p2/other2.proto";
    message Test6 {
        OtherThing2 foo = 1;
    }"""
)

collection = ProtoCollection(module1, module2).compiled()

Test6 = collection.modules["p3/p4/test6.proto"].py.Test6
OtherThing2 = collection.modules["p1/p2/other2.proto"].py.OtherThing2

ts = Timestamp(seconds=1234567890)
thing = OtherThing2(created=ts)
msg = Test6(foo=thing)

assert msg.foo.created.seconds == 1234567890

Stream of delimited messages

To decode a stream of contiguous protobuf messages of the same type, use DelimitedMessageFactory. Example:

from io import BytesIO
from proto_topy import ProtoModule, DelimitedMessageFactory

module = ProtoModule(
    source="""
    syntax = "proto3";
    message TestInt { int32 val = 1; }
    """
).compiled()

integers = (module.py.TestInt(val=val) for val in range(10))
factory = DelimitedMessageFactory(BytesIO(), *integers)

factory.rewind()
for i, (offset, msg) in enumerate(factory.message_read(module.py.TestInt)):
    assert msg.val == i

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

proto_topy-2.0.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

proto_topy-2.0.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file proto_topy-2.0.0.tar.gz.

File metadata

  • Download URL: proto_topy-2.0.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for proto_topy-2.0.0.tar.gz
Algorithm Hash digest
SHA256 9aaf807609dbde4ee2f2e843cc3b1c53e8e3f908cbbd0abae952b532101e3c53
MD5 2c725852fbf9caddda219db0385b20c7
BLAKE2b-256 dc9c6002f894368601964612c1c9982db8037bf11e24776cb191e401656d7130

See more details on using hashes here.

File details

Details for the file proto_topy-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: proto_topy-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for proto_topy-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 081ceff3329b9354d170ca06f0db6ded8f3f69839f2b509262a3fd591594b5ee
MD5 065fa6877593b975629b2f0544cb1a96
BLAKE2b-256 3731d32a02bad6ca16d6781ec4e399d010a6ba8ef72e8e2fd660c5789312d7cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page