î
â^Q\¥  ã               @   sf   d  d l  m Z d  d l m Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d	 S)
é    )ÚSequence)ÚPromisec               @   s.   e  Z d  Z d Z d d „  Z d d „  Z d S)Úmemoized_lazy_functionzî
    Represents a lazy value which is calculated by calling
    func(*args, **kwargs) and then is memoized.

    >>> f = memoized_lazy_function(lambda a: print('.') or a, 'hello')
    >>> f()
    .
    'hello'
    >>> f()
    'hello'
    c             O   s   | |  _  | |  _ | |  _ d  S)N)Ú_funcÚ_argsÚ_kwargs)ÚselfÚfuncÚargsÚkwargs© r   úB/var/www/dbchiro/venv/lib/python3.4/site-packages/leaflet/utils.pyÚ__init__   s    		zmemoized_lazy_function.__init__c             C   s4   t  |  d ƒ s- |  j |  j |  j Ž  |  _ n  |  j S)NÚ_result)Úhasattrr   r   r   r   )r   r   r   r   Ú__call__   s    zmemoized_lazy_function.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   r   c               @   s‘   e  Z d  Z d Z f  d d „ Z d d „  Z d d „  Z d d	 „  Z d
 d „  Z d d „  Z	 d d „  Z
 e d d „  ƒ Z e d d „  ƒ Z d S)ÚListWithLazyItemszÊ
    Mimics a lazy list.

    It keeps items in lazy state and evaluates them when they're
    returned.

    An item is considered lazy when it is
    a `django.utils.functional.Promise` instance.
    c             C   s.   t  | t ƒ r | j } n  t | ƒ |  _ d  S)N)Ú
isinstancer   Ú_listÚlist)r   Úiterabler   r   r   r   )   s    zListWithLazyItems.__init__c             c   s&   x |  j  D] } |  j | ƒ Vq
 Wd  S)N)r   Ú_resolve_lazy_item)r   Úitemr   r   r   Ú__iter__.   s    zListWithLazyItems.__iter__c             C   s   t  |  j ƒ S)N)Úlenr   )r   r   r   r   Ú__len__2   s    zListWithLazyItems.__len__c             C   s   |  j  |  j | ƒ S)N)r   r   )r   Úindexr   r   r   Ú__getitem__5   s    zListWithLazyItems.__getitem__c             C   s/   t  | t ƒ r | j } n  |  j j | ƒ d  S)N)r   r   r   Úextend)r   r   r   r   r   r"   8   s    zListWithLazyItems.extendc             C   s#   t  |  ƒ | ƒ } | j |  ƒ | S)N)Útyper"   )r   r   Ú	lazy_listr   r   r   Ú__radd__=   s    zListWithLazyItems.__radd__c             C   s#   t  |  ƒ |  ƒ } | j | ƒ | S)N)r#   r"   )r   r   r$   r   r   r   Ú__add__B   s    zListWithLazyItems.__add__c             C   s   |  j  | ƒ r | ƒ  } n  | S)N)Úis_lazy_item)Úclsr   r   r   r   r   G   s    z$ListWithLazyItems._resolve_lazy_itemc             C   s   t  | t ƒ S)N)r   r   )r(   r   r   r   r   r'   N   s    zListWithLazyItems.is_lazy_itemN)r   r   r   r   r   r   r   r!   r"   r%   r&   Úclassmethodr   r'   r   r   r   r   r      s   	r   c               @   s"   e  Z d  Z d Z d d „  Z d S)ÚListWithLazyItemsRawIteratorz]
    This lazy list yields raw items (i.e. Promises are not resolved)
    when iterated.
    c             C   s   t  |  j ƒ S)N)Úiterr   )r   r   r   r   r   Y   s    z%ListWithLazyItemsRawIterator.__iter__N)r   r   r   r   r   r   r   r   r   r*   S   s   r*   N)Úcollectionsr   Zdjango.utils.functionalr   r   r   r*   r   r   r   r   Ú<module>   s   5