CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

code directory

Views: 5048
�
?o�Xc@sJdZddlZdddgZdddfZedejd �ZyeWne	k
rkd	�ZnXydd
l
mZWnek
r�e
ZnXyeWne	k
r�eZnXyeWne	k
r�eZnXdefd��YZd
�Zd�Zd�Zd�Zdefd��YZe
Zdefd��YZiZded<d�Zeed<[d�Zeed<[d�Zeed<[edkr�d�Z e ed<[ nd �Z!e!ed!<[!ed"kr�d#�Z"e"ed$<["nXd%�Z#e#ed&<[#d'�Z$e$ed(<[$d)�Z%e%ed*<[%d+�Z&e&ed,<[&d-�Z'e'ed.<['d/�Z(e(ed0<[(d1�Z)e)ed2<[)d3�Z*e*ed4<[*ed5��Z+e+ed6<[+ed7��Z,e,ed8<[,e-e
d9��Z.e.ed:<[.edefe�Z[de/efd;��YZ0d<�Z1d=�Z2dS(>sPython Enumerationsi����NtEnumtIntEnumtuniqueiis%s.%scCsx|D]}|rtSqWtS(N(tTruetFalse(titerabletelement((slib/enum/__init__.pytanys
(tOrderedDictt_RouteClassAttributeToGetattrcBs8eZdZdd�Zdd�Zd�Zd�ZRS(sjRoute attribute access on a class to __getattr__.

    This is a descriptor, used to define attributes that act differently when
    accessed through an instance and through a class.  Instance access remains
    normal, but access to an attribute through a class will be routed to the
    class's __getattr__ method; this is done by raising AttributeError.

    cCs
||_dS(N(tfget(tselfR
((slib/enum/__init__.pyt__init__/scCs%|dkrt��n|j|�S(N(tNonetAttributeErrorR
(Rtinstancet
ownerclass((slib/enum/__init__.pyt__get__2scCstd��dS(Nscan't set attribute(R(RRtvalue((slib/enum/__init__.pyt__set__7scCstd��dS(Nscan't delete attribute(R(RR((slib/enum/__init__.pyt
__delete__:sN(t__name__t
__module__t__doc__R
RRRR(((slib/enum/__init__.pyR	&s
	cCs+t|d�p*t|d�p*t|d�S(s5Returns True if obj is a descriptor, False otherwise.RRR(thasattr(tobj((slib/enum/__init__.pyt_is_descriptor>scCsZ|d |dkodknoY|dd!dkoY|dd!dkoYt|�dkS(s3Returns True if a __dunder__ name, False otherwise.ii����t__it_i����i(tlen(tname((slib/enum/__init__.pyt
_is_dunderFs$cCsZ|d|dkodknoY|dd!dkoY|dd!dkoYt|�dkS(s1Returns True if a _sunder_ name, False otherwise.ii����Riii����(R(R((slib/enum/__init__.pyt
_is_sunderNs$cCs"dd�}||_d|_dS(s"Make the given class un-picklable.cSstd|��dS(Ns%r cannot be pickled(t	TypeError(Rtprotocol((slib/enum/__init__.pyt_break_on_call_reduceXss	<unknown>N(R
t
__reduce_ex__R(tclsR#((slib/enum/__init__.pyt_make_class_unpicklableVs	t	_EnumDictcBs eZdZd�Zd�ZRS(s�Track enum member order and ensure member names are not reused.

    EnumMeta will use the names found in self._member_names as the
    enumeration member names.

    cCs tt|�j�g|_dS(N(tsuperR'Rt
_member_names(R((slib/enum/__init__.pyRescCs�tdkr|dkrdSt|�r7td��nst|�rFnd||jkrhtd|��nBt|�s�||kr�td||��n|jj|�ntt	|�j
||�dS(sChanges anything not dundered or not a descriptor.

        If a descriptor is added with the same name as an enum member, the name
        is removed from _member_names (this may leave a hole in the numerical
        sequence of values).

        If an enum member name is used twice, an error is raised; duplicate
        values are not checked for.

        Single underscore (sunder) names are reserved.

        Note:   in 3.x __order__ is simply discarded as a not necessary piece
                leftover from 2.x

        g@t	__order__Ns(_names_ are reserved for future Enum usesAttempted to reuse key: %rsKey already defined as: %r(tpyverR t
ValueErrorRR)R!RtappendR(R't__setitem__(RtkeyR((slib/enum/__init__.pyR.is(RRRRR.(((slib/enum/__init__.pyR'^s	tEnumMetacBs�eZdZed��Zd�Zddddd�Zd�Zd�Z	d�Z
ed��Zd	�Z
d
�Zd�Zd�Zd
�Zd�Zd�Zddddd�Zed��Zedkr�ed��Zned��ZRS(sMetaclass for EnumcCst�S(N(R'(tmetaclsR%tbases((slib/enum/__init__.pyt__prepare__�scs�t��tkrK�}t��x'|j�D]\}}|�|<q.Wn|j|�\�}|j��|�\}}	}
t�fd��jD��}x�jD]
}�|=q�W�jd�}
|
dkr_t	dkrSy8gt
|j�dd��D]\}}|^q�}
Wq\tk
rOgt
|j��D]}|^q:}
q\Xq��j}
n]�d=t	dkr�|
j
dd�j�}
g|D]}||
kr�|^q�}|
|7}
nt|�tdg�@}|r�td	d
j|�f��ntg|D]}|jD]}|^qq�}tt|�j|||��}g|_tdk	rnt�|_n	i|_�|_i|_|dkr�|j}nx~|
D]v}||}t|t�s�|f}n|}�tkr�|f}n|
s�|r)||�}t|d�sY||_qYn0|||�}t|d�sY�|�|_n|j}||_||_ |j!|�xI|jj�D](\}}|j"|jkr�|}Pq�q�W|jj#|�||kr�t$|||�n||j|<y||j|<Wq�tk
rq�Xq�Wt%}d�kr{�t&k	r{d}t'�fd�|D��sxt(|�t)}qxq{nx�d D]|}t*||�}t*�|d�}t*||d�}|�kr�||k	r�|dkr�|r�q�nt$|||�q�q�Wt	dkr<t+|t,�r�t$|dt*t,d��q�nKt	dkr�t+|t,�r�x*d!D]}t$||t*t,|��q^Wq�nt-dk	r�|	r�t$|d|jd�nt$|dt-jd�n|S("Nc3s|]}|�|fVqdS(N((t.0tk(t	classdict(slib/enum/__init__.pys	<genexpr>�sR*g@R/cSs|dS(Ni((titem((slib/enum/__init__.pyt<lambda>�st,t tmrosInvalid enum member name(s): %ss, t_value_R$t__getnewargs_ex__t__getnewargs__t
__reduce__c3s|]}|�jkVqdS(N(t__dict__(R4tm(tmember_type(slib/enum/__init__.pys	<genexpr>st__repr__t__str__t
__format__g������@t__cmp__t__le__t__lt__t__gt__t__ge__t__eq__t__ne__t__hash__t__member_new__t__new__(R=R>R$R?(RCRDRER$(RGRHRIRJRKRLRM(.ttypetdictR'titemst_get_mixins_t
_find_new_R)tgetR
R+tsortedR!tkeystreplacetsplittsetR,tjoinR@R(R0ROt_member_names_Rt_member_map_t
_member_type_t_value2member_map_t
isinstancettupleRR<t_name_t__objclass__RRR-tsetattrRtobjectRR&Rtgetattrt
issubclasstintR(R1R%R2R6t
original_dictR5tvt
first_enumROtsave_newtuse_argstmembersRR*Rtaliasest
invalid_namestbtatbase_attributest
enum_classtmember_nametargstenum_membertcanonical_membertunpicklabletmethodstclass_methodt
obj_methodtenum_methodtmethod((R6RBslib/enum/__init__.pyRO�s�	8
,%
/!				

			






#ic	Cs>|dkr|j||�S|j||d|d|d|�S(s�Either returns an existing member, or creates a new enum class.

        This method is used both when an enum class is given a value to match
        to an enumeration member (i.e. Color(3)) and for the functional API
        (i.e. Color = Enum('Color', names='red green blue')).

        When used for the functional API: `module`, if set, will be stored in
        the new class' __module__ attribute; `type`, if set, will be mixed in
        as the first base class.

        Note: if `module` is not set this routine will attempt to discover the
        calling module by walking the frame stack; if this is unsuccessful
        the resulting class will not be pickleable.

        tmoduleRPtstartN(R
ROt_create_(R%RtnamesRRPR�((slib/enum/__init__.pyt__call__AscCst||�o|j|jkS(N(R`RR](R%tmember((slib/enum/__init__.pyt__contains__VscCs?||jkr%td|j��ntt|�j|�dS(Ns%s: cannot delete Enum member.(R]RRR(R0t__delattr__(R%tattr((slib/enum/__init__.pyR�YscCsddddg|jS(Nt	__class__Rt__members__R(R\(R((slib/enum/__init__.pyt__dir__ascCs
|jj�S(s�Returns a mapping of member name->value.

        This mapping lists all enum members, including aliases. Note that this
        is a copy of the internal mapping.

        (R]tcopy(R%((slib/enum/__init__.pyR�escCsNt|�rt|��ny|j|SWntk
rIt|��nXdS(s5Return the enum member matching `name`

        We use __getattr__ instead of descriptors or inserting into the enum
        class' __dict__ in order to support `name` and `value` being both
        properties for enum members (which live in the class' __dict__) and
        enum members themselves.

        N(RRR]tKeyError(R%R((slib/enum/__init__.pyt__getattr__os	
cCs|j|S(N(R](R%R((slib/enum/__init__.pyt__getitem__scs�fd��jD�S(Nc3s|]}�j|VqdS(N(R](R4R(R%(slib/enum/__init__.pys	<genexpr>�s(R\(R%((R%slib/enum/__init__.pyt__iter__�scs�fd�t�j�D�S(Nc3s|]}�j|VqdS(N(R](R4R(R%(slib/enum/__init__.pys	<genexpr>�s(treversedR\(R%((R%slib/enum/__init__.pyt__reversed__�scCs
t|j�S(N(RR\(R%((slib/enum/__init__.pyt__len__�scCsd|jS(Ns	<enum %r>(R(R%((slib/enum/__init__.pyRC�scCsM|jjdi�}||kr0td��ntt|�j||�dS(s�Block attempts to reassign Enum members.

        A simple assignment to the class namespace only changes one of the
        several possible ways to get an Enum member from the Enum class,
        resulting in an inconsistent Enumeration.

        R]sCannot reassign members.N(R@RURR(R0t__setattr__(R%RRt
member_map((slib/enum/__init__.pyR��scCs$tdkrXt|t�rXy|jd�}WqUtk
rQtd|��qUXqXn|j}|d
kry|f}n||f}|j||�}g}	t|t	�r�|j
dd�j�}nt|tt
f�r!t|dt	�r!gt|�D]\}
}||
|f^q�}nd
}xT|D]L}t|t	�rW|||}
}n|\}
}|||
<|	j|
�q.Wt|t	�s�dj|	�|d<n|j||||�}|d
kr�ytjd�jd	}Wq�ttfk
r�q�Xn|d
krt|�n	||_|S(s}Convenience method to create a new Enum class.

        `names` can be:

        * A string containing member names, separated either with spaces or
          commas.  Values are auto-numbered from 1.
        * An iterable of member names.  Values are auto-numbered from 1.
        * An iterable of (member name, value) pairs.
        * A mapping of member name -> value.

        g@tasciis %r is not representable in ASCIIR9R:iR*iRN(R+R`tunicodetencodetUnicodeEncodeErrorR!R�R
R3t
basestringRXRYRatlistt	enumerateR-R[ROt_syst	_getframet	f_globalsRR,R&R(R%t
class_nameR�RRPR�R1R2R6R*titeR7Rutmember_valueRt((slib/enum/__init__.pyR��sF
	(2


	cCs|stdkrttfSd}}xA|D]9}|tk	r.t|t�r.|jr.td��q.q.Wt|t�s�td��nt|dt�s�|d}|d}nTxQ|djD]B}t|t�r�|dkr|}qq�|dkr�|}q�q�W||fS(s�Returns the type for creating enum members, and the first inherited
        enum class.

        bases: the tuple of bases that was given to __new__

        sCannot extend enumerationssHnew enumerations must be created as `ClassName([mixin_type,] enum_type)`ii����N(RR
ReRgR\R!t__mro__(R2RBRktbase((slib/enum/__init__.pyRS�s(


	


g@cCst|jdd�}|r%dttfStdd�}ttd�}tdkrX|}n
tjd}x�dD]�}x�||fD]�}y|j|}	Wn)ttfk
r�t||d�}	nX|	d|||gkr|dkr�|	|d<dt	tfSt
|	t�r|	j|�}	n|	}PqqW|dk	rlPqlqlWtj
}|tj
krat	}
nt}
|t	|
fS(sReturns the __new__ to be used for creating the enum members.

            classdict: the class dictionary given to __new__
            member_type: the data type whose __new__ will be used by default
            first_enum: enumeration to check for an overriding __new__

            RORNN(RNRO(RUR
RRfReRR@RR�RR`tstaticmethodRRO(R6RBRkROtN__new__tO__new__tE__new__R~tpossiblettargetRm((slib/enum/__init__.pyRTsB
	



		c	Cs�|jdd�}|dk	}|dkr�x~dD]j}xQ||fD]C}t||d�}|ddjtjtjfkrD|}PqDqDW|dk	r1Pq1q1Wtj}n|tjkr�t}nt}|||fS(sReturns the __new__ to be used for creating the enum members.

            classdict: the class dictionary given to __new__
            member_type: the data type whose __new__ will be used by default
            first_enum: enumeration to check for an overriding __new__

            RORNN(RNRO(RUR
RfROReRRR(	R6RBRkRORlR~R�R�Rm((slib/enum/__init__.pyRTAs(
	N(RRRtclassmethodR3ROR
R�R�R�R�tpropertyR�R�R�R�R�R�RCR�R�R�RSR+RT(((slib/enum/__init__.pyR0�s(	�			
							
=.:sNGeneric enumeration.

    Derive from this class to define new enumerations.

RcCs�t|�|kr|j}ny||jkr;|j|SWn>tk
r|x.|jj�D]}|j|kr\|Sq\WnXtd||jf��dS(Ns%s is not a valid %s(RPRR_R!R]tvaluesR,R(R%RR�((slib/enum/__init__.pyROys
ROcCsd|jj|j|jfS(Ns<%s.%s: %r>(R�RRbR<(R((slib/enum/__init__.pyRC�sRCcCsd|jj|jfS(Ns%s.%s(R�RRb(R((slib/enum/__init__.pyRD�sRDg@cCsbg|jj�D];}|jD]+}|ddkr||jkr|^qq}dddg|S(NiRR�RR(R�R;R@R](RR%RAtadded_behavior((slib/enum/__init__.pyR��s

.R�cCsF|jtkr$t}t|�}n|j}|j}|j||�S(N(R^RetstrRRE(Rtformat_specR%tval((slib/enum/__init__.pyRE�s		REg������@cCsUt|�|jkr/||kr(dSndSntStd|jj|jjf��(Nii����s unorderable types: %s() and %s()(RPR�tNotImplementedR!R(Rtother((slib/enum/__init__.pyRF�sRFcCs&td|jj|jjf��dS(Nsunorderable types: %s() <= %s()(R!R�R(RR�((slib/enum/__init__.pyRG�sRGcCs&td|jj|jjf��dS(Nsunorderable types: %s() < %s()(R!R�R(RR�((slib/enum/__init__.pyRH�sRHcCs&td|jj|jjf��dS(Nsunorderable types: %s() >= %s()(R!R�R(RR�((slib/enum/__init__.pyRJ�sRJcCs&td|jj|jjf��dS(Nsunorderable types: %s() > %s()(R!R�R(RR�((slib/enum/__init__.pyRI�sRIcCs#t|�|jkr||kStS(N(RPR�R�(RR�((slib/enum/__init__.pyRK�s
RKcCs#t|�|jkr||k	StS(N(RPR�R�(RR�((slib/enum/__init__.pyRL�s
RLcCs
t|j�S(N(thashRb(R((slib/enum/__init__.pyRM�sRMcCs|j|jffS(N(R�R<(Rtproto((slib/enum/__init__.pyR$�sR$cCs|jS(N(Rb(R((slib/enum/__init__.pyRsRcCs|jS(N(R<(R((slib/enum/__init__.pyRsRcs�ttj|�}|r(t|�}n|}t�fd�|j�D��}|||d|�}t|_|j|j�|||<|S(sS
    Create a new Enum subclass that replaces a collection of global constants
    c3s-|]#\}}�|�r||fVqdS(N((R4RR(tfilter(slib/enum/__init__.pys	<genexpr>!sR(	tvarsR�tmodulesRQRRt_reduce_ex_by_nameR$tupdateR�(R%RRR�tsourcetmodule_globalsRn((R�slib/enum/__init__.pyt_converts
"	
R�cBseZdZRS(s.Enum where members are also (and must be) ints(RRR(((slib/enum/__init__.pyR0scCs|jS(N(R(RR�((slib/enum/__init__.pyR�3scCs�g}xE|jj�D]4\}}||jkr|j||jf�qqW|r�djg|D]\}}d||f^qa�}td||f��n|S(sIClass decorator that ensures only unique members exist in an enumeration.s, s%s -> %ssduplicate names found in %r: %s(R�RRRR-R[R,(tenumerationt
duplicatesRR�taliastduplicate_names((slib/enum/__init__.pyR6s,(3RtsysR�t__all__tversiontfloattversion_infoR+Rt	NameErrortcollectionsRtImportErrorR
R�R�R�ReR	RRR R&RQR'RRPR0ttemp_enum_dictRORCRDR�RERFRGRHRJRIRKRLRMR$RRR�R�RhRR�R(((slib/enum/__init__.pyt<module>s�







				/��