1. Atsushi Odagiri
  2. standardenum

Source

standardenum / src / standardenum / __init__.py

#
# -*- coding:utf-8 -*-
"""
>>> class SampleEnum(StandardEnum):
...     A = 1
...     B = 2
...     C = 3
...
>>> SampleEnum.A
<SampleEnum.A value=1>
>>> for e in SampleEnum:
...     print e
...
<SampleEnum.A value=1>
<SampleEnum.B value=2>
<SampleEnum.C value=3>
>>> SampleEnum.A == SampleEnum.A
True
>>> SampleEnum.A == SampleEnum.B
False
"""


class EnumType(type):

    def __init__(cls, name, bases, dct):
        super(EnumType, cls).__init__(name, bases, dct)
        cls._values = []

        for key, value in dct.iteritems():
            if not key.startswith('_'):
                v = cls(key, value)
                setattr(cls, key, v)
                cls._values.append(v)

    def __iter__(cls):
        return iter(cls._values)


class StandardEnum(object):

    __metaclass__ = EnumType

    def __init__(self, k, v):
        self.v = v
        self.k = k

    def __repr__(self):
        return "<%s.%s value=%s>" % (self.__class__.__name__, self.k, self.v)

    def __str__(self):
        return str(self.v)

    def __int__(self):
        return int(self.v)

    def __long__(self):
        return long(self.v)

    def __float__(self):
        return float(self.v)

    def __complex__(self):
        return complex(self.v)



if __name__ == '__main__':
    import doctest
    doctest.testmod()