File size: 3,414 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
Type Marshaling
===============

Proto Plus provides a service that converts between protocol buffer objects
and native Python types (or the wrapper types provided by this library).

This allows native Python objects to be used in place of protocol buffer
messages where appropriate. In all cases, we return the native type, and are
liberal on what we accept.

Well-known types
----------------

The following types are currently handled by Proto Plus:

=================================== ======================= ========
Protocol buffer type                Python type             Nullable
=================================== ======================= ========
``google.protobuf.BoolValue``       ``bool``                     Yes
``google.protobuf.BytesValue``      ``bytes``                    Yes
``google.protobuf.DoubleValue``     ``float``                    Yes
``google.protobuf.Duration``        ``datetime.timedelta````google.protobuf.FloatValue``      ``float``                    Yes
``google.protobuf.Int32Value``      ``int``                      Yes
``google.protobuf.Int64Value``      ``int``                      Yes
``google.protobuf.ListValue``       ``MutableSequence``          Yes
``google.protobuf.StringValue``     ``str``                      Yes
``google.protobuf.Struct``          ``MutableMapping``           Yes
``google.protobuf.Timestamp``       ``datetime.datetime``        Yes
``google.protobuf.UInt32Value``     ``int``                      Yes
``google.protobuf.UInt64Value``     ``int``                      Yes
``google.protobuf.Value``           JSON-encodable values        Yes
=================================== ======================= ========

.. note::

    Protocol buffers include well-known types for ``Timestamp`` and
    ``Duration``, both of which have nanosecond precision. However, the
    Python ``datetime`` and ``timedelta`` objects have only microsecond
    precision. This library converts timestamps to an implementation of
    ``datetime.datetime``, DatetimeWithNanoseconds, that includes nanosecond
    precision.

    If you *write* a timestamp field using a Python ``datetime`` value,
    any existing nanosecond precision will be overwritten.

.. note::

   Setting a ``bytes`` field from a string value will first base64 decode the string.
   This is necessary to preserve the original protobuf semantics when converting between
   Python dicts and proto messages.
   Converting a message containing a bytes field to a dict will
   base64 encode the bytes field and yield a value of type str.

.. code-block:: python

  import proto
  from google.protobuf.json_format import ParseDict

  class MyMessage(proto.Message):
      data = proto.Field(proto.BYTES, number=1)

  msg = MyMessage(data=b"this is a message")
  msg_dict = MyMessage.to_dict(msg)

  # Note: the value is the base64 encoded string of the bytes field.
  # It has a type of str, NOT bytes.
  assert type(msg_dict['data']) == str

  msg_pb = ParseDict(msg_dict, MyMessage.pb())
  msg_two = MyMessage(msg_dict)

  assert msg == msg_pb == msg_two
      
  
Wrapper types
-------------

Additionally, every :class:`~.Message` subclass is a wrapper class. The
creation of the class also creates the underlying protocol buffer class, and
this is registered to the marshal.

The underlying protocol buffer message class is accessible with the
:meth:`~.Message.pb` class method.