
    AeH                     d    d Z ddlZddlmZ dgZ G d de      Z G d de      Z G d d	e      Zy)
a  
Helper for looping over sequences, particular in templates.

Often in a loop in a template it's handy to know what's next up,
previously up, if this is the first or last item in the sequence, etc.
These can be awkward to manage in a normal Python loop, but using the
looper you can get a better sense of the context.  Use like::

    >>> for loop, item in looper(['a', 'b', 'c']):
    ...     print loop.number, item
    ...     if not loop.last:
    ...         print '---'
    1 a
    ---
    2 b
    ---
    3 c

    N)basestring_looperc                   "    e Zd ZdZd Zd Zd Zy)r   z
    Helper for looping (particularly in templates)

    Use this like::

        for loop, item in looper(seq):
            if loop.first:
                ...
    c                     || _         y N)seqselfr   s     6lib/python3.12/site-packages/Cython/Tempita/_looper.py__init__zlooper.__init__&   s	        c                 ,    t        | j                        S r   )looper_iterr   r
   s    r   __iter__zlooper.__iter__)   s    488$$r   c                 P    d| j                   j                  d| j                  dS )N<z for >)	__class____name__r   r   s    r   __repr__zlooper.__repr__,   s    NN##TXX/ 	/r   N)r   
__module____qualname____doc__r   r   r    r   r   r   r      s    %/r   c                   B    e Zd Zd Zd Zd Zej                  dk  reZyy)r   c                 2    t        |      | _        d| _        y Nr   )listr   posr	   s     r   r   zlooper_iter.__init__3   s    9r   c                     | S r   r   r   s    r   r   zlooper_iter.__iter__7   s    r   c                     | j                   t        | j                        k\  rt        t	        | j                  | j                         | j                  | j                      f}| xj                   dz  c_         |S N   )r    lenr   StopIterationloop_pos)r
   results     r   __next__zlooper_iter.__next__:   sT    88s488}$$((DHH-txx/AAAr   3N)	r   r   r   r   r   r)   sysversionnextr   r   r   r   r   1   s)     {{S r   r   c                   ,   e Zd Zd Zd Zd Z ee      Zd Z ee      Zd Z ee      Zd Z	 ee	      Z	e
j                  dk  re	Zd Z ee      Zd	 Z ee      Zd
 Z ee      Zd Z ee      Zd Z ee      Zd Z ee      ZddZddZd Zy)r'   c                      || _         || _        y r   r   r    )r
   r   r    s      r   r   zloop_pos.__init__G   s    r   c                 V    d| j                   | j                     d| j                  dS )Nz
<loop pos=z at r   r0   r   s    r   r   zloop_pos.__repr__K   s"    HHTXX* 	*r   c                     | j                   S r   r    r   s    r   indexzloop_pos.indexO   s    xxr   c                      | j                   dz   S r#   r3   r   s    r   numberzloop_pos.numberS       xx!|r   c                 4    | j                   | j                     S r   r0   r   s    r   itemzloop_pos.itemW   s    xx!!r   c                 Z    	 | j                   | j                  dz      S # t        $ r Y y w xY wr#   )r   r    
IndexErrorr   s    r   r)   zloop_pos.__next__[   s0    	88DHHqL)) 		s    	**r*   c                 Z    | j                   dk(  ry | j                  | j                   dz
     S )Nr   r$   )r    r   r   s    r   previouszloop_pos.previouse   s'    88q=xx1%%r   c                 "    | j                   dz   S N   r3   r   s    r   oddzloop_pos.oddk   s    88a<r   c                      | j                   dz  S r?   r3   r   s    r   evenzloop_pos.eveno   r7   r   c                      | j                   dk(  S r   r3   r   s    r   firstzloop_pos.firsts   s    xx1}r   c                 L    | j                   t        | j                        dz
  k(  S r#   )r    r%   r   r   s    r   lastzloop_pos.lastw   s    xx3txx=1,,,r   c                 ,    t        | j                        S r   )r%   r   r   s    r   lengthzloop_pos.length{   s    488}r   Nc                 j    | j                   ry| j                  | j                  | j                  |      S )a  
        Returns true if this item is the start of a new group,
        where groups mean that some attribute has changed.  The getter
        can be None (the item itself changes), an attribute name like
        ``'.attr'``, a function, or a dict key or list index.
        T)rE   _compare_groupr9   r=   r
   getters     r   first_groupzloop_pos.first_group   s+     ::""499dmmVDDr   c                 j    | j                   ry| j                  | j                  | j                  |      S )a  
        Returns true if this item is the end of a new group,
        where groups mean that some attribute has changed.  The getter
        can be None (the item itself changes), an attribute name like
        ``'.attr'``, a function, or a dict key or list index.
        T)rG   rK   r9   r)   rL   s     r   
last_groupzloop_pos.last_group   s+     99""499dmmVDDr   c                 P   |||k7  S t        |t              rh|j                  d      rW|dd  }|j                  d      r(|d d } t	        ||              t	        ||             k7  S t	        ||      t	        ||      k7  S t        |d      r ||       ||      k7  S ||   ||   k7  S )N.r$   z()__call__)
isinstancer   
startswithendswithgetattrhasattr)r
   r9   otherrM   s       r   rK   zloop_pos._compare_group   s    >5= -##C(ABZFt$,wtV,.2H'%2H2JJJtV,v0FFFVZ($<6%=00<5=00r   r   )r   r   r   r   r   r4   propertyr6   r9   r)   r+   r,   r-   r=   rA   rC   rE   rG   rI   rN   rP   rK   r   r   r   r'   r'   E   s    *UOEfF"D>D
 !H
{{S& !H 
3-CD>DUOE-D>DfF	E	E1r   r'   )	r   r+   Cython.Tempita.compat3r   __all__objectr   r   r'   r   r   r   <module>r_      s?   (  .*/V /,& (^1v ^1r   