a
    ”˜ëj‡  ã                   @   sü   d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZmZmZmZmZ ddlmZ ed	ƒZG d
d„ dƒZee ee eee  ee eeddf dœdd„ZG dd„ dee ƒZeZee ee eee  dœdd„ZdS )z%Utilities for working with iterators.é    ©Údeque)Ú	GeneratorÚIterableÚIterator)ÚAbstractContextManager)Úislice)ÚTracebackType)ÚAnyÚGenericÚOptionalÚTypeVarÚUnionÚoverload)ÚLiteralÚTc                   @   sF   e Zd ZdZddœdd„Zeee  ee ee e	d dœdd	„Z
dS )
ÚNoLockz@Dummy lock that provides the proper interface but no protection.N©Úreturnc                 C   s   dS )zDo nothing.N© ©Úselfr   r   úX/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/utils/iter.pyÚ	__enter__   s    zNoLock.__enter__F©Úexc_typeÚexc_valÚexc_tbr   c                 C   s   dS )z(Return False (exception not suppressed).Fr   ©r   r   r   r   r   r   r   Ú__exit__   s    zNoLock.__exit__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚtypeÚBaseExceptionr	   r   r   r   r   r   r   r      s   
ûr   N)ÚiteratorÚbufferÚpeersÚlockr   c                 c   sf  zð|s€|�f |rW d  ƒ qzt | ƒ}W n" tyL   Y W d  ƒ qŒY n0 |D ]}| |¡ qRW d  ƒ n1 sv0    Y  | ¡ V  qW |�N t|ƒD ]\}}||u rœ| |¡  q¼qœ|sÒt| dƒrÒ|  ¡  W d  ƒ n1 sæ0    Y  np|�X t|ƒD ]$\}}||u �r | |¡  �q&�q |�s@t| dƒ�r@|  ¡  W d  ƒ n1 �sV0    Y  0 dS )a  An individual iterator of a :py:func:`~.tee`.

    This function is a generator that yields items from the shared iterator
    ``iterator``. It buffers items until the least advanced iterator has
    yielded them as well. The buffer is shared with all other peers.

    Args:
        iterator: The shared iterator.
        buffer: The buffer for this peer.
        peers: The buffers of all peers.
        lock: The lock to synchronise access to the shared buffers.

    Yields:
        The next item from the shared iterator.
    NÚclose)ÚnextÚStopIterationÚappendÚpopleftÚ	enumerateÚpopÚhasattrr*   )r&   r'   r(   r)   ÚitemZpeer_bufferÚidxr   r   r   Útee_peer&   s4    *
