當前位置: 首頁>>代碼示例>>Python>>正文


Python messages.EnumField方法代碼示例

本文整理匯總了Python中protorpc.messages.EnumField方法的典型用法代碼示例。如果您正苦於以下問題:Python messages.EnumField方法的具體用法?Python messages.EnumField怎麽用?Python messages.EnumField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在protorpc.messages的用法示例。


在下文中一共展示了messages.EnumField方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: testDefineField_Enum

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testDefineField_Enum(self):
    """Test defining an enum field."""
    field_descriptor = descriptor.FieldDescriptor()

    field_descriptor.name = 'a_field'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.ENUM
    field_descriptor.type_name = 'something.yet.to.be.Defined'
    field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED

    field = definition.define_field(field_descriptor)

    # Name will not be set from the original descriptor.
    self.assertFalse(hasattr(field, 'name'))

    self.assertTrue(isinstance(field, messages.EnumField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.ENUM, field.variant)
    self.assertFalse(field.required)
    self.assertTrue(field.repeated)
    self.assertRaisesWithRegexpMatch(messages.DefinitionNotFoundError,
                                     'Could not find definition for '
                                     'something.yet.to.be.Defined',
                                     getattr, field, 'type') 
開發者ID:google,項目名稱:protorpc,代碼行數:26,代碼來源:definition_test.py

示例2: testEnumField_WrongType

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testEnumField_WrongType(self):
    """Test that forward referencing the wrong type raises an error."""
    global AMessage
    try:
      class AMessage(messages.Message):
        pass

      class AnotherMessage(messages.Message):

        a_field = messages.EnumField('AMessage', 1)

      self.assertRaises(messages.FieldDefinitionError,
                        getattr,
                        AnotherMessage.field_by_name('a_field'),
                        'type')
    finally:
      del AMessage 
開發者ID:google,項目名稱:protorpc,代碼行數:19,代碼來源:messages_test.py

示例3: testDefault_EnumField

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testDefault_EnumField(self):
    class MyEnum(messages.Enum):

      VAL = 1

    module_name = test_util.get_module_name(MyEnum)
    field = messages.EnumField(MyEnum, 10, default=MyEnum.VAL)
    field.name = 'a_field'

    expected = descriptor.FieldDescriptor()
    expected.name = 'a_field'
    expected.number = 10
    expected.label = descriptor.FieldDescriptor.Label.OPTIONAL
    expected.variant = messages.EnumField.DEFAULT_VARIANT
    expected.type_name = '%s.MyEnum' % module_name
    expected.default_value = '1'

    described = descriptor.describe_field(field)
    self.assertEquals(expected, described) 
開發者ID:google,項目名稱:protorpc,代碼行數:21,代碼來源:descriptor_test.py

示例4: setUp

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def setUp(self):
    super(MsgPropTests, self).setUp()
    global Greeting

    class Greeting(messages.Message):
      text = messages.StringField(1, required=True)
      when = messages.IntegerField(2)
      color = messages.EnumField(Color, 3) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:10,代碼來源:msgprop_test.py

示例5: testDefaultFields_Enum

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testDefaultFields_Enum(self):
    """Test the default for enum fields."""
    class Symbol(messages.Enum):

      ALPHA = 1
      BETA = 2
      GAMMA = 3

    field = messages.EnumField(Symbol, 1, default=Symbol.ALPHA)

    self.assertEquals(Symbol.ALPHA, field.default) 
開發者ID:google,項目名稱:protorpc,代碼行數:13,代碼來源:messages_test.py

示例6: testDefaultFields_EnumStringDelayedResolution

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testDefaultFields_EnumStringDelayedResolution(self):
    """Test that enum fields resolve default strings."""
    field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label',
                               1,
                               default='OPTIONAL')

    self.assertEquals(descriptor.FieldDescriptor.Label.OPTIONAL, field.default) 
開發者ID:google,項目名稱:protorpc,代碼行數:9,代碼來源:messages_test.py

示例7: testDefaultFields_EnumIntDelayedResolution

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testDefaultFields_EnumIntDelayedResolution(self):
    """Test that enum fields resolve default integers."""
    field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label',
                               1,
                               default=2)

    self.assertEquals(descriptor.FieldDescriptor.Label.REQUIRED, field.default) 
