12 class EnumClass(object):
14 def __iter__(self):
return iter(constants)
15 def __len__(self):
return len(constants)
16 def __getitem__(self, i):
return constants[i]
17 def __repr__(self):
return 'Enum' + str(names)
18 def __str__(self):
return 'enum ' + str(constants)
20 class EnumValue(object):
21 __slots__ = (
'__value')
22 def __init__(self, value): self.__value = value
23 Value = property(
lambda self: self.__value)
24 EnumType = property(
lambda self: EnumType)
25 def __hash__(self):
return hash(self.__value)
26 def __cmp__(self, other):
29 assert self.EnumType
is other.EnumType,
"Only values from the same enum are comparable"
30 return cmp(self.__value, other.__value)
31 def __invert__(self):
return constants[maximum - self.__value]
32 def __nonzero__(self):
return bool(self.__value)
33 def __repr__(self):
return str(names[self.__value])
35 maximum = len(names) - 1
36 constants = [
None] * len(names)
37 for i, each
in enumerate(names):
39 setattr(EnumClass, each, val)
41 constants = tuple(constants)
42 EnumType = EnumClass()