本文整理汇总了Python中protorpc.messages.Message方法的典型用法代码示例。如果您正苦于以下问题:Python messages.Message方法的具体用法?Python messages.Message怎么用?Python messages.Message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类protorpc.messages
的用法示例。
在下文中一共展示了messages.Message方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testErrors
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def testErrors(self):
class Storage(model.Model):
greet = msgprop.MessageProperty(Greeting, indexed_fields=['text'])
# Call MessageProperty(x) where x is not a Message class.
self.assertRaises(TypeError, msgprop.MessageProperty, Storage)
self.assertRaises(TypeError, msgprop.MessageProperty, 42)
self.assertRaises(TypeError, msgprop.MessageProperty, None)
# Call MessageProperty(Greeting, indexed_fields=x) where x
# includes invalid field names.
self.assertRaises(ValueError, msgprop.MessageProperty,
Greeting, indexed_fields=['text', 'nope'])
self.assertRaises(TypeError, msgprop.MessageProperty,
Greeting, indexed_fields=['text', 42])
self.assertRaises(TypeError, msgprop.MessageProperty,
Greeting, indexed_fields=['text', None])
self.assertRaises(ValueError, msgprop.MessageProperty,
Greeting, indexed_fields=['text', 'text']) # Duplicate.
# Set a MessageProperty value to a non-Message instance.
self.assertRaises(TypeError, Storage, greet=42)
示例2: testRepeatedNestedMessageField
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [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])
示例3: testNestedRepeatedMessageField
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [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])
示例4: testNestedFieldErrors
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [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'])
示例5: testProjectionQueries
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [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')))
示例6: testProjectionQueriesRepeatedField
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [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')]))
示例7: _message_to_entity
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def _message_to_entity(msg, modelclass):
"""Recursive helper for _to_base_type() to convert a message to an entity.
Args:
msg: A Message instance.
modelclass: A Model subclass.
Returns:
An instance of modelclass.
"""
ent = modelclass()
for prop_name, prop in modelclass._properties.iteritems():
if prop._code_name == 'blob_': # TODO: Devise a cleaner test.
continue # That's taken care of later.
value = getattr(msg, prop_name)
if value is not None and isinstance(prop, model.StructuredProperty):
if prop._repeated:
value = [_message_to_entity(v, prop._modelclass) for v in value]
else:
value = _message_to_entity(value, prop._modelclass)
setattr(ent, prop_name, value)
return ent
示例8: _from_base_type
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def _from_base_type(self, ent):
"""Convert a Model instance (entity) to a Message value."""
if ent._projection:
# Projection query result. Reconstitute the message from the fields.
return _projected_entity_to_message(ent, self._message_type)
blob = ent.blob_
if blob is not None:
protocol = self._protocol_impl
else:
# Perhaps it was written using a different protocol.
protocol = None
for name in _protocols_registry.names:
key = '__%s__' % name
if key in ent._values:
blob = ent._values[key]
if isinstance(blob, model._BaseValue):
blob = blob.b_val
protocol = _protocols_registry.lookup_by_name(name)
break
if blob is None or protocol is None:
return None # This will reveal the underlying dummy model.
msg = protocol.decode_message(self._message_type, blob)
return msg
示例9: testDefineMessageEnumOnly
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def testDefineMessageEnumOnly(self):
"""Test definition a message with only enums."""
class AMessage(messages.Message):
class NestedEnum(messages.Enum):
pass
message_descriptor = descriptor.describe_message(AMessage)
message_class = definition.define_message(message_descriptor, '__main__')
self.assertEquals('AMessage', message_class.__name__)
self.assertEquals('__main__', message_class.__module__)
self.assertEquals(message_descriptor,
descriptor.describe_message(message_class))
示例10: testDefineMessageFieldsOnly
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def testDefineMessageFieldsOnly(self):
"""Test definition a message with only fields."""
class AMessage(messages.Message):
field1 = messages.IntegerField(1)
field2 = messages.StringField(2)
message_descriptor = descriptor.describe_message(AMessage)
message_class = definition.define_message(message_descriptor, '__main__')
self.assertEquals('AMessage', message_class.__name__)
self.assertEquals('__main__', message_class.__module__)
self.assertEquals(message_descriptor,
descriptor.describe_message(message_class))
示例11: testDefineMessage
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def testDefineMessage(self):
"""Test defining Message class from descriptor."""
class AMessage(messages.Message):
class NestedEnum(messages.Enum):
pass
field1 = messages.IntegerField(1)
field2 = messages.StringField(2)
message_descriptor = descriptor.describe_message(AMessage)
message_class = definition.define_message(message_descriptor, '__main__')
self.assertEquals('AMessage', message_class.__name__)
self.assertEquals('__main__', message_class.__module__)
self.assertEquals(message_descriptor,
descriptor.describe_message(message_class))
示例12: document_to_message
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def document_to_message(document, message):
"""Builds a search document into an ProtoRPC message.
Args:
document: search.ScoredDocument, A document from a search result.
message: messages.Message, a ProtoRPC.messages message to build.
Returns:
A constructed ProtoRPC message.
"""
for field in document.fields:
if field.name == 'assignment_date':
setattr(
message, 'max_extend_date',
device_model.calculate_return_dates(field.value).max)
try:
setattr(message, field.name, field.value)
except messages.ValidationError:
if field.value == 'True':
setattr(message, field.name, True)
elif field.value == 'False':
setattr(message, field.name, False)
elif isinstance(field.value, float):
setattr(message, field.name, int(field.value))
except AttributeError:
if field.name == 'lat_long':
setattr(message, 'latitude', field.value.latitude)
setattr(message, 'longitude', field.value.longitude)
else:
logging.error('Unable to map %s to any attribute.', field.name)
return message
示例13: set_search_query_options
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def set_search_query_options(request):
"""Sets the search query options based on a ProtoRPC request message.
Args:
request: messages.Message, The message that contains the values of the
query options.
Returns:
A tuple containing the values of query options if they exist in the
message.
"""
try:
query = request.query_string
except AttributeError:
query = None
expressions = []
sort_options = None
try:
for message_expression in request.expressions:
direction = search.SortExpression.DESCENDING
if (message_expression.direction ==
shared_messages.SortDirection.ASCENDING):
direction = search.SortExpression.ASCENDING
expressions.append(
search.SortExpression(
expression=message_expression.expression, direction=direction))
if expressions:
sort_options = search.SortOptions(expressions=expressions)
except AttributeError:
# We do not want to do anything if the message does not have expressions
# since sort_options is already set to None above.
pass
try:
returned_fields = request.returned_fields
except AttributeError:
returned_fields = None
return query, sort_options, returned_fields
示例14: setUp
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [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)
示例15: testRepeatedIndexedField
# 需要导入模块: from protorpc import messages [as 别名]
# 或者: from protorpc.messages import Message [as 别名]
def testRepeatedIndexedField(self):
class AltGreeting(messages.Message):
lines = messages.StringField(1, repeated=True)
when = messages.IntegerField(2)
class Store(model.Model):
altg = msgprop.MessageProperty(AltGreeting, indexed_fields=['lines'])
s1 = Store(altg=AltGreeting(lines=['foo', 'bar'], when=123))
s1.put()
s2 = Store(altg=AltGreeting(lines=['baz', 'bletch'], when=456))
s2.put()
res = Store.query(Store.altg.lines == 'foo').fetch()
self.assertEqual(res, [s1])