B
    ž†\H  ã               @   sR   d Z ddlZddlmZ dgZG dd„ deƒZG dd„ deƒZG dd	„ d	eƒZdS )
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               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r   z¤
    Helper for looping (particularly in templates)

    Use this like::

        for loop, item in looper(seq):
            if loop.first:
                ...
    c             C   s
   || _ d S )N)Úseq)Úselfr   © r   ú5lib/python3.7/site-packages/Cython/Tempita/_looper.pyÚ__init__&   s    zlooper.__init__c             C   s
   t | jƒS )N)Úlooper_iterr   )r   r   r   r   Ú__iter__)   s    zlooper.__iter__c             C   s   d| j j| jf S )Nz<%s for %r>)Ú	__class__Ú__name__r   )r   r   r   r   Ú__repr__,   s    zlooper.__repr__N)r   Ú
__module__Ú__qualname__Ú__doc__r   r
   r   r   r   r   r   r      s   	c               @   s2   e Zd Zdd„ Zdd„ Zdd„ Zejdk r.eZdS )	r	   c             C   s   t |ƒ| _d| _d S )Nr   )Úlistr   Úpos)r   r   r   r   r   r   3   s    
zlooper_iter.__init__c             C   s   | S )Nr   )r   r   r   r   r
   7   s    zlooper_iter.__iter__c             C   s@   | j t| jƒkrt‚t| j| j ƒ| j| j  f}|  j d7  _ |S )Né   )r   Úlenr   ÚStopIterationÚloop_pos)r   Úresultr   r   r   Ú__next__:   s
    zlooper_iter.__next__Ú3N)	r   r   r   r   r
   r   ÚsysÚversionÚnextr   r   r   r   r	   1   s
   
r	   c               @   sæ   e Zd Zdd„ Zdd„ Zdd„ ZeeƒZdd„ ZeeƒZd	d
„ ZeeƒZdd„ Z	ee	ƒZ	e
jdk rfe	Zdd„ ZeeƒZdd„ ZeeƒZdd„ ZeeƒZdd„ ZeeƒZdd„ ZeeƒZdd„ ZeeƒZd!dd„Zd"dd„Zdd „ ZdS )#r   c             C   s   || _ || _d S )N)r   r   )r   r   r   r   r   r   r   G   s    zloop_pos.__init__c             C   s   d| j | j | jf S )Nz<loop pos=%r at %r>)r   r   )r   r   r   r   r   K   s    zloop_pos.__repr__c             C   s   | j S )N)r   )r   r   r   r   ÚindexO   s    zloop_pos.indexc             C   s
   | j d S )Nr   )r   )r   r   r   r   ÚnumberS   s    zloop_pos.numberc             C   s   | j | j S )N)r   r   )r   r   r   r   ÚitemW   s    zloop_pos.itemc             C   s*   y| j | jd  S  tk
r$   d S X d S )Nr   )r   r   Ú
IndexError)r   r   r   r   r   [   s    zloop_pos.__next__r   c             C   s   | j dkrd S | j| j d  S )Nr   r   )r   r   )r   r   r   r   Úpreviouse   s    
zloop_pos.previousc             C   s   | j d  S )Né   )r   )r   r   r   r   Úoddk   s    zloop_pos.oddc             C   s
   | j d S )Nr"   )r   )r   r   r   r   Úeveno   s    zloop_pos.evenc             C   s
   | j dkS )Nr   )r   )r   r   r   r   Úfirsts   s    zloop_pos.firstc             C   s   | j t| jƒd kS )Nr   )r   r   r   )r   r   r   r   Úlastw   s    zloop_pos.lastc             C   s
   t | jƒS )N)r   r   )r   r   r   r   Úlength{   s    zloop_pos.lengthNc             C   s   | j r
dS |  | 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)r%   Ú_compare_groupr   r!   )r   Úgetterr   r   r   Úfirst_group   s    zloop_pos.first_groupc             C   s   | j r
dS |  | 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)r&   r(   r   r   )r   r)   r   r   r   Ú
last_groupŠ   s    zloop_pos.last_groupc             C   s¢   |d kr||kS t |tƒrt| d¡rt|dd … }| d¡r^|d d… }t||ƒƒ t||ƒƒ kS t||ƒt||ƒkS n*t|dƒrŽ||ƒ||ƒkS || || kS d S )NÚ.r   z()éþÿÿÿÚ__call__)Ú
isinstancer   Ú
startswithÚendswithÚgetattrÚhasattr)r   r   Úotherr)   r   r   r   r(   •   s    



zloop_pos._compare_group)N)N)r   r   r   r   r   r   Úpropertyr   r   r   r   r   r   r!   r#   r$   r%   r&   r'   r*   r+   r(   r   r   r   r   r   E   s6   


r   )	r   r   ZCython.Tempita.compat3r   Ú__all__Úobjectr   r	   r   r   r   r   r   Ú<module>   s   