Protobuf 3 to Python Dict Converter
A Python library designed to convert Protocol Buffer 3 messages into standard Python dictionaries and vice-versa. It is particularly useful as an intermediate step for serialization tasks, such as converting Protobuf messages to JSON. The library is a Python 3 adaptation of an earlier project and currently maintains version 0.1.5, with infrequent updates.
Warnings
- gotcha The library is installed as `protobuf3-to-dict` but imported as `protobuf_to_dict`. This is a common source of 'ModuleNotFoundError'.
- breaking This library explicitly targets Protobuf 3 and Python 3. Despite PyPI's `requires_python: None` metadata for some older versions, it is not compatible with Python 2.x environments.
- gotcha The library strictly pins its `protobuf` dependency to `protobuf==3.19.4`. Using a different version of the `protobuf` library in your environment may lead to unexpected behavior or `ImportError` issues.
- gotcha Unlike `google.protobuf.json_format.MessageToDict` which defaults to converting field names to `camelCase`, `protobuf3-to-dict` preserves the original Protobuf field names (e.g., `snake_case`) in the resulting Python dictionary.
- gotcha To convert your own custom Protobuf messages, you must first install the `protoc` compiler and use it to generate Python classes from your `.proto` files. This library works with the generated Python Protobuf classes.
Install
-
pip install protobuf3-to-dict
Imports
- protobuf_to_dict
from protobuf_to_dict import protobuf_to_dict
- dict_to_protobuf
from protobuf_to_dict import dict_to_protobuf
Quickstart
from google.protobuf.timestamp_pb2 import Timestamp
from protobuf_to_dict import protobuf_to_dict, dict_to_protobuf
import datetime
# Create a Protobuf Timestamp message
dt_obj = datetime.datetime.now(datetime.timezone.utc)
timestamp_msg = Timestamp()
timestamp_msg.FromDatetime(dt_obj)
print(f"Original Protobuf Timestamp: {timestamp_msg}")
# Convert Protobuf message to Python dictionary
data_dict = protobuf_to_dict(timestamp_msg)
print(f"Converted dictionary: {data_dict}")
# Convert dictionary back to Protobuf message
new_timestamp_msg = dict_to_protobuf(Timestamp, data_dict)
print(f"Converted back to Protobuf: {new_timestamp_msg}")
# Verify conversion
assert new_timestamp_msg.seconds == timestamp_msg.seconds
assert new_timestamp_msg.nanos == timestamp_msg.nanos
print("Conversion verified successfully!")