File size: 9,013 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
Messages
========

The fundamental building block in protocol buffers are `messages`_.
Messages are essentially permissive, strongly-typed structs (dictionaries),
which have zero or more fields that may themselves contain primitives or
other messages.

.. code-block:: protobuf

  syntax = "proto3";

  message Song {
    Composer composer = 1;
    string title = 2;
    string lyrics = 3;
    int32 year = 4;
  }

  message Composer {
    string given_name = 1;
    string family_name = 2;
  }

The most common use case for protocol buffers is to write a ``.proto`` file,
and then use the protocol buffer compiler to generate code for it.


Declaring messages
------------------

However, it is possible to declare messages directly.
This is the equivalent message declaration in Python, using this library:

.. code-block:: python

    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)

A few things to note:

* This library only handles proto3.
* The ``number`` is really a field ID. It is *not* a value of any kind.
* All fields are optional (as is always the case in proto3).
  The only general way to determine whether a field was explicitly set to its
  falsy value or not set all is to mark it ``optional``.
* Because all fields are optional, it is the responsibility of application logic
  to determine whether a necessary field has been set.
* You can optionally define a `__protobuf__` attribute in your module which will be used
  to differentiate messages which have the same name but exist in different modules.

.. code-block:: python

    # file a.py
    import proto

    __protobuf__ = proto.module(package="a")

    class A(proto.Message):
        name = proto.Field(proto.STRING, number=1)

    # file b.py
    import proto

    __protobuf__ = proto.module(package="b")

    class A(proto.Message):
        name = proto.Field(proto.STRING, number=1)

    # file main.py
    import a
    import b

    _a = a.A(name="Hello, A!")
    _b = b.A(name="Hello, B!")

.. _messages: https://developers.google.com/protocol-buffers/docs/proto3#simple

Messages are fundamentally made up of :doc:`fields`. Most messages are nothing
more than a name and their set of fields.


Usage
-----

Instantiate messages using either keyword arguments or a :class:`dict`
(and mix and matching is acceptable):

.. code-block:: python

    >>> song = Song(
    ...     composer={'given_name': 'Johann', 'family_name': 'Pachelbel'},
    ...     title='Canon in D',
    ...     year=1680,
    ... )
    >>> song.composer.family_name
    'Pachelbel'
    >>> song.title
    'Canon in D'


Assigning to Fields
-------------------

One of the goals of proto-plus is to make protobufs feel as much like regular python
objects as possible. It is possible to update a message's field by assigning to it,
just as if it were a regular python object.

.. code-block:: python

   song = Song()
   song.composer = Composer(given_name="Johann", family_name="Bach")

   # Can also assign from a dictionary as a convenience.
   song.composer = {"given_name": "Claude", "family_name": "Debussy"}

   # Repeated fields can also be assigned
   class Album(proto.Message):
       songs = proto.RepeatedField(Song, number=1)

   a = Album()
   songs = [Song(title="Canon in D"), Song(title="Little Fugue")]
   a.songs = songs

.. note::

   Assigning to a proto-plus message field works by making copies, not by updating references.
   This is necessary because of memory layout requirements of protocol buffers.
   These memory constraints are maintained by the protocol buffers runtime.
   This behavior can be surprising under certain circumstances, e.g. trying to save
   an alias to a nested field.

   :class:`proto.Message` defines a helper message, :meth:`~.Message.copy_from` to
   help make the distinction clear when reading code.
   The semantics of :meth:`~.Message.copy_from` are identical to the field assignment behavior described above.

   .. code-block:: python

      composer = Composer(given_name="Johann", family_name="Bach")
      song = Song(title="Tocatta and Fugue in D Minor", composer=composer)
      composer.given_name = "Wilhelm"

      # 'composer' is NOT a reference to song.composer
      assert song.composer.given_name == "Johann"

      # We CAN update the song's composer by assignment.
      song.composer = composer
      composer.given_name = "Carl"

      # 'composer' is STILL not a reference to song.composer.
      assert song.composer.given_name == "Wilhelm"

      # It does work in reverse, though,
      # if we want a reference we can access then update.
      composer = song.composer
      composer.given_name = "Gottfried"

      assert song.composer.given_name == "Gottfried"

      # We can use 'copy_from' if we're concerned that the code
      # implies that assignment involves references.
      composer = Composer(given_name="Elisabeth", family_name="Bach")
      # We could also do Message.copy_from(song.composer, composer) instead.
      Composer.copy_from(song.composer, composer)

      assert song.composer.given_name == "Elisabeth"


Enums
-----

Enums are also supported:

.. code-block:: python

    import proto

    class Genre(proto.Enum):
        GENRE_UNSPECIFIED = 0
        CLASSICAL = 1
        JAZZ = 2
        ROCK = 3

    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)
        genre = proto.Field(Genre, number=5)

All enums **must** begin with a ``0`` value, which is always the default in
proto3 (and, as above, indistuiguishable from unset).

Enums utilize Python :class:`enum.IntEnum` under the hood:

.. code-block:: python

    >>> song = Song(
    ...     composer={'given_name': 'Johann', 'family_name': 'Pachelbel'},
    ...     title='Canon in D',
    ...     year=1680,
    ...     genre=Genre.CLASSICAL,
    ... )
    >>> song.genre
    <Genre.CLASSICAL: 1>
    >>> song.genre.name
    'CLASSICAL'
    >>> song.genre.value
    1

Additionally, it is possible to provide strings or plain integers:

.. code-block:: python

    >>> song.genre = 2
    >>> song.genre
    <Genre.JAZZ: 2>
    >>> song.genre = 'CLASSICAL'
    <Genre.CLASSICAL: 1>

Serialization
-------------

Serialization and deserialization is available through the
:meth:`~.Message.serialize` and :meth:`~.Message.deserialize` class methods.

The :meth:`~.Message.serialize` method is available on the message *classes*
only, and accepts an instance:

.. code-block:: python

    serialized_song = Song.serialize(song)

The :meth:`~.Message.deserialize` method accepts a :class:`bytes`, and
returns an instance of the message:

.. code-block:: python

    song = Song.deserialize(serialized_song)

JSON serialization and deserialization are also available from message *classes*
via the :meth:`~.Message.to_json` and :meth:`~.Message.from_json` methods.

.. code-block:: python

    json = Song.to_json(song)

    new_song = Song.from_json(json)

Similarly, messages can be converted into dictionaries via the
:meth:`~.Message.to_dict` helper method.
There is no :meth:`~.Message.from_dict` method because the Message constructor
already allows construction from mapping types.

.. code-block:: python

   song_dict = Song.to_dict(song)

   new_song = Song(song_dict)

.. note::

   Although Python's pickling protocol has known issues when used with
   untrusted collaborators, some frameworks do use it for communication
   between trusted hosts.  To support such frameworks, protobuf messages
   **can** be pickled and unpickled, although the preferred mechanism for
   serializing proto messages is :meth:`~.Message.serialize`.

   Multiprocessing example:

   .. code-block:: python

       import proto
       from multiprocessing import Pool

       class Composer(proto.Message):
          name = proto.Field(proto.STRING, number=1)
          genre = proto.Field(proto.STRING, number=2)

       composers = [Composer(name=n) for n in ["Bach", "Mozart", "Brahms", "Strauss"]]

       with multiprocessing.Pool(2) as p:
          def add_genre(comp_bytes):
              composer = Composer.deserialize(comp_bytes)
              composer.genre = "classical"
              return Composer.serialize(composer)

       updated_composers = [
          Composer.deserialize(comp_bytes)
          for comp_bytes in p.map(add_genre, (Composer.serialize(comp) for comp in composers))
       ]