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
Metadata
Release files for proto-topy 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| proto_topy-2.0.0.tar.gz | 10.6 kB | Without provenance | Details for proto_topy-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| proto_topy-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for proto_topy-2.0.0-py3-none-any.whl |
Total release size: 17.8 kB
Release files / proto_topy-2.0.0.tar.gz
| Download URL | proto_topy-2.0.0.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9aaf807609dbde4ee2f2e843cc3b1c53e8e3f908cbbd0abae952b532101e3c53
|
|
BLAKE2b-256 checksum How to use checksums |
dc9c6002f894368601964612c1c9982db8037bf11e24776cb191e401656d7130
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / proto_topy-2.0.0-py3-none-any.whl
| Download URL | proto_topy-2.0.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
081ceff3329b9354d170ca06f0db6ded8f3f69839f2b509262a3fd591594b5ee
|
|
BLAKE2b-256 checksum How to use checksums |
3731d32a02bad6ca16d6781ec4e399d010a6ba8ef72e8e2fd660c5789312d7cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for proto-topy 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.