
    dO3                     T    d Z dZ	  G d d          Zd Zedk    r e             dS dS )aa  Implementation of enumerated types.

This module provides the `Enum` class, which can be used to construct
enumerated types.  Those types are defined by providing an *exhaustive
set or list* of possible, named values for a variable of that type.
Enumerated variables of the same type are usually compared between them
for equality and sometimes for order, but are not usually operated upon.

Enumerated values have an associated *name* and *concrete value*.  Every
name is unique and so are concrete values.  An enumerated variable
always takes the concrete value, not its name.  Usually, the concrete
value is not used directly, and frequently it is entirely irrelevant.
For the same reason, an enumerated variable is not usually compared with
concrete values out of its enumerated type.  For that kind of use,
standard variables and constants are more adequate.

reStructuredTextc                   l    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )Enumaw
  Enumerated type.

    Each instance of this class represents an enumerated type. The
    values of the type must be declared
    *exhaustively* and named with
    *strings*, and they might be given explicit
    concrete values, though this is not compulsory. Once the type is
    defined, it can not be modified.

    There are three ways of defining an enumerated type. Each one
    of them corresponds to the type of the only argument in the
    constructor of Enum:

    - *Sequence of names*: each enumerated
      value is named using a string, and its order is determined by
      its position in the sequence; the concrete value is assigned
      automatically::

          >>> boolEnum = Enum(['True', 'False'])

    - *Mapping of names*: each enumerated
      value is named by a string and given an explicit concrete value.
      All of the concrete values must be different, or a
      ValueError will be raised::

          >>> priority = Enum({'red': 20, 'orange': 10, 'green': 0})
          >>> colors = Enum({'red': 1, 'blue': 1})
          Traceback (most recent call last):
          ...
          ValueError: enumerated values contain duplicate concrete values: 1

    - *Enumerated type*: in that case, a copy
      of the original enumerated type is created. Both enumerated
      types are considered equal::

          >>> prio2 = Enum(priority)
          >>> priority == prio2
          True

    Please note that names starting with _ are
    not allowed, since they are reserved for internal usage::

        >>> prio2 = Enum(['_xx'])
        Traceback (most recent call last):
        ...
        ValueError: name of enumerated value can not start with ``_``: '_xx'

    The concrete value of an enumerated value is obtained by
    getting its name as an attribute of the Enum
    instance (see __getattr__()) or as an item (see
    __getitem__()). This allows comparisons between
    enumerated values and assigning them to ordinary Python
    variables::

        >>> redv = priority.red
        >>> redv == priority['red']
        True
        >>> redv > priority.green
        True
        >>> priority.red == priority.orange
        False

    The name of the enumerated value corresponding to a concrete
    value can also be obtained by using the
    __call__() method of the enumerated type. In this
    way you get the symbolic name to use it later with
    __getitem__()::

        >>> priority(redv)
        'red'
        >>> priority.red == priority[priority(priority.red)]
        True

    (If you ask, the __getitem__() method is
    not used for this purpose to avoid ambiguity in the case of using
    strings as concrete values.)

    c                    | j         }i |d<   i |d<   t          |t                    st          |t                    r-t	          |          D ]\  }}|                     ||           d S t          |t                    r2|                                D ]\  }}|                     ||           d S t          |t                    r7|j	                                        D ]\  }}|                     ||           d S t          d          )N_names_valueszPenumerations can only be created from sequences, mappings and other enumerations)__dict__
