PK™ŽM5ðÏŒ¥ƒƒenum.py# -*- encoding: utf-8 -*- # enum.py # Part of enum, a package providing enumerated types for Python. # # Copyright © 2006 Ben Finney # This is free software; you may copy, modify and/or distribute this work # under the terms of the GNU General Public License, version 2 or later # or, at your option, the terms of the Python license. """Robust enumerated type support in Python This package provides a module for robust enumerations in Python. An enumeration object is created with a sequence of string arguments to the Enum() constructor:: >>> from enum import Enum >>> Colours = Enum('red', 'blue', 'green') >>> Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') The return value is an immutable sequence object with a value for each of the string arguments. Each value is also available as an attribute named from the corresponding string argument:: >>> pizza_night = Weekdays[4] >>> shirt_colour = Colours.green The values are constants that can be compared only with values from the same enumeration; comparison with other values will invoke Python's fallback comparisons:: >>> pizza_night == Weekdays.fri True >>> shirt_colour > Colours.red True >>> shirt_colour == "green" False Each value from an enumeration exports its sequence index as an integer, and can be coerced to a simple string matching the original arguments used to create the enumeration:: >>> str(pizza_night) 'fri' >>> shirt_colour.index 2 """ __author_name__ = "Ben Finney" __author_email__ = "ben+python@benfinney.id.au" __author__ = "%s <%s>" % (__author_name__, __author_email__) __date__ = "2006-10-13" __copyright__ = "Copyright © %s %s" % ( __date__.split('-')[0], __author_name__ ) __license__ = "Choice of GPL or Python license" __url__ = "http://cheeseshop.python.org/pypi/enum/" __version__ = "0.4.2" class EnumException(Exception): """ Base class for all exceptions in this module """ def __init__(self): if self.__class__ is EnumException: raise NotImplementedError, \ "%s is an abstract class for subclassing" % self.__class__ class EnumEmptyError(AssertionError, EnumException): """ Raised when attempting to create an empty enumeration """ def __str__(self): return "Enumerations cannot be empty" class EnumBadKeyError(TypeError, EnumException): """ Raised when creating an Enum with non-string keys """ def __init__(self, key): self.key = key def __str__(self): return "Enumeration keys must be strings: %s" % (self.key,) class EnumImmutableError(TypeError, EnumException): """ Raised when attempting to modify an Enum """ def __init__(self, *args): self.args = args def __str__(self): return "Enumeration does not allow modification" class EnumValue(object): """ A specific value of an enumerated type """ def __init__(self, enumtype, index, key): """ Set up a new instance """ self.__enumtype = enumtype self.__index = index self.__key = key def __get_enumtype(self): return self.__enumtype enumtype = property(__get_enumtype) def __get_key(self): return self.__key key = property(__get_key) def __str__(self): return "%s" % (self.key) def __get_index(self): return self.__index index = property(__get_index) def __repr__(self): return "EnumValue(%s, %s, %s)" % ( repr(self.__enumtype), repr(self.__index), repr(self.__key), ) def __hash__(self): return hash(self.__index) def __cmp__(self, other): result = NotImplemented self_type = self.enumtype try: assert self_type == other.enumtype result = cmp(self.index, other.index) except (AssertionError, AttributeError): result = NotImplemented return result class Enum(object): """ Enumerated type """ def __init__(self, *keys, **kwargs): """ Create an enumeration instance """ value_type = kwargs.get('value_type', EnumValue) if not keys: raise EnumEmptyError() keys = tuple(keys) values = [None] * len(keys) for i, key in enumerate(keys): value = value_type(self, i, key) values[i] = value try: super(Enum, self).__setattr__(key, value) except TypeError, e: raise EnumBadKeyError(key) super(Enum, self).__setattr__('_keys', keys) super(Enum, self).__setattr__('_values', values) def __setattr__(self, name, value): raise EnumImmutableError(name) def __delattr__(self, name): raise EnumImmutableError(name) def __len__(self): return len(self._values) def __getitem__(self, index): return self._values[index] def __setitem__(self, index, value): raise EnumImmutableError(index) def __delitem__(self, index): raise EnumImmutableError(index) def __iter__(self): return iter(self._values) def __contains__(self, value): is_member = False if isinstance(value, basestring): is_member = (value in self._keys) else: try: is_member = (value in self._values) except EnumValueCompareError, e: is_member = False return is_member PKºM5ÌsÇ&&enum.pyc;ò ÒE/Ec@sèdZdZdZdeefZdZdeidƒdefZdZd Zd Z d e fd „ƒYZ d e e fd„ƒYZ dee fd„ƒYZdee fd„ƒYZdefd„ƒYZdefd„ƒYZdS(sˆRobust enumerated type support in Python This package provides a module for robust enumerations in Python. An enumeration object is created with a sequence of string arguments to the Enum() constructor:: >>> from enum import Enum >>> Colours = Enum('red', 'blue', 'green') >>> Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') The return value is an immutable sequence object with a value for each of the string arguments. Each value is also available as an attribute named from the corresponding string argument:: >>> pizza_night = Weekdays[4] >>> shirt_colour = Colours.green The values are constants that can be compared only with values from the same enumeration; comparison with other values will invoke Python's fallback comparisons:: >>> pizza_night == Weekdays.fri True >>> shirt_colour > Colours.red True >>> shirt_colour == "green" False Each value from an enumeration exports its sequence index as an integer, and can be coerced to a simple string matching the original arguments used to create the enumeration:: >>> str(pizza_night) 'fri' >>> shirt_colour.index 2 s Ben Finneysben+python@benfinney.id.aus%s <%s>s 2006-10-13sCopyright © %s %ss-isChoice of GPL or Python licenses'http://cheeseshop.python.org/pypi/enum/s0.4.2s EnumExceptioncBstZdZd„ZRS(s. Base class for all exceptions in this module cCs(|itjotd|i‚ndS(Ns'%s is an abstract class for subclassing(sselfs __class__s EnumExceptionsNotImplementedError(sself((s"build/bdist.linux-i686/egg/enum.pys__init__@s(s__name__s __module__s__doc__s__init__(((s"build/bdist.linux-i686/egg/enum.pys EnumException>s sEnumEmptyErrorcBstZdZd„ZRS(s7 Raised when attempting to create an empty enumeration cCsdSdS(NsEnumerations cannot be empty((sself((s"build/bdist.linux-i686/egg/enum.pys__str__Hs(s__name__s __module__s__doc__s__str__(((s"build/bdist.linux-i686/egg/enum.pysEnumEmptyErrorEs sEnumBadKeyErrorcBs tZdZd„Zd„ZRS(s3 Raised when creating an Enum with non-string keys cCs ||_dS(N(skeysself(sselfskey((s"build/bdist.linux-i686/egg/enum.pys__init__NscCsd|ifSdS(Ns$Enumeration keys must be strings: %s(sselfskey(sself((s"build/bdist.linux-i686/egg/enum.pys__str__Qs(s__name__s __module__s__doc__s__init__s__str__(((s"build/bdist.linux-i686/egg/enum.pysEnumBadKeyErrorKs  sEnumImmutableErrorcBs tZdZd„Zd„ZRS(s* Raised when attempting to modify an Enum cGs ||_dS(N(sargssself(sselfsargs((s"build/bdist.linux-i686/egg/enum.pys__init__WscCsdSdS(Ns'Enumeration does not allow modification((sself((s"build/bdist.linux-i686/egg/enum.pys__str__Zs(s__name__s __module__s__doc__s__init__s__str__(((s"build/bdist.linux-i686/egg/enum.pysEnumImmutableErrorTs  s EnumValuecBsztZdZd„Zd„ZeeƒZd„ZeeƒZd„Z d„Z ee ƒZ d„Z d„Z d„ZRS( s( A specific value of an enumerated type cCs||_||_||_dS(s Set up a new instance N(senumtypesselfs_EnumValue__enumtypesindexs_EnumValue__indexskeys_EnumValue__key(sselfsenumtypesindexskey((s"build/bdist.linux-i686/egg/enum.pys__init__as  cCs |iSdS(N(sselfs_EnumValue__enumtype(sself((s"build/bdist.linux-i686/egg/enum.pys__get_enumtypegscCs |iSdS(N(sselfs_EnumValue__key(sself((s"build/bdist.linux-i686/egg/enum.pys __get_keykscCsd|iSdS(Ns%s(sselfskey(sself((s"build/bdist.linux-i686/egg/enum.pys__str__oscCs |iSdS(N(sselfs_EnumValue__index(sself((s"build/bdist.linux-i686/egg/enum.pys __get_indexrscCs0dt|iƒt|iƒt|iƒfSdS(NsEnumValue(%s, %s, %s)(sreprsselfs_EnumValue__enumtypes_EnumValue__indexs_EnumValue__key(sself((s"build/bdist.linux-i686/egg/enum.pys__repr__vscCst|iƒSdS(N(shashsselfs_EnumValue__index(sself((s"build/bdist.linux-i686/egg/enum.pys__hash__}scCsit}|i}y0||ijpt‚t|i|iƒ}Wntt fj o t}nX|SdS(N( sNotImplementedsresultsselfsenumtypes self_typesothersAssertionErrorscmpsindexsAttributeError(sselfsothers self_typesresult((s"build/bdist.linux-i686/egg/enum.pys__cmp__€s  (s__name__s __module__s__doc__s__init__s_EnumValue__get_enumtypespropertysenumtypes_EnumValue__get_keyskeys__str__s_EnumValue__get_indexsindexs__repr__s__hash__s__cmp__(((s"build/bdist.linux-i686/egg/enum.pys EnumValue^s           sEnumcBs_tZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z RS( s Enumerated type c Osö|idtƒ}| o tƒ‚nt|ƒ}tgt|ƒ}xwt |ƒD]i\}}||||ƒ}|||>> from enum import Enum >>> Colours = Enum('red', 'blue', 'green') >>> Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') The return value is an immutable sequence object with a value for each of the string arguments. Each value is also available as an attribute named from the corresponding string argument:: >>> pizza_night = Weekdays[4] >>> shirt_colour = Colours.green The values are constants that can be compared only with values from the same enumeration; comparison with other values will invoke Python's fallback comparisons:: >>> pizza_night == Weekdays.fri True >>> shirt_colour > Colours.red True >>> shirt_colour == "green" False Each value from an enumeration exports its sequence index as an integer, and can be coerced to a simple string matching the original arguments used to create the enumeration:: >>> str(pizza_night) 'fri' >>> shirt_colour.index 2 Keywords: enum enumerated enumeration Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: License :: OSI Approved :: GNU General Public License (GPL) Classifier: License :: OSI Approved :: Python Software Foundation License Classifier: Programming Language :: Python Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Operating System :: OS Independent Classifier: Intended Audience :: Developers PK¹M5?ÁN]ùùEGG-INFO/SOURCES.txtLICENSE.GPL LICENSE.PSF MANIFEST.in enum.py ez_setup.py setup.py enum.egg-info/PKG-INFO enum.egg-info/SOURCES.txt enum.egg-info/dependency_links.txt enum.egg-info/top_level.txt enum.egg-info/zip-safe test/__init__.py test/test_enum.py test/tools.py PK¹M5“×2EGG-INFO/dependency_links.txt PK¹M5_Ùµ.EGG-INFO/top_level.txtenum PKvM5“×2EGG-INFO/zip-safe PK™ŽM5ðÏŒ¥ƒƒ¤enum.pyPKºM5ÌsÇ&&¤¨enum.pycPK¹M5¾Þx¤â;EGG-INFO/PKG-INFOPK¹M5?ÁN]ùù¤DEGG-INFO/SOURCES.txtPK¹M5“×2¤BEEGG-INFO/dependency_links.txtPK¹M5_Ùµ.¤~EEGG-INFO/top_level.txtPKvM5“×2¤·EEGG-INFO/zip-safePKºçE