ó V Qc@smdZddlZdd d„ƒYZdd d„ƒYZedd iƒZd„Zed krieƒndS( sCEnumeration metaclass. XXX This is very much a work in progress. iÿÿÿÿNt EnumMetaClasscBs)eZdZd„Zd„Zd„ZRS(shMetaclass for enumeration. To define your own enumeration, do something like class Color(Enum): red = 1 green = 2 blue = 3 Now, Color.red, Color.green and Color.blue behave totally different: they are enumerated values, not integers. Enumerations cannot be instantiated; however they can be subclassed. cCs“x)|D]!}|jtk rtd‚qqWtd„|ƒ}||_||_i|_x3|jƒD]%\}}t|||ƒ|j|)sN( t __class__Rt TypeErrortfiltert__name__t __bases__t_EnumMetaClass__dicttitemst EnumInstance(tselftnametbasestdicttbasetkeytvalue((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyt__init__s    cCs…|dkr|jjƒSy|j|SWnMtk rwx=|jD].}yt||ƒSWqBtk roqBqBXqBWnXt|‚dS(sÁReturn an enumeration value. For example, Color.red returns the value corresponding to red. XXX Perhaps the values should be created in the constructor? This looks in the class dictionary and if it is not found there asks the base classes. The special attribute __members__ returns the list of names defined in this class (it does not merge in the names defined in base classes). t __members__N(R tkeystKeyErrorRtgetattrtAttributeError(R R R((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyt __getattr__0s    cCs±|j}|jrB|dtjtd„|jƒdƒd}n|jr­g}x:|jjƒD])\}}|jd|t|ƒfƒqaWd|tj|dƒf}n|S(Nt(cSs|jS(N(R(R((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyRPss, t)s%s: %ss%s: {%s}( RRtstringtjointmapR R tappendtint(R tstlistRR((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyt__repr__Ms  0 !(Rt __module__t__doc__RRR#(((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyR s  R cBs;eZdZd„Zd„Zd„Zd„Zd„ZRS(s Class to represent an enumeration value. EnumInstance('Color', 'red', 12) prints as 'Color.red' and behaves like the integer 12 when compared, but doesn't support arithmetic. XXX Should it record the actual enumeration rather than just its name? cCs||_||_||_dS(N(t_EnumInstance__classnamet_EnumInstance__enumnamet_EnumInstance__value(R t classnametenumnameR((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyRes  cCs|jS(N(R((R ((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyt__int__jscCsd|j|j|jfS(NsEnumInstance(%r, %r, %r)(R&R'R((R ((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyR#ms cCsd|j|jfS(Ns%s.%s(R&R'(R ((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyt__str__rscCst|jt|ƒƒS(N(tcmpR(R (R tother((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyt__cmp__us(RR$R%RR+R#R,R/(((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyR Zs      RcCsõdtfd„ƒY}|jGHt|ƒGH|j|jkGH|j|jkGH|jdkGH|jdkGHd|fd„ƒY}|jGH|jGH|j|jkGHdtfd„ƒY}d ||fd „ƒY}|jGH|jGH|GH|GH|GH|GHdS( NtColorcBseZdZdZdZRS(iii(RR$tredtgreentblue(((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyR0€siit ExtendedColorcBs&eZdZdZdZdZdZRS(iiiii(RR$twhitetorangetyellowtpurpletblack(((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyR4s t OtherColorcBseZdZdZRS(ii(RR$R5R3(((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyR:™st MergedColorcBseZRS((RR$(((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyR;s(RR1tdirR3R6R5(R0R4R:R;((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyt_test~s& t__main__((((R%RRR RR=R(((s-/usr/lib64/python2.7/Demo/metaclasses/Enum.pyts Q! *