isinstancelisttuple	enumerate_check_and_set_pairdictitemsr   r   	TypeError)selfenummydictvaluenames        0lib/python3.11/site-packages/tables/misc/enum.py__init__zEnum.__init__h   s;   xydD!! 	/Ze%<%< 	/!*4 6 6((u55556 6d## 		/!% 6 6u((u55556 6d## 	/!%!2!2!4!4 6 6u((u55556 6  . / / /    c                 >   | j         }| j        }t          |t                    st	          d|          |                    d          rt          d|z            ||v rt          d|z            ||v rt          d|z            |||<   |||<   || j        |<   dS )z;Check validity of enumerated value and insert it into type.*name of enumerated value is not a string: _z5name of enumerated value can not start with ``_``: %rz-enumerated values contain duplicate names: %rz7enumerated values contain duplicate concrete values: %rN)r   r   r	   strr   
startswith
ValueErrorr   )r   r   r   namesvaluess        r   r   zEnum._check_and_set_pair|   s     $$$ 	GETEEG G G??3 	G   5==?$FH H H F??I   du#dr   c                 ^    	 | j         |         S # t          $ r t          d|          w xY w)a+  Get the concrete value of the enumerated value with that name.

        The name of the enumerated value must be a string. If there is no value
        with that name in the enumeration, a KeyError is raised.

        Examples
        --------

        Let ``enum`` be an enumerated type defined as:

        >>> enum = Enum({'T0': 0, 'T1': 2, 'T2': 5})

        then:

        >>> enum['T1']
        2
        >>> enum['foo']
        Traceback (most recent call last):
          ...
        KeyError: "no enumerated value with that name: 'foo'"

        z$no enumerated value with that name: )r   KeyErrorr   r   s     r   __getitem__zEnum.__getitem__   sK    0	L;t$$ 	L 	L 	LJ$JJKKK	Ls    ,c                      t          d          zThis operation is forbidden.zoperation not allowed
IndexErrorr   r   r   s      r   __setitem__zEnum.__setitem__       0111r   c                      t          d          r&   r'   r#   s     r   __delitem__zEnum.__delitem__   r+   r   c                 T    	 | |         S # t           $ r}t          |j         d}~ww xY w)a/  Get the concrete value of the enumerated value with that name.

        The name of the enumerated value must be a string. If there is no value
        with that name in the enumeration, an AttributeError is raised.

        Examples
        --------
        Let ``enum`` be an enumerated type defined as:

        >>> enum = Enum({'T0': 0, 'T1': 2, 'T2': 5})

        then:

        >>> enum.T1
        2
        >>> enum.foo
        Traceback (most recent call last):
          ...
        AttributeError: no enumerated value with that name: 'foo'

        N)r"   AttributeErrorargs)r   r   kes      r   __getattr__zEnum.__getattr__   s=    .	+: 	+ 	+ 	+ "'**	+s   
 
'"'c                      t          d          r&   r/   r)   s      r   __setattr__zEnum.__setattr__       4555r   c                      t          d          r&   r4   r#   s     r   __delattr__zEnum.__delattr__   r6   r   c                 b    t          |t                    st          d|          || j        v S )a  Is there an enumerated value with that name in the type?

        If the enumerated type has an enumerated value with that name, True is
        returned.  Otherwise, False is returned. The name must be a string.

        This method does *not* check for concrete values matching a value in an
        enumerated type. For that, please use the :meth:`Enum.__call__` method.

        Examples
        --------
        Let ``enum`` be an enumerated type defined as:

        >>> enum = Enum({'T0': 0, 'T1': 2, 'T2': 5})

        then:

        >>> 'T1' in enum
        True
        >>> 'foo' in enum
        False
        >>> 0 in enum
        Traceback (most recent call last):
          ...
        TypeError: name of enumerated value is not a string: 0
        >>> enum.T1 in enum  # Be careful with this!
        Traceback (most recent call last):
          ...
        TypeError: name of enumerated value is not a string: 2

        r   )r	   r   r   r   r#   s     r   __contains__zEnum.__contains__   sE    @ $$$ 	GETEEG G Gt{""r   c                     	 | j         |         S # t          $ r0 t          |          dk    r