開發者ID:google,項目名稱:protorpc,代碼行數:9,代碼來源:messages_test.py

示例8: testDefaultFields_EnumOkIfTypeKnown

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testDefaultFields_EnumOkIfTypeKnown(self):
    """Test that enum fields accept valid default values when type is known."""
    field = messages.EnumField(descriptor.FieldDescriptor.Label,
                               1,
                               default='REPEATED')

    self.assertEquals(descriptor.FieldDescriptor.Label.REPEATED, field.default) 
開發者ID:google,項目名稱:protorpc,代碼行數:9,代碼來源:messages_test.py

示例9: testDefaultFields_EnumInvalidDelayedResolution

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testDefaultFields_EnumInvalidDelayedResolution(self):
    """Test that enum fields raise errors upon delayed resolution error."""
    field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label',
                               1,
                               default=200)

    self.assertRaisesWithRegexpMatch(TypeError,
                                     'No such value for 200 in Enum Label',
                                     getattr,
                                     field,
                                     'default') 
開發者ID:google,項目名稱:protorpc,代碼行數:13,代碼來源:messages_test.py

示例10: testEnumField

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testEnumField(self):
    """Test the construction of enum fields."""
    self.assertRaises(messages.FieldDefinitionError,
                      messages.EnumField,
                      str,
                      10)

    self.assertRaises(messages.FieldDefinitionError,
                      messages.EnumField,
                      messages.Enum,
                      10)

    class Color(messages.Enum):
      RED = 1
      GREEN = 2
      BLUE = 3

    field = messages.EnumField(Color, 10)
    self.assertEquals(Color, field.type)

    class Another(messages.Enum):
      VALUE = 1

    self.assertRaises(messages.InvalidDefaultError,
                      messages.EnumField,
                      Color,
                      10,
                      default=Another.VALUE) 
開發者ID:google,項目名稱:protorpc,代碼行數:30,代碼來源:messages_test.py

示例11: testEnumField_ForwardReference

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testEnumField_ForwardReference(self):
    """Test the construction of forward reference enum fields."""
    global MyMessage
    global ForwardEnum
    global ForwardMessage
    try:
      class MyMessage(messages.Message):

        forward = messages.EnumField('ForwardEnum', 1)
        nested = messages.EnumField('ForwardMessage.NestedEnum', 2)
        inner = messages.EnumField('Inner', 3)

        class Inner(messages.Enum):
          pass

      class ForwardEnum(messages.Enum):
        pass

      class ForwardMessage(messages.Message):

        class NestedEnum(messages.Enum):
          pass

      self.assertEquals(ForwardEnum,
                        MyMessage.field_by_name('forward').type)

      self.assertEquals(ForwardMessage.NestedEnum,
                        MyMessage.field_by_name('nested').type)

      self.assertEquals(MyMessage.Inner,
                        MyMessage.field_by_name('inner').type)
    finally:
      try:
        del MyMessage
        del ForwardEnum
        del ForwardMessage
      except:
        pass 
開發者ID:google,項目名稱:protorpc,代碼行數:40,代碼來源:messages_test.py

示例12: _make_model_class

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def _make_model_class(message_type, indexed_fields, **props):
  """Construct a Model subclass corresponding to a Message subclass.

  Args:
    message_type: A Message subclass.
    indexed_fields: A list of dotted and undotted field names.
    **props: Additional properties with which to seed the class.

  Returns:
    A Model subclass whose properties correspond to those fields of
    message_type whose field name is listed in indexed_fields, plus
    the properties specified by the **props arguments.  For dotted
    field names, a StructuredProperty is generated using a Model
    subclass created by a recursive call.

  Raises:
    Whatever _analyze_indexed_fields() raises.
    ValueError if a field name conflicts with a name in **props.
    ValueError if a field name is not valid field of message_type.
    ValueError if an undotted field name designates a MessageField.
  """
  analyzed = _analyze_indexed_fields(indexed_fields)
  for field_name, sub_fields in analyzed.iteritems():
    if field_name in props:
      raise ValueError('field name %s is reserved' % field_name)
    try:
      field = message_type.field_by_name(field_name)
    except KeyError:
      raise ValueError('Message type %s has no field named %s' %
                       (message_type.__name__, field_name))
    if isinstance(field, messages.MessageField):
      if not sub_fields:
        raise ValueError(
            'MessageField %s cannot be indexed, only sub-fields' % field_name)
      sub_model_class = _make_model_class(field.type, sub_fields)
      prop = model.StructuredProperty(sub_model_class, field_name,
                                      repeated=field.repeated)
    else:
      if sub_fields is not None:
        raise ValueError(
            'Unstructured field %s cannot have indexed sub-fields' % field_name)
      if isinstance(field, messages.EnumField):
        prop = EnumProperty(field.type, field_name, repeated=field.repeated)
      elif isinstance(field, messages.BytesField):
        prop = model.BlobProperty(field_name,
                                  repeated=field.repeated, indexed=True)
      else:
        # IntegerField, FloatField, BooleanField, StringField.
        prop = model.GenericProperty(field_name, repeated=field.repeated)
    props[field_name] = prop
  return model.MetaModel('_%s__Model' % message_type.__name__,
                         (model.Model,), props) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:54,代碼來源:msgprop.py

