
    g3fi                        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  ed      Z G d	 d
      Zdee   dee   deee      dee   deeddf   f
dZ G d dee         ZeZdedz  dee   deee      fdZy)z%Utilities for working with iterators.    )deque)	GeneratorIterableIterator)AbstractContextManager)islice)TracebackType)AnyGenericLiteralTypeVaroverloadTc            	       N    e Zd ZdZd
dZdee   dz  dedz  dedz  ded   fd	Z	y)NoLockz@Dummy lock that provides the proper interface but no protection.returnNc                      y)zDo nothing.N selfs    W/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/utils/iter.py	__enter__zNoLock.__enter__   s        exc_typeexc_valexc_tbFc                      y)z(Return False (exception not suppressed).Fr   r   r   r   r   s       r   __exit__zNoLock.__exit__   s     r   r   N)
__name__
__module____qualname____doc__r   typeBaseExceptionr	   r   r   r   r   r   r   r      sM    J}%, % $	
 
r   r   iteratorbufferpeerslockr   Nc              #     K   	 	 |s<|5  |r
	 ddd       	 t        |       }|D ]  }|j                  |        	 ddd       |j                          Q# t        $ r Y ddd       nw xY w# 1 sw Y   3xY w	 |5  t	        |      D ]  \  }}||u s|j                  |        n |st        | d      r| j                          ddd       y# 1 sw Y   yxY w# |5  t	        |      D ]  \  }}||u s|j                  |        n |st        | d      r| j                          ddd       w # 1 sw Y   w xY wxY ww)a  An individual iterator of a `.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appendStopIterationpopleft	enumeratepophasattrr,   )r'   r(   r)   r*   itempeer_bufferidxs          r   tee_peerr7   #   s]    .! 5  	5 5
5#H~ ,1 5K'..t455" ..""'  ) 5 55 5 5&  	!$-e$4  [&(IIcN
 WXw7 	! 	! 	!T 	!$-e$4  [&(IIcN
 WXw7 	! 	! 	!s   D>C A-	C AA-C 	A*A- 	C )A**A--A62C :D><C2C	D>CD>D;D/42D/&	D;/D84D;;D>c            	       &   e Zd ZdZ	 ddddee   dedee   dz  fdZ	defd	Z
ed
edee   fd       Zed
edeee   df   fd       Zd
eez  dee   eee   df   z  fdZdeee      fdZddZdee   dz  dedz  dedz  ded   fdZddZy)Teea  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.

    ```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 `itertools.tee`, `.tee` returns a custom type instead
    of a :py`tuple`. Like a tuple, it can be indexed, iterated and unpacked
    to get the child iterators. In addition, its `.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`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`asyncio.Lock` instance in an :py:mod:`asyncio` application -
    and access is automatically synchronised.

    N)r*   iterablenr*   c                     t        |       _        t        |      D cg c]  }t                c} _        t         fd j                  D               _        yc c}w )zCreate a `tee`.

        Args:
            iterable: The iterable to split.
            n: The number of iterators to create.
            lock: The lock to synchronise access to the shared buffers.

        c              3   |   K   | ]3  }t        j                  |j                  n	t                      5 y w)N)r'   r(   r)   r*   )r7   	_iterator_buffersr   ).0r(   r*   r   s     r   	<genexpr>zTee.__init__.<locals>.<genexpr>   sB      
  mm!-T68	 
s   9<N)iterr>   ranger   r?   tuple	_children)r   r:   r;   r*   _s   `  ` r   __init__zTee.__init__   sM     h:?((CQ(C 
 --
 
 )Ds   Ar   c                 ,    t        | j                        S )z%Return the number of child iterators.)lenrE   r   s    r   __len__zTee.__len__   s    4>>""r   r4   c                      y Nr   r   r4   s     r   __getitem__zTee.__getitem__   s    58r   .c                      y rL   r   rM   s     r   rN   zTee.__getitem__   s    CFr   c                      | j                   |   S )z9Return the child iterator(s) at the given index or slice.rE   rM   s     r   rN   zTee.__getitem__   s    ~~d##r   c              #   8   K   | j                   E d{    y7 w)zgReturn an iterator over the child iterators.

        Yields:
            The child iterators.
        NrQ   r   s    r   __iter__zTee.__iter__   s      >>!!s   c                     | S )zReturn Tee instance.r   r   s    r   r   zTee.__enter__   s    r   r   r   r   Fc                 $    | j                          y)zcClose all child iterators.

        Returns:
            False (exception not suppressed).
        F)r,   r   s       r   r   zTee.__exit__   s     	

r   c                 F    | j                   D ]  }|j                           y)zClose all child iterators.N)rE   r,   )r   childs     r   r,   z	Tee.close   s    ^^ 	EKKM	r   )   )r   zTee[T]r    )r!   r"   r#   r$   r   r   intr   r
   rG   rJ   r   rN   slicerD   rS   r   r%   r&   r	   r   r   r,   r   r   r   r9   r9   [   s'   #P 

 48
1+
 

 %S)D0
6# # 888 8FF%S0@*AF F$e $eHQKQTDT>U0U $"(8A;/ "}%, % $	
 
r   r9   sizer:   c              #   \   K   t        |      }	 t        t        ||             }|sy| w)zUtility 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)rB   listr   )r[   r:   itchunks       r   batch_iterater`      s5      
hB
VB%&	 s   *,)r$   collectionsr   collections.abcr   r   r   
contextlibr   	itertoolsr   typesr	   typingr
   r   r   r   r   r   r   r]   r7   r9   safeteerY   r`   r   r   r   <module>rh      s    +  9 9 -    CL  5!qk5! !H5!
 a>5! !
%5! q$}5!pl'!* l` d
 hqk htAw>O r   