Python protorpc.messages.Enum() Examples
The following are 30
code examples of protorpc.messages.Enum().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
protorpc.messages
, or try the search function
.
Example #1
Source File: descriptor_test.py From protorpc with Apache License 2.0 | 6 votes |
def testEnums(self): """Test that enums are described.""" module = self.LoadModule('my.package', 'class Enum1(messages.Enum): pass\n' 'class Enum2(messages.Enum): pass\n') enum1 = descriptor.EnumDescriptor() enum1.name = 'Enum1' enum2 = descriptor.EnumDescriptor() enum2.name = 'Enum2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.enum_types = [enum1, enum2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
Example #2
Source File: messages_test.py From protorpc with Apache License 2.0 | 6 votes |
def testComparison(self): """Test comparing various enums to different types.""" class Enum1(messages.Enum): VAL1 = 1 VAL2 = 2 class Enum2(messages.Enum): VAL1 = 1 self.assertEquals(Enum1.VAL1, Enum1.VAL1) self.assertNotEquals(Enum1.VAL1, Enum1.VAL2) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertNotEquals(Enum1.VAL1, 'VAL1') self.assertNotEquals(Enum1.VAL1, 1) self.assertNotEquals(Enum1.VAL1, 2) self.assertNotEquals(Enum1.VAL1, None) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertTrue(Enum1.VAL1 < Enum1.VAL2) self.assertTrue(Enum1.VAL2 > Enum1.VAL1) self.assertNotEquals(1, Enum2.VAL1)
Example #3
Source File: messages_test.py From protorpc with Apache License 2.0 | 6 votes |
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
Example #4
Source File: messages_test.py From protorpc with Apache License 2.0 | 6 votes |
def testAllowNestedEnums(self): """Test allowing nested enums in a message definition.""" class Trade(messages.Message): class Duration(messages.Enum): GTC = 1 DAY = 2 class Currency(messages.Enum): USD = 1 GBP = 2 INR = 3 # Sorted by name order seems to be the only feasible option. self.assertEquals(['Currency', 'Duration'], Trade.__enums__) # Message definition will now be set on Enumerated objects. self.assertEquals(Trade, Trade.Duration.message_definition())
Example #5
Source File: definition_test.py From protorpc with Apache License 2.0 | 6 votes |
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))
Example #6
Source File: definition_test.py From protorpc with Apache License 2.0 | 6 votes |
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))
Example #7
Source File: msgprop.py From python-compat-runtime with Apache License 2.0 | 6 votes |
def __init__(self, enum_type, name=None, default=None, choices=None, **kwds): """Constructor. Args: enum_type: A subclass of protorpc.messages.Enum. name: Optional datastore name (defaults to the property name). Additional keywords arguments specify the same options as supported by IntegerProperty. """ self._enum_type = enum_type if default is not None: self._validate(default) if choices is not None: map(self._validate, choices) super(EnumProperty, self).__init__(name, default=default, choices=choices, **kwds)
Example #8
Source File: descriptor_test.py From protorpc with Apache License 2.0 | 6 votes |
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)
Example #9
Source File: descriptor_test.py From protorpc with Apache License 2.0 | 6 votes |
def testNestedEnum(self): class MessageWithEnum(messages.Message): class Mood(messages.Enum): GOOD = 1 BAD = 2 UGLY = 3 class Music(messages.Enum): CLASSIC = 1 JAZZ = 2 BLUES = 3 expected = descriptor.MessageDescriptor() expected.name = 'MessageWithEnum' expected.enum_types = [descriptor.describe_enum(MessageWithEnum.Mood), descriptor.describe_enum(MessageWithEnum.Music)] described = descriptor.describe_message(MessageWithEnum) described.check_initialized() self.assertEquals(expected, described)
Example #10
Source File: msgprop.py From datastore-ndb-python with Apache License 2.0 | 6 votes |
def __init__(self, enum_type, name=None, default=None, choices=None, **kwds): """Constructor. Args: enum_type: A subclass of protorpc.messages.Enum. name: Optional datastore name (defaults to the property name). Additional keywords arguments specify the same options as supported by IntegerProperty. """ self._enum_type = enum_type if default is not None: self._validate(default) if choices is not None: map(self._validate, choices) super(EnumProperty, self).__init__(name, default=default, choices=choices, **kwds)
Example #11
Source File: descriptor_test.py From protorpc with Apache License 2.0 | 5 votes |
def testEmptyEnum(self): class EmptyEnum(messages.Enum): pass expected = descriptor.EnumDescriptor() expected.name = 'EmptyEnum' described = descriptor.describe_enum(EmptyEnum) described.check_initialized() self.assertEquals(expected, described)
Example #12
Source File: descriptor_test.py From protorpc with Apache License 2.0 | 5 votes |
def testDescribe(self): class MyEnum(messages.Enum): MY_NAME = 10 expected = descriptor.EnumValueDescriptor() expected.name = 'MY_NAME' expected.number = 10 described = descriptor.describe_enum_value(MyEnum.MY_NAME) described.check_initialized() self.assertEquals(expected, described)
Example #13
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
def testFindEnum(self): """Test that Enums are found.""" class Color(messages.Enum): pass A = self.DefineMessage('a', 'A', {'Color': Color}) self.assertEquals( Color, messages.find_definition('Color', A, importer=self.Importer))
Example #14
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
def DefineMessage(self, module, name, children={}, add_to_module=True): """Define a new Message class in the context of a module. Used for easily describing complex Message hierarchy. Message is defined including all child definitions. Args: module: Fully qualified name of module to place Message class in. name: Name of Message to define within module. children: Define any level of nesting of children definitions. To define a message, map the name to another dictionary. The dictionary can itself contain additional definitions, and so on. To map to an Enum, define the Enum class separately and map it by name. add_to_module: If True, new Message class is added to module. If False, new Message is not added. """ # Make sure module exists. module_instance = self.DefineModule(module) # Recursively define all child messages. for attribute, value in children.items(): if isinstance(value, dict): children[attribute] = self.DefineMessage( module, attribute, value, False) # Override default __module__ variable. children['__module__'] = module # Instantiate and possibly add to module. message_class = type(name, (messages.Message,), dict(children)) if add_to_module: setattr(module_instance, name, message_class) return message_class
Example #15
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
def testPickle(self): """Testing pickling and unpickling of Message instances.""" global MyEnum global AnotherMessage global MyMessage class MyEnum(messages.Enum): val1 = 1 val2 = 2 class AnotherMessage(messages.Message): string = messages.StringField(1, repeated=True) class MyMessage(messages.Message): field1 = messages.IntegerField(1) field2 = messages.EnumField(MyEnum, 2) field3 = messages.MessageField(AnotherMessage, 3) message = MyMessage(field1=1, field2=MyEnum.val2, field3=AnotherMessage(string=['a', 'b', 'c'])) message.set_unrecognized_field('exists', 'value', messages.Variant.STRING) message.set_unrecognized_field('repeated', ['list', 0, ('test',)], messages.Variant.STRING) unpickled = pickle.loads(pickle.dumps(message)) self.assertEquals(message, unpickled) self.assertTrue(AnotherMessage.string is unpickled.field3.string.field) self.assertTrue('exists' in message.all_unrecognized_fields()) self.assertEquals(('value', messages.Variant.STRING), message.get_unrecognized_field_info('exists')) self.assertEquals((['list', 0, ('test',)], messages.Variant.STRING), message.get_unrecognized_field_info('repeated'))
Example #16
Source File: descriptor_test.py From protorpc with Apache License 2.0 | 5 votes |
def testEnumWithItems(self): class EnumWithItems(messages.Enum): A = 3 B = 1 C = 2 expected = descriptor.EnumDescriptor() expected.name = 'EnumWithItems' a = descriptor.EnumValueDescriptor() a.name = 'A' a.number = 3 b = descriptor.EnumValueDescriptor() b.name = 'B' b.number = 1 c = descriptor.EnumValueDescriptor() c.name = 'C' c.number = 2 expected.values = [b, c, a] described = descriptor.describe_enum(EnumWithItems) described.check_initialized() self.assertEquals(expected, described)
Example #17
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
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
Example #18
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
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)
Example #19
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
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')
Example #20
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
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)
Example #21
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
def ActionOnAllFieldClasses(self, action): """Test all field classes except Message and Enum. Message and Enum require separate tests. Args: action: Callable that takes the field class as a parameter. """ for field_class in (messages.IntegerField, messages.FloatField, messages.BooleanField, messages.BytesField, messages.StringField, ): action(field_class)
Example #22
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
def testPickle(self): """Testing pickling and unpickling of Enum instances.""" colors = list(Color) unpickled = pickle.loads(pickle.dumps(colors)) self.assertEquals(colors, unpickled) # Unpickling shouldn't create new enum instances. for i, color in enumerate(colors): self.assertTrue(color is unpickled[i])
Example #23
Source File: messages_test.py From protorpc with Apache License 2.0 | 5 votes |
def testDocstring(self): """Test that docstring is supported ok.""" class NotImportant(messages.Enum): """I have a docstring.""" VALUE1 = 1 self.assertEquals('I have a docstring.', NotImportant.__doc__)
Example #24
Source File: msgprop.py From python-compat-runtime with Apache License 2.0 | 5 votes |
def _validate(self, msg): """Validate an Enum value. Raises: TypeError if the value is not an instance of self._message_type. """ if not isinstance(msg, self._message_type): raise TypeError('Expected a %s instance for %s property', self._message_type.__name__, self._code_name or self._name)
Example #25
Source File: main.py From billing-export-python with Apache License 2.0 | 5 votes |
def EnumPropertyHandler(obj): """Serialize datetime objects.""" return obj.name if isinstance(obj, messages.Enum) else obj
Example #26
Source File: msgprop.py From datastore-ndb-python with Apache License 2.0 | 5 votes |
def _validate(self, value): """Validate an Enum value. Raises: TypeError if the value is not an instance of self._enum_type. """ if not isinstance(value, self._enum_type): raise TypeError('Expected a %s instance, got %r instead' % (self._enum_type.__name__, value))
Example #27
Source File: msgprop.py From datastore-ndb-python with Apache License 2.0 | 5 votes |
def _to_base_type(self, enum): """Convert an Enum value to a base type (integer) value.""" return enum.number
Example #28
Source File: msgprop.py From datastore-ndb-python with Apache License 2.0 | 5 votes |
def _from_base_type(self, val): """Convert a base type (integer) value to an Enum value.""" return self._enum_type(val)
Example #29
Source File: msgprop.py From datastore-ndb-python with Apache License 2.0 | 5 votes |
def _validate(self, msg): """Validate an Enum value. Raises: TypeError if the value is not an instance of self._message_type. """ if not isinstance(msg, self._message_type): raise TypeError('Expected a %s instance for %s property', self._message_type.__name__, self._code_name or self._name)
Example #30
Source File: msgprop.py From python-compat-runtime with Apache License 2.0 | 5 votes |
def _validate(self, value): """Validate an Enum value. Raises: TypeError if the value is not an instance of self._enum_type. """ if not isinstance(value, self._enum_type): raise TypeError('Expected a %s instance, got %r instead' % (self._enum_type.__name__, value))