Registry / serialization / protobuf3-to-dict

protobuf3-to-dict

JSON →
library0.1.5pypypi✓ verified 24d ago

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.

pip install protobuf3-to-dict
INSTALL
IMPORT
SIG · PROTOBUF3-TO-DICT
P
protobuf3-to-dict
serializationpythonv0.1.5
Install
2.8s avg
Import
21ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.5 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.022s · 21MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.8s · import 0.020s · 22MB
19MB installed
● package 19MB
Code
Verified usage

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

protobuf_to_dict
from protobuf_to_dict import protobuf_to_dict
dict_to_protobuf
from protobuf_to_dict import dict_to_protobuf

This quickstart demonstrates converting a `google.protobuf.Timestamp` message to a Python dictionary and back. For custom Protobuf messages, you would first need to compile your `.proto` files into Python classes using the `protoc` compiler.

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!")
Debug
Known issues
gotchaThe library is installed as `protobuf3-to-dict` but imported as `protobuf_to_dict`. This is a common source of 'ModuleNotFoundError'.
fix
Ensure your import statements use `from protobuf_to_dict import ...` after running `pip install protobuf3-to-dict`.
affects: All versions
breakingThis 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.
fix
Ensure your project is running on Python 3.6 or later.
affects: All versions
gotchaThe 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.
fix
Adhere to the specified `protobuf==3.19.4` dependency. If you require a different `protobuf` version, carefully test for compatibility or consider alternative conversion libraries.
affects: All versions
gotchaUnlike `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.
fix
Be aware of the naming convention when comparing outputs or migrating from other Protobuf-to-dict conversion methods. If `camelCase` is required, manual post-processing of the dictionary will be necessary.
affects: All versions
gotchaTo 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.
fix
Install `protoc` (e.g., via your OS package manager or by downloading from GitHub) and run `protoc --python_out=. your_message.proto` to generate `your_message_pb2.py`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'protobuf3_to_dict'
The `protobuf3-to-dict` library is not installed, or the import statement uses an incorrect module name or capitalization.
fix
Ensure the library is correctly installed using `pip install protobuf3-to-dict`. The package name on PyPI is `protobuf3-to-dict`, but the import statement uses underscores: `from protobuf3_to_dict import protobuf_to_dict, dict_to_protobuf`.
AttributeError: Assignment not allowed to field "field_name" in the protocol message object.
This error occurs when attempting to directly assign a Python dictionary to a Protocol Buffer message field that expects a nested Protobuf message object, or a `google.protobuf.Any` field, instead of a primitive type or a properly converted Protobuf object.
fix
Use the library's `dict_to_protobuf` function to convert the dictionary into a Protobuf message instance before assigning it, or populate the Protobuf message field by field. For a `google.protobuf.Any` field, you would typically pack a message into it after creating the message from a dictionary.
NameError: name 'long' is not defined
This error arises when running Python 2 code or an older version of `protobuf-to-dict` that used the `long` type in a Python 3 environment, where `long` was removed and all integers are handled by the `int` type.
fix
Ensure you are using the `protobuf3-to-dict` library (version 0.1.5 or newer) which is designed for Python 3. If you encounter this with generated protobuf code, regenerate the `.py` files using a Python 3 compatible `protoc` and ensure your environment uses Python 3.
Upgrade
Version history
0.1.5latest on PyPI · released Feb 23, 2017
Audit
Dependencies
protobufrequiredRequired for Protobuf message definitions and operations.
Agent activity
13 hits · last 30 days
node
10
Resources
protobuf3-to-dict — pip install protobuf3-to-dict · libregistry