示例13: _get_type_format

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def _get_type_format(field):
  """Returns the schema type and format for the given message type.

  Args:
    field: The protorpc.messages.Field to get schema type and format for.

  Returns:
    (type, format) for use in the "schemas" section of a discovery document.
  """
  if isinstance(field, messages.BooleanField):
    return ('boolean', None)

  if isinstance(field, messages.BytesField):
    return ('string', 'byte')

  if isinstance(field, message_types.DateTimeField):
    return ('string', 'date-time')

  if isinstance(field, messages.EnumField):
    return ('string', None)

  if isinstance(field, messages.FloatField):
    if field.variant == messages.Variant.DOUBLE:
      return ('number', 'double')
    return ('number', 'float')

  if isinstance(field, messages.IntegerField):
    if field.variant in (messages.Variant.INT32, messages.Variant.SINT32):
      return ('integer', 'int32')

    if field.variant in (messages.Variant.INT64, messages.Variant.SINT64):
      # If the type requires int64 or uint64, specify string or JavaScript will
      # convert them to 32-bit.
      return ('string', 'int64')

    if field.variant == messages.Variant.UINT32:
      return ('integer', 'uint32')

    if field.variant == messages.Variant.UINT64:
      return ('string', 'uint64')

    # Despite the warning about JavaScript, Endpoints v2's discovery document
    # generator uses integer, int64 as the default here. Follow their choice.
    return ('integer', 'int64')

  if isinstance(field, messages.StringField):
    return ('string', None)

  return (None, None) 
開發者ID:luci,項目名稱:luci-py,代碼行數:51,代碼來源:discovery.py

示例14: testEquality

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import EnumField [as 別名]
def testEquality(self):
    """Test message class equality."""
    # Comparison against enums must work.
    class MyEnum(messages.Enum):
      val1 = 1
      val2 = 2

    # Comparisons against nested messages must work.
    class AnotherMessage(messages.Message):
      string = messages.StringField(1)

    class MyMessage(messages.Message):
      field1 = messages.IntegerField(1)
      field2 = messages.EnumField(MyEnum, 2)
      field3 = messages.MessageField(AnotherMessage, 3)

    message1 = MyMessage()

    self.assertNotEquals('hi', message1)
    self.assertNotEquals(AnotherMessage(), message1)
    self.assertEquals(message1, message1)

    message2 = MyMessage()

    self.assertEquals(message1, message2)

    message1.field1 = 10
    self.assertNotEquals(message1, message2)

    message2.field1 = 20
    self.assertNotEquals(message1, message2)

    message2.field1 = 10
    self.assertEquals(message1, message2)

    message1.field2 = MyEnum.val1
    self.assertNotEquals(message1, message2)

    message2.field2 = MyEnum.val2
    self.assertNotEquals(message1, message2)

    message2.field2 = MyEnum.val1
    self.assertEquals(message1, message2)

    message1.field3 = AnotherMessage()
    message1.field3.string = u'value1'
    self.assertNotEquals(message1, message2)

    message2.field3 = AnotherMessage()
    message2.field3.string = u'value2'
    self.assertNotEquals(message1, message2)

    message2.field3.string = u'value1'
    self.assertEquals(message1, message2) 
開發者ID:google,項目名稱:protorpc,代碼行數:56,代碼來源:messages_test.py


注:本文中的protorpc.messages.EnumField方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。