Proto Plus for Python
A Python library that provides a beautiful, Pythonic interface for working with protocol buffers. Current version: 1.27.2. Maintained with regular updates.
Warnings
- breaking Proto Plus 1.27.0 introduced changes to the 'Field' class constructor, requiring explicit 'type' and 'number' arguments.
- gotcha When defining nested messages, ensure that the nested class is defined before the parent class to avoid 'NameError'.
Install
-
pip install proto-plus
Imports
- proto
import proto
- proto.Message
from proto import Message
Quickstart
import proto
class Composer(proto.Message):
given_name = proto.Field(proto.STRING, number=1)
family_name = proto.Field(proto.STRING, number=2)
class Song(proto.Message):
composer = proto.Field(Composer, number=1)
title = proto.Field(proto.STRING, number=2)
lyrics = proto.Field(proto.STRING, number=3)
year = proto.Field(proto.INT32, number=4)
song = Song(
composer={'given_name': 'Johann', 'family_name': 'Pachelbel'},
title='Canon in D',
year=1680,
)
print(song.composer.family_name) # Output: Pachelbel
print(song.title) # Output: Canon in D