|d         cY S t          d|          w xY w)a  Get the name of the enumerated value with that concrete value.

        If there is no value with that concrete value in the enumeration and a
        second argument is given as a default, this is returned. Else, a
        ValueError is raised.

        This method can be used for checking that a concrete value belongs to
        the set of concrete values in an enumerated type.

        Examples
        --------
        Let ``enum`` be an enumerated type defined as:

        >>> enum = Enum({'T0': 0, 'T1': 2, 'T2': 5})

        then:

        >>> enum(5)
        'T2'
        >>> enum(42, None) is None
        True
        >>> enum(42)
        Traceback (most recent call last):
          ...
        ValueError: no enumerated value with that concrete value: 42

            z.no enumerated value with that concrete value: )r   r"   lenr   )r   r   defaults      r   __call__zEnum.__call__  su    :	L<&& 	L 	L 	L7||aqz!!!JJJL L L	Ls    %A	A	c                 *    t          | j                  S )zReturn the number of enumerated values in the enumerated type.

        Examples
        --------
        >>> len(Enum(['e%d' % i for i in range(10)]))
        10

        )r=   r   r   s    r   __len__zEnum.__len__*  s     4;r   c              #   H   K   | j                                         E d{V  dS )a  Iterate over the enumerated values.

        Enumerated values are returned as (name, value) pairs *in no particular
        order*.

        Examples
        --------
        >>> enumvals = {'red': 4, 'green': 2, 'blue': 1}
        >>> enum = Enum(enumvals)
        >>> enumdict = dict([(name, value) for (name, value) in enum])
        >>> enumvals == enumdict
        True

        N)r   r   rA   s    r   __iter__zEnum.__iter__6  s4        ;$$&&&&&&&&&&&r   c                 P    t          |t                    sdS | j        |j        k    S )a  Is the other enumerated type equivalent to this one?

        Two enumerated types are equivalent if they have exactly the same
        enumerated values (i.e. with the same names and concrete values).

        Examples
        --------

        Let ``enum*`` be enumerated types defined as:

        >>> enum1 = Enum({'T0': 0, 'T1': 2})
        >>> enum2 = Enum(enum1)
        >>> enum3 = Enum({'T1': 2, 'T0': 0})
        >>> enum4 = Enum({'T0': 0, 'T1': 2, 'T2': 5})
        >>> enum5 = Enum({'T0': 0})
        >>> enum6 = Enum({'T0': 10, 'T1': 20})

        then:

        >>> enum1 == enum1
        True
        >>> enum1 == enum2 == enum3
        True
        >>> enum1 == enum4
        False
        >>> enum5 == enum1
        False
        >>> enum1 == enum6
        False

        Comparing enumerated types with other kinds of objects produces
        a false result:

        >>> enum1 == {'T0': 0, 'T1': 2}
        False
        >>> enum1 == ['T0', 'T1']
        False
        >>> enum1 == 2
        False

        F)r	   r   r   r   others     r   __eq__zEnum.__eq__H  s+    V %&& 	5{el**r   c                 .    |                      |           S )a$  Is the `other` enumerated type different from this one?

        Two enumerated types are different if they don't have exactly
        the same enumerated values (i.e. with the same names and
        concrete values).

        Examples
        --------

        Let ``enum*`` be enumerated types defined as:

        >>> enum1 = Enum({'T0': 0, 'T1': 2})
        >>> enum2 = Enum(enum1)
        >>> enum3 = Enum({'T1': 2, 'T0': 0})
        >>> enum4 = Enum({'T0': 0, 'T1': 2, 'T2': 5})
        >>> enum5 = Enum({'T0': 0})
        >>> enum6 = Enum({'T0': 10, 'T1': 20})

        then:

        >>> enum1 != enum1
        False
        >>> enum1 != enum2 != enum3
        False
        >>> enum1 != enum4
        True
        >>> enum5 != enum1
        True
        >>> enum1 != enum6
        True

        )rH   rF   s     r   __ne__zEnum.__ne__w  s    D ;;u%%%%r   c                     d| j         z  S )a.  Return the canonical string representation of the enumeration. The
        output of this method can be evaluated to give a new enumeration object
        that will compare equal to this one.

        Examples
        --------
        >>> repr(Enum({'name': 10}))
        "Enum({'name': 10})"

        zEnum(%s))r   rA   s    r   __repr__zEnum.__repr__  s     DK''r   N)__name__
__module____qualname____doc__r   r   r$   r*   r-   r2   r5   r8   r:   r?   rB   rD   rH   rJ   rL    r   r   r   r      s       M M^/ / /($ $ $6L L L:2 2 22 2 2+ + +86 6 66 6 6## ## ##J#L #L #LJ
  
  
 ' ' '$-+ -+ -+^"& "& "&P( ( ( ( (r   r   c                  2    dd l } |                                 S )Nr<   )doctesttestmod)rS   s    r   _testrU     s    NNN??r   __main__N)rP   __docformat__r   rU   rM   rQ   r   r   <module>rX      s}    & # 9S( S( S( S( S( S( S( S(l  
 z	EGGGGG r   