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


Python messages.MessageField方法代碼示例

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


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

示例1: testNestedMessageField

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testNestedMessageField(self):
    class Inner(messages.Message):
      count = messages.IntegerField(1)
      greet = messages.MessageField(Greeting, 2)

    class Outer(messages.Message):
      inner = messages.MessageField(Inner, 1)
      extra = messages.StringField(2)

    class Store(model.Model):
      outer = msgprop.MessageProperty(Outer,
                                      indexed_fields=['inner.greet.text'])
    greet = Greeting(text='abc', when=123)
    inner = Inner(count=42, greet=greet)
    outer = Outer(inner=inner)
    st = Store(outer=outer)
    st.put()
    res = Store.query(Store.outer.inner.greet.text == 'abc').fetch()
    self.assertEqual(res, [st]) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:21,代碼來源:msgprop_test.py

示例2: testRepeatedNestedMessageField

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testRepeatedNestedMessageField(self):
    class Outer(messages.Message):
      greeting = messages.MessageField(Greeting, 1)
      extra = messages.IntegerField(2)

    class Store(model.Model):
      outers = msgprop.MessageProperty(Outer, repeated=True,
                                       indexed_fields=['greeting.text'])
    gr1 = Greeting(text='abc', when=123)
    gr2 = Greeting(text='def', when=456)
    outer1 = Outer(greeting=gr1, extra=1)
    outer2 = Outer(greeting=gr2, extra=2)
    store1 = Store(outers=[outer1])
    store1.put()
    store2 = Store(outers=[outer2])
    store2.put()
    store3 = Store(outers=[outer1, outer2])
    store3.put()
    res = Store.query(Store.outers.greeting.text == 'abc').fetch()
    self.assertEqual(res, [store1, store3]) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:22,代碼來源:msgprop_test.py

示例3: testNestedRepeatedMessageField

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testNestedRepeatedMessageField(self):
    class Outer(messages.Message):
      greetings = messages.MessageField(Greeting, 1, repeated=True)
      extra = messages.IntegerField(2)

    class Store(model.Model):
      outer = msgprop.MessageProperty(Outer, indexed_fields=['greetings.text',
                                                             'extra'])
    gr1 = Greeting(text='abc', when=123)
    gr2 = Greeting(text='def', when=456)
    outer1 = Outer(greetings=[gr1], extra=1)
    outer2 = Outer(greetings=[gr2], extra=2)
    outer3 = Outer(greetings=[gr1, gr2], extra=3)
    store1 = Store(outer=outer1)
    store1.put()
    store2 = Store(outer=outer2)
    store2.put()
    store3 = Store(outer=outer3)
    store3.put()
    res = Store.query(Store.outer.greetings.text == 'abc').fetch()
    self.assertEqual(res, [store1, store3]) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:23,代碼來源:msgprop_test.py

示例4: testNestedFieldErrors

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testNestedFieldErrors(self):
    class Outer(messages.Message):
      greetings = messages.MessageField(Greeting, 1, repeated=True)
      extra = messages.IntegerField(2)
    # Parent/child conflicts.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings.text', 'greetings'])
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings', 'greetings.text'])
    # Duplicate inner field.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings.text',
                                             'greetings.text'])
    # Can't index MessageField.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings'])
    # Can't specify subfields for non-MessageField.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['extra.foobar'])
    # Non-existent subfield.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings.foobar']) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:24,代碼來源:msgprop_test.py

示例5: testProjectionQueries

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testProjectionQueries(self):
    class Wrapper(messages.Message):
      greet = messages.MessageField(Greeting, 1)

    class Storage(model.Model):
      wrap = msgprop.MessageProperty(Wrapper, indexed_fields=['greet.text',
                                                              'greet.when'])
    gr1 = Greeting(text='abc', when=123)
    wr1 = Wrapper(greet=gr1)
    st1 = Storage(wrap=wr1)
    st1.put()
    res1 = Storage.query().get(projection=['wrap.greet.text',
                                           Storage.wrap.greet.when])
    self.assertNotEqual(res1, st1)
    self.assertEqual(res1.wrap, st1.wrap)
    res2 = Storage.query().get(projection=['wrap.greet.text'])
    self.assertEqual(res2.wrap, Wrapper(greet=Greeting(text='abc'))) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:19,代碼來源:msgprop_test.py

示例6: testProjectionQueriesRepeatedField

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testProjectionQueriesRepeatedField(self):
    class Wrapper(messages.Message):
      greets = messages.MessageField(Greeting, 1, repeated=True)

    class Storage(model.Model):
      wrap = msgprop.MessageProperty(Wrapper, indexed_fields=['greets.text',
                                                              'greets.when'])
    gr1 = Greeting(text='abc', when=123)
    wr1 = Wrapper(greets=[gr1])
    st1 = Storage(wrap=wr1)
    st1.put()
    res1 = Storage.query().get(projection=['wrap.greets.text',
                                           Storage.wrap.greets.when])
    self.assertNotEqual(res1, st1)
    self.assertEqual(res1.wrap, st1.wrap)
    res2 = Storage.query().get(projection=['wrap.greets.text'])
    self.assertEqual(res2.wrap, Wrapper(greets=[Greeting(text='abc')])) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:19,代碼來源:msgprop_test.py

