Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 1 | """Enumeration metaclass. |
| 2 | |
| 3 | XXX This is very much a work in progress. |
| 4 | |
| 5 | """ |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 6 | |
| 7 | import string |
| 8 | |
| 9 | class EnumMetaClass: |
| 10 | """Metaclass for enumeration. |
| 11 | |
| 12 | To define your own enumeration, do something like |
| 13 | |
| 14 | class Color(Enum): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 15 | red = 1 |
| 16 | green = 2 |
| 17 | blue = 3 |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 18 | |
| 19 | Now, Color.red, Color.green and Color.blue behave totally |
| 20 | different: they are enumerated values, not integers. |
| 21 | |
| 22 | Enumerations cannot be instantiated; however they can be |
| 23 | subclassed. |
| 24 | |
| 25 | """ |
| 26 | |
| 27 | def __init__(self, name, bases, dict): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 28 | """Constructor -- create an enumeration. |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 30 | Called at the end of the class statement. The arguments are |
| 31 | the name of the new class, a tuple containing the base |
| 32 | classes, and a dictionary containing everything that was |
| 33 | entered in the class' namespace during execution of the class |
| 34 | statement. In the above example, it would be {'red': 1, |
| 35 | 'green': 2, 'blue': 3}. |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 37 | """ |
| 38 | for base in bases: |
| 39 | if base.__class__ is not EnumMetaClass: |
| 40 | raise TypeError, "Enumeration base class must be enumeration" |
| 41 | bases = filter(lambda x: x is not Enum, bases) |
| 42 | self.__name__ = name |
| 43 | self.__bases__ = bases |
| 44 | self.__dict = {} |
| 45 | for key, value in dict.items(): |
| 46 | self.__dict[key] = EnumInstance(name, key, value) |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 47 | |
| 48 | def __getattr__(self, name): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 49 | """Return an enumeration value. |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 51 | For example, Color.red returns the value corresponding to red. |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 53 | XXX Perhaps the values should be created in the constructor? |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 55 | This looks in the class dictionary and if it is not found |
| 56 | there asks the base classes. |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 58 | The special attribute __members__ returns the list of names |
| 59 | defined in this class (it does not merge in the names defined |
| 60 | in base classes). |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 62 | """ |
| 63 | if name == '__members__': |
| 64 | return self.__dict.keys() |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 66 | try: |
| 67 | return self.__dict[name] |
| 68 | except KeyError: |
| 69 | for base in self.__bases__: |
| 70 | try: |
| 71 | return getattr(base, name) |
| 72 | except AttributeError: |
| 73 | continue |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 75 | raise AttributeError, name |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 76 | |
| 77 | def __repr__(self): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 78 | s = self.__name__ |
| 79 | if self.__bases__: |
| 80 | s = s + '(' + string.join(map(lambda x: x.__name__, |
| 81 | self.__bases__), ", ") + ')' |
| 82 | if self.__dict: |
| 83 | list = [] |
| 84 | for key, value in self.__dict.items(): |
| 85 | list.append("%s: %s" % (key, int(value))) |
| 86 | s = "%s: {%s}" % (s, string.join(list, ", ")) |
| 87 | return s |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | class EnumInstance: |
| 91 | """Class to represent an enumeration value. |
| 92 | |
| 93 | EnumInstance('Color', 'red', 12) prints as 'Color.red' and behaves |
| 94 | like the integer 12 when compared, but doesn't support arithmetic. |
| 95 | |
| 96 | XXX Should it record the actual enumeration rather than just its |
| 97 | name? |
| 98 | |
| 99 | """ |
| 100 | |
| 101 | def __init__(self, classname, enumname, value): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 102 | self.__classname = classname |
| 103 | self.__enumname = enumname |
| 104 | self.__value = value |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 105 | |
| 106 | def __int__(self): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 107 | return self.__value |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 108 | |
| 109 | def __repr__(self): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 110 | return "EnumInstance(%s, %s, %s)" % (`self.__classname`, |
| 111 | `self.__enumname`, |
| 112 | `self.__value`) |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 113 | |
| 114 | def __str__(self): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 115 | return "%s.%s" % (self.__classname, self.__enumname) |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 116 | |
| 117 | def __cmp__(self, other): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 118 | return cmp(self.__value, int(other)) |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | # Create the base class for enumerations. |
| 122 | # It is an empty enumeration. |
| 123 | Enum = EnumMetaClass("Enum", (), {}) |
| 124 | |
| 125 | |
| 126 | def _test(): |
| 127 | |
| 128 | class Color(Enum): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 129 | red = 1 |
| 130 | green = 2 |
| 131 | blue = 3 |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 132 | |
| 133 | print Color.red |
| 134 | print dir(Color) |
| 135 | |
| 136 | print Color.red == Color.red |
| 137 | print Color.red == Color.blue |
| 138 | print Color.red == 1 |
| 139 | print Color.red == 2 |
| 140 | |
| 141 | class ExtendedColor(Color): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 142 | white = 0 |
| 143 | orange = 4 |
| 144 | yellow = 5 |
| 145 | purple = 6 |
| 146 | black = 7 |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 147 | |
| 148 | print ExtendedColor.orange |
| 149 | print ExtendedColor.red |
| 150 | |
| 151 | print Color.red == ExtendedColor.red |
| 152 | |
| 153 | class OtherColor(Enum): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 154 | white = 4 |
| 155 | blue = 5 |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 156 | |
| 157 | class MergedColor(Color, OtherColor): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 158 | pass |
Guido van Rossum | bff110f | 1997-08-23 21:14:37 +0000 | [diff] [blame] | 159 | |
| 160 | print MergedColor.red |
| 161 | print MergedColor.white |
| 162 | |
| 163 | print Color |
| 164 | print ExtendedColor |
| 165 | print OtherColor |
| 166 | print MergedColor |
| 167 | |
| 168 | if __name__ == '__main__': |
| 169 | _test() |