The MessageField class is used to define messages for efficient transmission across network or process space. Messages are defined using field classes.
MessageField is provided by the protorpc.messages
module.
Constructor
The constructor of the MessageField class is defined as follows:
- class MessageField(message_type, number, [required=False | repeated=False])
-
Defines fields for sub-message values.
Arguments- message_type
- The message type for the field. Must be a subclass of Message.
- number
- Number of the field. Must be unique per message class.
- required=False
- Whether or not this field is required. Mutually exclusive with the
repeatedargument; do not specifyrepeated=Trueif you userequired=True. - repeated=False
- Whether or not this field is repeated. Mutually exclusive with the
requiredargument; do not specifyrequired=Trueif you userepeated=True.
Raises a FieldDefinitionError if the
message_typeis invalid.
Class Property
The MessageField class provides the following properties:
- type
- The Python type used for values of this field. For example, in the case
of DateTimeField,
typeisdatetime.datetime. For user defined MessageFields,typeis the specified Message type. - message_type
- The underlying message type used for serialization. Specifically this is
the type that you can store on an instance of a Message class. For example,
for DateTimeField,
the type will be
message_types.DateTimeMessage. For normal message fields it will be the protorpc message subclass. For example:class Sub(messages.Message): x = messages.IntegerField(1) class M(messages.Message): sub = messages.MessageField(Sub, 1) dt = message_types.DateTimeField(2) print 'M.sub.type =', M.sub.type print 'M.sub.message_type =', M.sub.message_type print 'M.dt.type =', M.dt.type print 'M.dt.message_type =', M.dt.message_type === output === M.sub.type = <class '__main__.Sub> M.sub.message_type = <class '__main__.Sub'> M.dt.type = <type 'datetime.datetime'> M.dt.message_type = <class 'protorpc.message_types.DateTimeMessage'>