A tiny Python library for creating Python dicts from protocol buffers and the reverse.
Project description
protobuf-to-dict
protobuf-to-dict is a small Python library for creating dicts from protocol buffers. It is intended to be used as an intermediate step before serialization (e.g. to JSON).
Installation
Note: This is a fork. Install by pointing to this github repo.
For example:
pip install git@github.com:yepcord/protobuf-to-dict.git
Example
Given the google.protobuf.message.Message
subclass MyMessage
:
>> > from protobuf3_to_dict import protobuf_to_dict, dict_to_protobuf
>> > my_message = MyMessage()
>> > # pb_my_message is a protobuf string
>> > my_message.ParseFromString(pb_my_message)
>> > my_message_dict = protobuf_to_dict(my_message)
>> > print(my_message_dict)
{'message': 'Hello'}
>> > msg = dict_to_protobuf(MyMessage, values=my_message_dict)
>> > assert msg == my_message
True
Datetime conversion
This package automatically converts Python's datetime objects to Google's Timestamp and vice-versa. If you want to manually do the conversion, the functions are:
from protobuf3_to_dict import datetime_to_timestamp, timestamp_to_datetime
timestamp = datetime_to_timestamp(sample_datetime)
result_sample_datetime = timestamp_to_datetime(timestamp)
assert sample_datetime == result_sample_datetime
Getting all fields, field names and options of a protobuf class
For example in the tests folder you can find sample.proto:
message Obj {
int32 id = 1 [(is_optional) = true];
string item_id = 2;
google.protobuf.Timestamp transacted_at = 3;
Status status = 5;
}
Then you can:
>> > from protobuf3_to_dict import get_field_names_and_options
>> > for field, field_name, options in get_field_names_and_options(sample_pb2.Obj):
...
print('name: {}, options: {}'.format(field_name, options))
name: id, options: {'is_optional': True}
name: item_id, options: {}
name: transacted_at, options: {}
name: status, options: {}
Validation for required fields
Protobuf 3 does not have a notion of required vs. optional fields. Everything is optional. However if you need some sort of validation before converting your dictionary to a protobuf object, first of all you need to add an option to your protobuf messages that is called is_optional
. Note that this is different than the optional
keyword in Prorobuf 2. This is an "option":
For example in the tests folder you can find sample.proto:
message Obj {
int32 id = 1 [(is_optional) = true];
string item_id = 2;
google.protobuf.Timestamp transacted_at = 3;
Status status = 5;
}
Then you can validate if a dictionary has all the fields you need:
>> > import datetime
>> > from protobuf3_to_dict import validate_dict_for_required_pb_fields
>> >
>> > dic = {'item_id': 2, 'transacted_at': datetime.datetime.now(), 'status': 0}
>> > from tests import sample_pb2
>> > validate_dict_for_required_pb_fields(pb=sample_pb2.Obj, dic=dic)
But if you have missing fields:
>>> dic = {'item_id': 2}
>>> validate_dict_for_required_pb_fields(pb=sample_pb2.Obj, dic=dic)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "protobuf-to-dict/protobuf_to_dict/convertor.py", line 274, in validate_dict_for_required_pb_fields
raise FieldsMissing('Missing fields: {}'.format(', '.join(missing_fields)))
protobuf_to_dict.convertor.FieldsMissing: Missing fields: transacted_at, status
Caveats
This library grew out of the desire to serialize a protobuf-encoded message to
JSON. As JSON has no built-in binary type (all strings in
JSON are Unicode strings), any field whose type is
FieldDescriptor.TYPE_BYTES
is, by default, converted to a base64-encoded
string.
If you want to override this behaviour, you may do so by passing
protobuf_to_dict
a dictionary of protobuf types to callables via the
type_callable_map
kwarg:
>> > from copy import copy
>> > from google.protobuf.descriptor import FieldDescriptor
>> > from protobuf3_to_dict import protobuf_to_dict, TYPE_CALLABLE_MAP
>> >
>> > type_callable_map = copy(TYPE_CALLABLE_MAP)
>> > # convert TYPE_BYTES to a Python bytestring
>> > type_callable_map[FieldDescriptor.TYPE_BYTES] = str
>> >
>> > # my_message is a google.protobuf.message.Message instance
>> > protobuf_to_dict(my_message, type_callable_map=type_callable_map)
By default, the integer representation is used for enum values. To use their
string labels instead, pass use_enum_labels=True
into protobuf_to_dict
:
>>> protobuf_to_dict(my_message, use_enum_labels=True)
And if you need the enum labels to be automatically converted to lowercase:
>>> protobuf_to_dict(my_message, use_enum_labels=True, lowercase_enum_lables=True)
When you convert from dictionary to protobuf, if you need the enums to work both
in lowercase and uppercase, set the strict=False
.
Testing
pytest tests/
To regenerate src/tests/sample_pb2.py
:
run the compile.sh
file inside the tests folder.
attention
Prorobuf 3.0.0 has supported json now. Check https://developers.google.com/protocol-buffers/docs/reference/python/ for more details.
Authors
protobuf-to-dict is written and maintained by Ben Hodgson, with significant contributions from Nino Walker, Jonathan Klaassen, and Tristram Gräbener. Sep Dehpour
(Un)license
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file yc_protobuf3_to_dict-0.3.0.tar.gz
.
File metadata
- Download URL: yc_protobuf3_to_dict-0.3.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/6.3.8-200.fc38.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03b75e182318afac94e33161005186868ea307203db1cc054f62a87a89d1a54c |
|
MD5 | 625af1f6be423d324e0d7da9aa43ed41 |
|
BLAKE2b-256 | c13a0d654101ffeef08c92958647569308054b701d970b6c6c15e63d720418ae |
File details
Details for the file yc_protobuf3_to_dict-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: yc_protobuf3_to_dict-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/6.3.8-200.fc38.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 629e912a229e26c82955d303fd584fcb2613ea92551355dea73f3db44b4eddc6 |
|
MD5 | 150e2aab354cb8e1e7a2c94daf7097e3 |
|
BLAKE2b-256 | f94c7f66c97a2beb4548a054825fa2653181ebbc99ca937e0d9ed7bd750a7185 |