def Enum(*args, **kwargs):
def getLabel(cls, val):<EOL><INDENT>"""<STR_LIT>"""<EOL>return cls.__labels[val]<EOL><DEDENT>def validate(cls, val):<EOL><INDENT>"""<STR_LIT>"""<EOL>return val in cls.__values<EOL><DEDENT>def getValues(cls):<EOL><INDENT>"""<STR_LIT>"""<EOL>return list(cls.__values)<EOL><DEDENT>def getLabels(cls):<EOL><INDENT>"""<STR_LIT>"""<EOL>return list(cls.__labels.values())<EOL><DEDENT>def getValue(cls, label):<EOL><INDENT>"""<STR_LIT>"""<EOL>return cls.__labels[label]<EOL><DEDENT>for arg in list(args)+kwargs.keys():<EOL><INDENT>if type(arg) is not str:<EOL><INDENT>raise TypeError("<STR_LIT>".format(arg))<EOL><DEDENT>if not __isidentifier(arg):<EOL><INDENT>raise ValueError("<STR_LIT>""<STR_LIT>".format(arg))<EOL><DEDENT><DEDENT>kwargs.update(zip(args, args))<EOL>newType = type("<STR_LIT>", (object,), kwargs)<EOL>newType.__labels = dict( (v,k) for k,v in kwargs.iteritems())<EOL>newType.__values = set(newType.__labels.keys())<EOL>newType.getLabel = functools.partial(getLabel, newType)<EOL>newType.validate = functools.partial(validate, newType)<EOL>newType.getValues = functools.partial(getValues, newType)<EOL>newType.getLabels = functools.partial(getLabels, newType)<EOL>newType.getValue = functools.partial(getValue, newType)<EOL>return newType<EOL>
Utility function for creating enumerations in python

Example Usage:
  >> Color = Enum("Red", "Green", "Blue", "Magenta")
  >> print Color.Red
  >> 0
  >> print Color.Green
  >> 1
  >> print Color.Blue
  >> 2
  >> print Color.Magenta
  >> 3
  >> Color.Violet
  >> 'violet'
  >> Color.getLabel(Color.Red)
  >> 'Red'
  >> Color.getLabel(2)
  >> 'Blue'