8 """Similar to C++'s 'enum', but with a few extra toys. Takes a
9 string with spaces in between the different 'enum' names (keys).
10 If 'asInt' is true, then values will be integers (useful for array
11 indicies). Once created, the enum values can not be changed."""
13 def __init__(self, names, prefix = '', asInt = False, intOffset = 0):
14 biggest = smallest =
""
17 for count, name
in enumerate (names.split()) :
21 "You can not duplicate Enum Names '%s'" % name
23 key =
"%s_%s" % (prefix, name)
25 key = count + intOffset
26 object.__setattr__ (self, name, key)
28 self._keys.append (name)
32 """ Returns true if this value is a valid enum value"""
33 return self._valueDict.has_key (value)
37 """ Returns true if this value is a valid enum key"""
38 return self.__dict__.has_key (key)
42 """ Returns the key (if it exists) for a given enum value"""
43 return self._valueDict.get (value,
None)
47 """ Returns copy of valid keys """
54 """Lets me set internal values, but throws an error if any of
55 the enum values are changed"""
56 if not name.startswith (
"_"):
58 raise RuntimeError,
"You can not modify Enum values."
60 object.__setattr__ (self, name, value)
64 return self.__dict__.get (key,
None)
Note: Please do not use or modify any data or functions with a leading underscore.