Registry / serialization / pbtools

pbtools

JSON →
library0.47.0pypypi✓ verified 82d ago

`pbtools` is a Python library for working with Google Protocol Buffers. It provides tools to read, write, encode, and decode Protocol Buffers messages (both binary and text) directly from `.proto` definitions, *without* requiring generated Python code. This simplifies field access and enables decoding of unknown messages. The current version is 0.47.0, and it maintains an active release cadence with incremental updates.

pip install pbtools
INSTALL
IMPORT
SIG · PBTOOLS
P
pbtools
serializationpythonv0.47.0
Install
2.6s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.47.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.6MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.6s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse_file
from pbtools import parse_file
from pbtools import build_database

This quickstart demonstrates how to define a Protocol Buffer message, build a database from its `.proto` definition, create a message instance, encode it to binary, and then decode it back using `pbtools`. It highlights the direct interaction with messages without code generation.

import pbtools import os # Example .proto file content proto_file_content = """ syntax = "proto3"; message MyMessage { int32 id = 1; string name = 2; repeated int32 values = 3; } """ # For simplicity, write to a temporary file proto_path = "my_message.proto" with open(proto_path, "w") as f: f.write(proto_file_content) # Build a database from .proto files database = pbtools.build_database([proto_path]) # Get the message type from the database MyMessage = database.MyMessage # Create a message instance message = MyMessage(id=1, name="hello", values=[10, 20, 30]) print(f"Original message: {message}") # Encode to binary encoded = message.encode() print(f"Encoded bytes: {encoded}") # Decode from binary decoded = MyMessage.decode(encoded) print(f"Decoded message: {decoded}") # Access fields print(f"Decoded ID: {decoded.id}") print(f"Decoded Name: {decoded.name}") print(f"Decoded Values: {decoded.values}") # Clean up the temporary file os.remove(proto_path)
Debug
Known issues
gotcha`pbtools` explicitly avoids generating Python code, which is a common workflow for `protoc`. Users accustomed to `protoc`-generated classes might find this approach different. `pbtools` parses `.proto` files at runtime.
fix
Understand that `pbtools` dynamically works with message definitions by parsing `.proto` files at runtime, offering a different paradigm than compiled protobuf code. It's designed for direct data manipulation.
affects: All versions
gotchaAlthough `pbtools` does not generate code, it still relies on the core `protobuf` library for its underlying mechanisms. Ensure the `protobuf` package is installed and compatible with your `pbtools` version (e.g., `pbtools` 0.47.0 requires `protobuf>=4.0.0`).
fix
Usually handled automatically by `pip install pbtools`. If issues arise, check `pip check` and ensure there are no conflicting `protobuf` installations or manual downgrades of `protobuf`.
affects: All versions
gotchaWhen accessing fields on a decoded message, if a field is not present in the binary data, `pbtools` will return its default value (e.g., `0` for integers, `''` for strings, empty list for repeated fields) rather than raising an `AttributeError`. This can mask missing data if not explicitly checked.
fix
Be aware of Protobuf's default value behavior. For optional fields or fields that might genuinely be missing, consider checking if the field was 'set' if such a feature becomes available or design your `.proto` schema accordingly.
affects: All versions
Upgrade
Version history
0.47.0latest on PyPI · released Jun 4, 2023
Audit
Dependencies
protobufrequiredRequired for parsing `.proto` files and handling Protocol Buffers message structures. `pbtools` builds upon the core `protobuf` library.
Agent activity
2 hits · last 30 days
node
2
Resources
pbtools — pip install pbtools · libregistry