Since the version 5, Java includes an enum type in its language. It provides a way to perceive the enumerated types through an object-oriented approach.
In Java, each enumeration is a particular class and each item of this enumeration is an instance of this class. So, the value of an item can represent an integer, but also a point in a space, a operation, etc. Moreover it is possible to extend an enumeration by using the inheritance.
This recipe proposes a Python solution to implement the Java enum types, with the help of reflexion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | # enum.py - an object-oriented enumerated type
#
#
"""Object-oriented enumerated type
The creation of a new enumerated type (enum) is done by the creation of a class
which inherits from class 'enum' or one of its subclasses.
Items in enum are instances of the enum class. They are declared in the class
variable '__names__' and they are automatically associated to the variables
'__name__' and '__value__' which are respectively the name of the item and its
value. Each item is constant, in the sense that it cannot be directly modified.
You use specialized the enum to do so. Moreover each item is associated to a
name which belongs to the class namespace.
'__names__' may be associated to a list, a tuple or a dict. In the case of a
list or a tuple, each element must be a string representing the name of the
item. The value of this items corresponds then to its name. In the case of
'__names__' is a dict, the name of the item corresponds to the key in the
dict and its value to the value in the dict. Each name must be a string.
Each enum class has the following methods:
- names(): returns all the item names,
- values(): returns the list of item values,
- has_name(name): check the existence of an item name,
- get(itemName): take a string representing the item name and
return the item,
Moreover, the enum type accepts certain read-only operation like len() or iter(). And it can be used in for loop.
Examples of use:
>>> from enum import enum
>>> # simple exemple
>>> class Count(enum):
... __names__ = ['ONE', 'TWO', 'THREE']
>>> counts = [count.__value__ for count in Count]
>>> counts.sort()
>>> print counts
['ONE', 'THREE', 'TWO']
>>> print Count.ONE.__name__
ONE
>>> print Count.ONE.__name__ == Count.ONE.__value__
True
>>> print Count.has_name('TWO')
True
>>> print Count.has_name('FOUR')
False
>>> # examples with ineritance
>>> class Op(enum):
... __names__ = {
... 'ADD': lambda x, y: x + y,
... 'SUB': lambda x, y: x - y,
... 'MUL': lambda x, y: x * y,
... 'DIV': lambda x, y: x / y,
... }
...
... def eval(self, x, y):
... return self.__value__(x, y)
>>> class TrueOp(Op):
... __names__ = {
... 'DIV': lambda x, y: float(x) / float(y),
... 'POW': lambda x, y: x ** y,
... }
>>> print Op.DIV.eval(2, 3)
0
>>> print TrueOp.DIV.eval(2, 3)
0.666666666667
>>> print TrueOp.DIV.eval(2, 3) == TrueOp.DIV.__value__(2, 3)
True
>>> print TrueOp.DIV.eval(2, 3) == TrueOp.['DIV'].eval(2, 3)
True
>>> print TrueOp.DIV.__name__
DIV
"""
__all__ = ['EnumType', 'enum']
class EnumType(type):
"""Enumerated base type.
"""
def __new__(mcls, name, bases, dct):
# set a default __names__ dictionary if there is none
decl = dct.setdefault('__names__', {})
return type.__new__(mcls, name, bases, dct)
def __init__(cls, name, bases, dct):
def _copy(cls, obj):
"""Copy the dictionnary of obj in an new instance of cls.
"""
tmp = object.__new__(cls, cls.__name__, cls.__bases__, cls.__dict__)
tmp.__dict__.update(obj.__dict__)
return tmp
type.__init__(name, bases, dct)
# the __readonly attribute indicates Python if the enum class can be
# modified (it is modifiable just during the initialisation)
type.__setattr__(cls, '__readonly', False)
decl = type.__getattribute__(cls, '__names__')
tmp_decl = {}
# get the attribute __names__ of the bases classes for inheritance
for base in bases:
if isinstance(base, EnumType):
base_decl = object.__getattribute__(base, '__names__')
for name, value in base_decl.iteritems():
tmp_decl[name] = _copy(cls, value)
# convert each __names__ item to an instance of the current class
if type(decl) is dict:
for name, value in decl.iteritems():
tmp_decl[name] = cls(value)
object.__setattr__(tmp_decl[name], '__name__', name)
elif type(decl) in [list, tuple]:
for name in decl:
tmp_decl[name] = cls(name)
object.__setattr__(tmp_decl[name], '__name__', name)
else:
raise AttributeError, 'bad __names__ declaration in %s' % cls
type.__setattr__(cls, '__names__', tmp_decl)
type.__setattr__(cls, '__readonly', True)
def __getattribute__(cls, name):
if not object.__getattribute__(cls, '__names__').has_key(name):
if not object.__getattribute__(cls, name):
raise AttributeError, "Unknow attribute: '%s'" % name
return object.__getattribute__(cls, name)
return object.__getattribute__(cls, '__names__')[name]
def __setattr__(cls, name, value):
decl = object.__getattribute__(cls, '__names__')
if decl.has_key(name):
raise AttributeError, "Read only instance: %s" % decl[name]
else:
raise AttributeError, "Read only class: %s" % cls
def has_name(cls, name):
return object.__getattribute__(cls, '__names__').has_key(name)
def names(cls):
return object.__getattribute__(cls, '__names__').values()
def get(cls, name):
return object.__getattribute__(cls, '__names__')[name]
def __len__(cls):
return len(object.__getattribute__(cls, '__names__'))
def __getitem__(cls, name):
return object.__getattribute__(cls, '__names__')[name]
def __iter__(cls):
return object.__getattribute__(cls, '__names__').itervalues()
def __contains__(cls, name):
return object.__getattribute__(cls, '__names__').has_key(name)
def __repr__(cls):
return "<EnumType '%s.%s'>" % (cls.__module__, cls.__name__)
class enum(object):
"""Enumeration base class.
"""
__metaclass__ = EnumType
def __init__(self, value):
object.__init__(self)
self.__value__ = value
def __repr__(self):
return "<%s.%s enum %s at %s>" % (self.__class__.__module__,
self.__class__.__name__,
self.__name__,
hex(id(self)))
def __setattr__(self, name, value):
if type.__getattribute__(self.__class__, '__readonly'):
raise AttributeError, "Read only instance: %s" % self
else:
object.__setattr__(self, name, value)
# ---- some more tests ---------------------------
def _test():
class Op(enum):
"""Some operators including integer division.
"""
__names__ = {
'ADD': lambda x, y: x + y,
'SUB': lambda x, y: x - y,
'MUL': lambda x, y: x * y,
'DIV': lambda x, y: x / y,
}
def eval(self, x, y):
return self.__value__(x, y)
def __str__(self):
return self.__name__
class TrueOp(Op):
"""Some operators including true division and power.
"""
__names__ = {
'DIV': lambda x, y: float(x) / float(y),
'POW': lambda x, y: x ** y,
}
# print the result of each operation in Op with the value 2 and 3
# DIV must return 0
for op in Op:
print '%s %s %s = %s' % (2, op, 3, op.eval(2, 3))
print ''
# print the result of each operation in TrueOp with the value 2 and 3
# DIV must return a float
for op in TrueOp:
print '%s %s %s = %s' % (2, op, 3, op.eval(2, 3))
print ''
print "len(Op) = %s" % len(Op) # must print 4
print "len(TrueOp) = %s" % len(TrueOp) # must print 5
print "POW in Op = %s" % ('POW' in Op) # must print False
print "POW in TrueOp = %s" % ('POW' in TrueOp) # must print True
print ''
import math
class SomePoint(enum):
"""An example with an inner class."""
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def norm(self):
return math.sqrt(self.x**2 + self.y**2)
__names__ = {
'A': Point(0, 0),
'B': Point(1, 2),
'C': Point(3, 2)
}
def getX(self):
return self.__value__.x
def getY(self):
return self.__value__.y
def norm(self):
return self.__value__.norm()
for point in SomePoint:
print "(%s,%s), norm = %s" % (point.getX(), point.getY(), point.norm())
if __name__ == '__main__':
import doctest
doctest.testmod()
_test()
# End
|
More information on Java enums can be found at http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html