a
    xdk                     @   s   d dl Z G dd dZdS )    Nc                   @   s   e Zd ZdZdd Zdd Zd!ddZd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  ZdS )"Choicesa  
    A class to encapsulate handy functionality for lists of choices
    for a Django model field.

    Each argument to ``Choices`` is a choice, represented as either a
    string, a two-tuple, or a three-tuple.

    If a single string is provided, that string is used as the
    database representation of the choice as well as the
    human-readable presentation.

    If a two-tuple is provided, the first item is used as the database
    representation and the second the human-readable presentation.

    If a triple is provided, the first item is the database
    representation, the second a valid Python identifier that can be
    used as a readable label in code, and the third the human-readable
    presentation. This is most useful when the database representation
    must sacrifice readability for some reason: to achieve a specific
    ordering, to use an integer rather than a character field, etc.

    Regardless of what representation of each choice is originally
    given, when iterated over or indexed into, a ``Choices`` object
    behaves as the standard Django choices list of two-tuples.

    If the triple form is used, the Python identifier names can be
    accessed as attributes on the ``Choices`` object, returning the
    database representation. (If the single or two-tuple forms are
    used and the database representation happens to be a valid Python
    identifier, the database representation itself is available as an
    attribute on the ``Choices`` object, returning itself.)

    Option groups can also be used with ``Choices``; in that case each
    argument is a tuple consisting of the option group name and a list
    of options, where each option in the list is either a string, a
    two-tuple, or a triple as outlined above.

    c                 G   s.   g | _ g | _i | _i | _t | _| | d S N)_triples_doubles_display_map_identifier_mapset
_db_values_process)selfchoices r   K/var/www/html/Ranjet/env/lib/python3.9/site-packages/model_utils/choices.py__init__,   s    zChoices.__init__c                 C   sX   |d | j |d < |d | j|d < | j|d  || ||d |d f d S )Nr         )r   r   r	   addappend)r   Ztripletriple_collectordouble_collectorr   r   r   _store:   s
    
zChoices._storeNc           
         s   d u rj  d u rj  fdd}|D ]}t|ttfrt|dkrX|| qt|dkrt|d ttfr|d }|d }g }||f g }	 ||	f |||	 q||d |d |d f qtdt| q0||||f q0d S )Nc                    s    |  S r   )r   )cr   r   r   r   r   <lambda>G       z"Choices._process.<locals>.<lambda>   r   r   r   z3Choices can't take a list of length %s, only 2 or 3)	r   r   
isinstancelisttuplelenr   r
   
ValueError)
r   r   r   r   storechoiceZ
group_nameZ
subchoicesZtcdcr   r   r   r
   A   s2    
zChoices._processc                 C   s
   t | jS r   )r   r   r   r   r   r   __len__a   s    zChoices.__len__c                 C   s
   t | jS r   )iterr   r$   r   r   r   __iter__d   s    zChoices.__iter__c                 C   s
   t | jS r   )reversedr   r$   r   r   r   __reversed__g   s    zChoices.__reversed__c                 C   s,   z| j | W S  ty&   t|Y n0 d S r   )r   KeyErrorAttributeError)r   Zattnamer   r   r   __getattr__j   s    zChoices.__getattr__c                 C   s
   | j | S r   )r   )r   keyr   r   r   __getitem__p   s    zChoices.__getitem__c                 C   s*   t || jr|j}nt|}t| j|  S r   )r   	__class__r   r   r   r   otherr   r   r   __add__s   s    zChoices.__add__c                 C   s   t |}t|| j  S r   )r   r   r   r0   r   r   r   __radd__z   s    zChoices.__radd__c                 C   s   t || jr| j|jkS dS )NF)r   r/   r   r0   r   r   r   __eq__   s    zChoices.__eq__c                 C   s"   d | jjddd | jD S )Nz{}({})z, c                 s   s   | ]}d t | V  qdS )z%sN)repr).0ir   r   r   	<genexpr>   r   z#Choices.__repr__.<locals>.<genexpr>)formatr/   __name__joinr   r$   r   r   r   __repr__   s    zChoices.__repr__c                 C   s
   || j v S r   )r	   )r   itemr   r   r   __contains__   s    zChoices.__contains__c                 C   s   | j t| j| S r   )r/   copydeepcopyr   )r   memor   r   r   __deepcopy__   s    zChoices.__deepcopy__c                    sD   t | j }| s*td|  | j fdd| jD  S )Nz-The following identifiers are not present: %sc                    s   g | ]}|d   v r|qS )r   r   )r6   r"   new_identifiersr   r   
<listcomp>   s   z"Choices.subset.<locals>.<listcomp>)r   r   keys
issupersetr    symmetric_differencer/   r   )r   rD   Zidentifiersr   rC   r   subset   s    
zChoices.subset)NN)r:   
__module____qualname____doc__r   r   r
   r%   r'   r)   r,   r.   r2   r3   r4   r<   r>   rB   rI   r   r   r   r   r      s    '
 r   )r?   r   r   r   r   r   <module>   s   