示例7: testDefineField_Message

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

    field_descriptor.name = 'a_field'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.MESSAGE
    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.MessageField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.MESSAGE, 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

示例8: testMessageField

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

    self.assertRaises(messages.FieldDefinitionError,
                      messages.MessageField,
                      messages.Message,
                      10)

    class MyMessage(messages.Message):
      pass

    field = messages.MessageField(MyMessage, 10)
    self.assertEquals(MyMessage, field.type) 
開發者ID:google,項目名稱:protorpc,代碼行數:19,代碼來源:messages_test.py

示例9: testMessageField_WrongType

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

      class AnotherMessage(messages.Message):

        a_field = messages.MessageField('AnEnum', 1)

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

示例10: testMessageFieldValueToMessageWrongType

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testMessageFieldValueToMessageWrongType(self):
    class MyMessage(messages.Message):
      pass

    class MyOtherMessage(messages.Message):
      pass

    class HasMessage(messages.Message):
      field = messages.MessageField(MyMessage, 1)

    instance = MyOtherMessage()

    self.assertRaisesWithRegexpMatch(
        messages.EncodeError,
        'Expected type MyMessage, got MyOtherMessage: <MyOtherMessage>',
        HasMessage.field.value_to_message, instance) 
開發者ID:google,項目名稱:protorpc,代碼行數:18,代碼來源:messages_test.py

示例11: testNestedMessageFieldIsNone

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testNestedMessageFieldIsNone(self):
    class Outer(messages.Message):
      greeting = messages.MessageField(Greeting, 1)

    class Store(model.Model):
      outer = msgprop.MessageProperty(Outer, indexed_fields=['greeting.text'])
    outer1 = Outer(greeting=None)
    store1 = Store(outer=outer1)
    store1.put()
    res = Store.query(Store.outer.greeting.text == 'abc').fetch()
    self.assertEqual(res, []) 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:13,代碼來源:msgprop_test.py

示例12: _projected_entity_to_message

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def _projected_entity_to_message(ent, message_type):
  """Recursive helper for _from_base_type() to convert an entity to a message.

  Args:
    ent: A Model instance.
    message_type: A Message subclass.

  Returns:
    An instance of message_type.
  """
  msg = message_type()
  analyzed = _analyze_indexed_fields(ent._projection)
  for name, sublist in analyzed.iteritems():
    prop = ent._properties[name]
    val = prop._get_value(ent)
    assert isinstance(prop, model.StructuredProperty) == bool(sublist)
    if sublist:
      field = message_type.field_by_name(name)
      assert isinstance(field, messages.MessageField)
      assert prop._repeated == field.repeated
      if prop._repeated:
        assert isinstance(val, list)
        val = [_projected_entity_to_message(v, field.type) for v in val]
      else:
        assert isinstance(val, prop._modelclass)
        val = _projected_entity_to_message(val, field.type)
    setattr(msg, name, val)
  return msg 
開發者ID:GoogleCloudPlatform,項目名稱:datastore-ndb-python,代碼行數:30,代碼來源:msgprop.py

示例13: testMessageFieldValidate

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testMessageFieldValidate(self):
    """Test validation on message field."""
    class MyMessage(messages.Message):
      pass

    class AnotherMessage(messages.Message):
      pass

    field = messages.MessageField(MyMessage, 10)
    field.validate(MyMessage())

    self.assertRaises(messages.ValidationError,
                      field.validate,
                      AnotherMessage()) 
開發者ID:google,項目名稱:protorpc,代碼行數:16,代碼來源:messages_test.py

示例14: testMessageFieldValueFromMessage

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testMessageFieldValueFromMessage(self):
    class MyMessage(messages.Message):
      pass

    class HasMessage(messages.Message):
      field = messages.MessageField(MyMessage, 1)

    instance = MyMessage()

    self.assertTrue(instance is HasMessage.field.value_from_message(instance)) 
開發者ID:google,項目名稱:protorpc,代碼行數:12,代碼來源:messages_test.py

示例15: testMessageFieldValueFromMessageWrongType

# 需要導入模塊: from protorpc import messages [as 別名]
# 或者: from protorpc.messages import MessageField [as 別名]
def testMessageFieldValueFromMessageWrongType(self):
    class MyMessage(messages.Message):
      pass

    class HasMessage(messages.Message):
      field = messages.MessageField(MyMessage, 1)

    self.assertRaisesWithRegexpMatch(
        messages.DecodeError,
        'Expected type MyMessage, got int: 10',
        HasMessage.field.value_from_message, 10) 
開發者ID:google,項目名稱:protorpc,代碼行數:13,代碼來源:messages_test.py


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