o
    Tik                     @   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   S/var/www/html/evchargy.com/venv/lib/python3.10/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 ]b}t|ttfrst|dkr,|| qt|dkrkt|d ttfr]|d }|d }g }||f g }	 ||	f |||	 q||d |d |d f qtdt| ||||f qd S )Nc                    s    |  S r   )r   )cr   r   r   r   r   <lambda>G   s    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choice
group_nameZ
subchoicesZtcZdcr   r   r   r
   A   s4   
zChoices._processc                 C   
   t | jS r   )r   r   r   r   r   r   __len__a      
zChoices.__len__c                 C   r#   r   )iterr   r$   r   r   r   __iter__d   r&   zChoices.__iter__c                 C   r#   r   )reversedr   r$   r   r   r   __reversed__g   r&   zChoices.__reversed__c                 C   s$   z| j | W S  ty   t|w 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   r&   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   r1   r   r   r   __radd__z   s   zChoices.__radd__c                 C   s   t || jr| j|jkS dS )NF)r   r0   r   r1   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>   s    z#Choices.__repr__.<locals>.<genexpr>)formatr0   __name__joinr   r$   r   r   r   __repr__   s   zChoices.__repr__c                 C   s
   || j v S r   )r	   )r   itemr   r   r   __contains__   r&   zChoices.__contains__c                 C   s   | j t| j| S r   )r0   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   )r7   r!   new_identifiersr   r   
<listcomp>   s
    z"Choices.subset.<locals>.<listcomp>)r   r   keys
issupersetr   symmetric_differencer0   r   )r   rE   identifiersr   rD   r   subset   s   
zChoices.subset)NN)r;   
__module____qualname____doc__r   r   r
   r%   r(   r*   r-   r/   r3   r4   r5   r=   r?   rC   rK   r   r   r   r   r      s"    '
 r   )r@   r   r   r   r   r   <module>   s    