(ø


r4   c                   @   s  e Zd ZdZdddœee eeee	  dœdd„Z
edœd	d
„Zeeee dœdd„ƒZeeeee df dœdd„ƒZeeef eee eee df f dœdd„Zeee  dœdd„Zddœdd„Zeee  ee ee ed dœdd„Zddœdd„ZdS )ÚTeeaY  Create ``n`` separate asynchronous iterators over ``iterable``.

    This splits a single ``iterable`` into multiple iterators, each providing
    the same items in the same order.
    All child iterators may advance separately but share the same items
    from ``iterable`` -- when the most advanced iterator retrieves an item,
    it is buffered until the least advanced iterator has yielded it as well.
    A ``tee`` works lazily and can handle an infinite ``iterable``, provided
    that all iterators advance.

    .. code-block:: python

        async def derivative(sensor_data):
            previous, current = a.tee(sensor_data, n=2)
            await a.anext(previous)  # advance one iterator
            return a.map(operator.sub, previous, current)

    Unlike :py:func:`itertools.tee`, :py:func:`~.tee` returns a custom type instead
    of a :py:class:`tuple`. Like a tuple, it can be indexed, iterated and unpacked
    to get the child iterators. In addition, its :py:meth:`~.tee.aclose` method
    immediately closes all children, and it can be used in an ``async with`` context
    for the same effect.

    If ``iterable`` is an iterator and read elsewhere, ``tee`` will *not*
    provide these items. Also, ``tee`` must internally buffer each item until the
    last iterator has yielded it; if the most and least advanced iterator differ
    by most data, using a :py:class:`list` is more efficient (but not lazy).

    If the underlying iterable is concurrency safe (``anext`` may be awaited
    concurrently) the resulting iterators are concurrency safe as well. Otherwise,
    the iterators are safe if there is only ever one single "most advanced" iterator.
    To enforce sequential use of ``anext``, provide a ``lock``
    - e.g. an :py:class:`asyncio.Lock` instance in an :py:mod:`asyncio` application -
    and access is automatically synchronised.

    é   N)r)   )ÚiterableÚnr)   c                   s>   t |ƒˆ_dd„ t|ƒD ƒˆ_t‡ ‡fdd„ˆjD ƒƒˆ_dS )a  Create a ``tee``.

        Args:
            iterable: The iterable to split.
            n: The number of iterators to create. Defaults to 2.
            lock: The lock to synchronise access to the shared buffers.
                Defaults to None.
        c                 S   s   g | ]
}t ƒ ‘qS r   r   )Ú.0Ú_r   r   r   Ú
<listcomp>”   ó    z Tee.__init__.<locals>.<listcomp>c                 3   s0   | ](}t ˆj|ˆjˆ d urˆ ntƒ d�V  qd S )N)r&   r'   r(   r)   )r4   Ú	_iteratorÚ_buffersr   )r9   r'   ©r)   r   r   r   Ú	<genexpr>•   s   úüzTee.__init__.<locals>.<genexpr>N)Úiterr=   Úranger>   ÚtupleÚ	_children)r   r7   r8   r)   r   r?   r   Ú__init__„   s
    
ùzTee.__init__r   c                 C   s
   t | jƒS )z%Return the number of child iterators.)ÚlenrD   r   r   r   r   Ú__len__Ÿ   s    zTee.__len__)r2   r   c                 C   s   d S ©Nr   ©r   r2   r   r   r   Ú__getitem__£   s    zTee.__getitem__.c                 C   s   d S rH   r   rI   r   r   r   rJ   ¦   s    c                 C   s
   | j | S )z9Return the child iterator(s) at the given index or slice.©rD   rI   r   r   r   rJ   ©   s    c                 c   s   | j E dH  dS )zgReturn an iterator over the child iterators.

        Yields:
            The child iterators.
        NrK   r   r   r   r   Ú__iter__¯   s    zTee.__iter__zTee[T]c                 C   s   | S )zReturn Tee instance.r   r   r   r   r   r   ·   s    zTee.__enter__Fr   c                 C   s   |   ¡  dS )zcClose all child iterators.

        Returns:
            False (exception not suppressed).
        F)r*   r   r   r   r   r   »   s    zTee.__exit__c                 C   s   | j D ]}| ¡  qdS )zClose all child iterators.N)rD   r*   )r   Úchildr   r   r   r*   É   s    
z	Tee.close)r6   )r    r!   r"   r#   r   r   Úintr   r   r
   rE   rG   r   rJ   ÚslicerC   r   rL   r   r$   r%   r	   r   r   r*   r   r   r   r   r5   ^   s2   ( ýû
û
þ
ûr5   )Úsizer7   r   c                 c   s*   t |ƒ}tt|| ƒƒ}|sdS |V  qdS )zËUtility batching function.

    Args:
        size: The size of the batch. If None, returns a single batch.
        iterable: The iterable to batch.

    Yields:
        The batches of the iterable.
    N)rA   Úlistr   )rP   r7   ÚitÚchunkr   r   r   Úbatch_iterateÓ   s
    
rT   )r#   Úcollectionsr   Úcollections.abcr   r   r   Ú
contextlibr   Ú	itertoolsr   Útypesr	   Útypingr
   r   r   r   r   r   Ztyping_extensionsr   r   r   rQ   r4   r5   ZsafeteerN   rT   r   r   r   r   Ú<module>   s$    	
ù8r