]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """Enumeration metaclass."""\r |
2 | \r | |
3 | class EnumMetaclass(type):\r | |
4 | """Metaclass for enumeration.\r | |
5 | \r | |
6 | To define your own enumeration, do something like\r | |
7 | \r | |
8 | class Color(Enum):\r | |
9 | red = 1\r | |
10 | green = 2\r | |
11 | blue = 3\r | |
12 | \r | |
13 | Now, Color.red, Color.green and Color.blue behave totally\r | |
14 | different: they are enumerated values, not integers.\r | |
15 | \r | |
16 | Enumerations cannot be instantiated; however they can be\r | |
17 | subclassed.\r | |
18 | """\r | |
19 | \r | |
20 | def __init__(cls, name, bases, dict):\r | |
21 | super(EnumMetaclass, cls).__init__(name, bases, dict)\r | |
22 | cls._members = []\r | |
23 | for attr in dict.keys():\r | |
24 | if not (attr.startswith('__') and attr.endswith('__')):\r | |
25 | enumval = EnumInstance(name, attr, dict[attr])\r | |
26 | setattr(cls, attr, enumval)\r | |
27 | cls._members.append(attr)\r | |
28 | \r | |
29 | def __getattr__(cls, name):\r | |
30 | if name == "__members__":\r | |
31 | return cls._members\r | |
32 | raise AttributeError, name\r | |
33 | \r | |
34 | def __repr__(cls):\r | |
35 | s1 = s2 = ""\r | |
36 | enumbases = [base.__name__ for base in cls.__bases__\r | |
37 | if isinstance(base, EnumMetaclass) and not base is Enum]\r | |
38 | if enumbases:\r | |
39 | s1 = "(%s)" % ", ".join(enumbases)\r | |
40 | enumvalues = ["%s: %d" % (val, getattr(cls, val))\r | |
41 | for val in cls._members]\r | |
42 | if enumvalues:\r | |
43 | s2 = ": {%s}" % ", ".join(enumvalues)\r | |
44 | return "%s%s%s" % (cls.__name__, s1, s2)\r | |
45 | \r | |
46 | class FullEnumMetaclass(EnumMetaclass):\r | |
47 | """Metaclass for full enumerations.\r | |
48 | \r | |
49 | A full enumeration displays all the values defined in base classes.\r | |
50 | """\r | |
51 | \r | |
52 | def __init__(cls, name, bases, dict):\r | |
53 | super(FullEnumMetaclass, cls).__init__(name, bases, dict)\r | |
54 | for obj in cls.__mro__:\r | |
55 | if isinstance(obj, EnumMetaclass):\r | |
56 | for attr in obj._members:\r | |
57 | # XXX inefficient\r | |
58 | if not attr in cls._members:\r | |
59 | cls._members.append(attr)\r | |
60 | \r | |
61 | class EnumInstance(int):\r | |
62 | """Class to represent an enumeration value.\r | |
63 | \r | |
64 | EnumInstance('Color', 'red', 12) prints as 'Color.red' and behaves\r | |
65 | like the integer 12 when compared, but doesn't support arithmetic.\r | |
66 | \r | |
67 | XXX Should it record the actual enumeration rather than just its\r | |
68 | name?\r | |
69 | """\r | |
70 | \r | |
71 | def __new__(cls, classname, enumname, value):\r | |
72 | return int.__new__(cls, value)\r | |
73 | \r | |
74 | def __init__(self, classname, enumname, value):\r | |
75 | self.__classname = classname\r | |
76 | self.__enumname = enumname\r | |
77 | \r | |
78 | def __repr__(self):\r | |
79 | return "EnumInstance(%s, %s, %d)" % (self.__classname, self.__enumname,\r | |
80 | self)\r | |
81 | \r | |
82 | def __str__(self):\r | |
83 | return "%s.%s" % (self.__classname, self.__enumname)\r | |
84 | \r | |
85 | class Enum:\r | |
86 | __metaclass__ = EnumMetaclass\r | |
87 | \r | |
88 | class FullEnum:\r | |
89 | __metaclass__ = FullEnumMetaclass\r | |
90 | \r | |
91 | def _test():\r | |
92 | \r | |
93 | class Color(Enum):\r | |
94 | red = 1\r | |
95 | green = 2\r | |
96 | blue = 3\r | |
97 | \r | |
98 | print Color.red\r | |
99 | \r | |
100 | print repr(Color.red)\r | |
101 | print Color.red == Color.red\r | |
102 | print Color.red == Color.blue\r | |
103 | print Color.red == 1\r | |
104 | print Color.red == 2\r | |
105 | \r | |
106 | class ExtendedColor(Color):\r | |
107 | white = 0\r | |
108 | orange = 4\r | |
109 | yellow = 5\r | |
110 | purple = 6\r | |
111 | black = 7\r | |
112 | \r | |
113 | print ExtendedColor.orange\r | |
114 | print ExtendedColor.red\r | |
115 | \r | |
116 | print Color.red == ExtendedColor.red\r | |
117 | \r | |
118 | class OtherColor(Enum):\r | |
119 | white = 4\r | |
120 | blue = 5\r | |
121 | \r | |
122 | class MergedColor(Color, OtherColor):\r | |
123 | pass\r | |
124 | \r | |
125 | print MergedColor.red\r | |
126 | print MergedColor.white\r | |
127 | \r | |
128 | print Color\r | |
129 | print ExtendedColor\r | |
130 | print OtherColor\r | |
131 | print MergedColor\r | |
132 | \r | |
133 | def _test2():\r | |
134 | \r | |
135 | class Color(FullEnum):\r | |
136 | red = 1\r | |
137 | green = 2\r | |
138 | blue = 3\r | |
139 | \r | |
140 | print Color.red\r | |
141 | \r | |
142 | print repr(Color.red)\r | |
143 | print Color.red == Color.red\r | |
144 | print Color.red == Color.blue\r | |
145 | print Color.red == 1\r | |
146 | print Color.red == 2\r | |
147 | \r | |
148 | class ExtendedColor(Color):\r | |
149 | white = 0\r | |
150 | orange = 4\r | |
151 | yellow = 5\r | |
152 | purple = 6\r | |
153 | black = 7\r | |
154 | \r | |
155 | print ExtendedColor.orange\r | |
156 | print ExtendedColor.red\r | |
157 | \r | |
158 | print Color.red == ExtendedColor.red\r | |
159 | \r | |
160 | class OtherColor(FullEnum):\r | |
161 | white = 4\r | |
162 | blue = 5\r | |
163 | \r | |
164 | class MergedColor(Color, OtherColor):\r | |
165 | pass\r | |
166 | \r | |
167 | print MergedColor.red\r | |
168 | print MergedColor.white\r | |
169 | \r | |
170 | print Color\r | |
171 | print ExtendedColor\r | |
172 | print OtherColor\r | |
173 | print MergedColor\r | |
174 | \r | |
175 | if __name__ == '__main__':\r | |
176 | _test()\r | |
177 | _test2()\r |