
    Dh1;                        d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
mZ ddlmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZ ej>                  fdee   d	ee   d
ee   fdZ dddee   de!de"d
eeedf      fdZ# G d d      Z$ e$       Z%dee   de!d
eeedf      fdZ&dee   de!d
eeedf      fdZ'dee   dee   d
ee   fdZ(d:deded
ee   fdZ)dee   d
ee   fdZ*dee   dee   d
ee   fdZ+dee   dee   d
ee   fd Z,e
dee   d
eeeee   f      fd!       Z-e
dee   d"eeef   d
eeeee   f      fd#       Z-	 d;dee   d"e	eeef      d
eeeee   f      fd$Z-e
dee   d%e	e!   d
ee   fd&       Z.e
	 d<dee   d'e!d%e	e!   d(e!d
ee   f
d)       Z.dee   d*e	e!   d
ee   fd+Z.	 d;dee   de	e!   d
eeedf      fd,Z/dd-d.ee   d/e!d
eeedf      fd0Z0d=d1ede!d
ee   fd2Z1d3ee   dee   d
ee   fd4Z2dee   dee   d
ee   fd5Z3d>dee   de!d
eee   df   fd6Z4dd7d.ee   d8ed
eeedf      fd9Z5y)?a  
Async-compatible version of itertools standard library functions.

These functions build on top of the async builtins components,
enabling use of both standard iterables and async iterables, without
needing to use if/else clauses or awkward logic.  Standard iterables
get wrapped in async generators, and all functions are designed for
use with `await`, `async for`, etc.

See https://docs.python.org/3/library/itertools.html for reference.
    N)AnyAsyncIteratorListOptionaloverloadTuple   )	enumerateiterlistnexttuplezip)maybe_await)
AccumulatorAnyFunctionAnyIterableAnyIterableIterableAnyStopKeyFunctionN	PredicateRTitrfuncreturnc                   K   t        |       } 	 t        |        d{   }| | 2 3 d{   }t         |||             d{   }| )7 4# t        $ r Y yw xY w7 57 6 yw)aG  
    Yield the running accumulation of an iterable and operator.

    Accepts both a standard function or a coroutine for accumulation.

    Example::

        data = [1, 2, 3, 4]

        async def mul(a, b):
            return a * b

        async for total in accumulate(data, func=mul):
            ...  # 1, 2, 6, 24

    N)r   r   r   r   )r   r   totalitems       x/var/www/fastuser/data/www/generator.snapmosaic.io/flask_app/venv/lib/python3.12/site-packages/aioitertools/itertools.py
accumulater"   &   sv     & s)Cc? K  d!$ud"344 # 4 si   A)A AA A)A'A#A'A)A%
A)A 	A A)A  A)#A'%A)'A)F)strictiterablenr#   .c               
  K   |dk  rt        d      t        |       }t        t        ||             d{   x}rA|rt	        |      |k7  rt        d      | t        t        ||             d{   x}r@yy7 H7 w)z
    Yield batches of values from the given iterable. The final batch may be shorter.

    Example::

        async for batch in batched(range(15), 5):
            ...  # (0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14)

    r	   zn must be at least oneNzbatched: incomplete batch)
ValueErrorr   r   islicelen)r$   r%   r#   	aiteratorbatchs        r!   batchedr,   E   s      	1u122XIvi344
4%
4c%jAo899 vi344
4%
444s'   4BA?>B5B6B=BBc                   D    e Zd Zdee   dee   fdZdee   dee   fdZy)Chainitrsr   c                 $    | j                  |      S )a  
        Yield values from one or more iterables in series.

        Consumes the first iterable lazily, in entirety, then the second, and so on.

        Example::

            async for value in chain([1, 2, 3], [7, 8, 9]):
                ...  # 1, 2, 3, 7, 8, 9

        )from_iterable)selfr/   s     r!   __call__zChain.__call__^   s     !!$''    c                v   K   t        |      2 3 d{   }t        |      2 3 d{   }| 7 7 6 )6 yw)za
        Like chain, but takes an iterable of iterables.

        Alias for chain(*itrs)
        N)r   )r2   r/   r   r    s       r!   r1   zChain.from_iterablel   sF      d 	 	#"3i  d
	i $s0   971795359759N)	__name__
__module____qualname__r   r   r   r3   r   r1    r4   r!   r.   r.   ]   s=    (k!n (q1A ((;A(> =QRCS r4   r.   rc                v   K   t        |        d{   }t        j                  ||      D ]  }| 	 y7 %w)aD  
    Yield r length subsequences from the given iterable.

    Simple wrapper around itertools.combinations for asyncio.
    This will consume the entire iterable before yielding values.

    Example::

        async for value in combinations(range(4), 3):
            ...  # (0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)

    N)r   	itertoolscombinationsr   r:   poolvalues       r!   r=   r=   z   s9      s)OD''a0  $   97&9c                v   K   t        |        d{   }t        j                  ||      D ]  }| 	 y7 %w)aq  
    Yield r length subsequences from the given iterable with replacement.

    Simple wrapper around itertools.combinations_with_replacement.
    This will consume the entire iterable before yielding values.

    Example::

        async for value in combinations_with_replacement("ABC", 2):
            ...  # ("A", "A"), ("A", "B"), ("A", "C"), ("B", "B"), ...

    N)r   r<   combinations_with_replacementr>   s       r!   rC   rC      s9      s)OD88qA  $rA   	selectorsc                T   K   t        | |      2 3 d{   \  }}|s| 7 6 yw)a  
    Yield elements only when the corresponding selector evaluates to True.

    Stops when either the iterable or the selectors have been exhausted.

    Example::

        async for value in compress(range(5), [1, 0, 0, 1, 1]):
            ...  # 0, 3, 4
    N)r   )r   rD   r@   selectors       r!   compressrG      s3      "%S)!4  oeXK!4s    (&$&((&(startstepc                &   K   | }	 | ||z  }w)z
    Yield an infinite series, starting at the given value and increasing by step.

    Example::

        async for value in counter(10, -1):
            ...  # 10, 9, 8, 7, ...

    r9   )rH   rI   r@   s      r!   countrK      s#      E
 s   c                   K   g }t        |       2 3 d{   }| |j                  |        7 6 	 |D ]  }| 	 w)a*  
    Yield a repeating series from the given iterable.

    Lazily consumes the iterable when the next value is needed, and caching
    the values in memory for future iterations of the series.

    Example::

        async for value in cycle([1, 2]):
            ...  # 1, 2, 1, 2, 1, 2, ...

    N)r   append)r   itemsr    s      r!   cyclerO      sW      E3i  d
Ti  	DJ	 s   A202A2A	predicatec                   K   t        |      }|2 3 d{   }t         | |             d{   r#|  |2 3 d{   }| 7 57 6 7 6 yw)a2  
    Drops all items until the predicate evaluates False; yields all items afterwards.

    Accepts both standard functions and coroutines for the predicate.

    Example::

        def pred(x):
            return x < 4

        async for item in dropwhile(pred, range(6)):
            ...  # 4, 5, 6

    Nr   r   )rP   r$   r   r    s       r!   	dropwhilerS      sf     " x.C  d 4111J  d
1 cs\   AAA
AAAAAAA AA
AAAAAc                ~   K   t        |      2 3 d{   }t         | |             d{   r#| )7 $7 6 yw)a)  
    Yield items from the iterable only when the predicate evaluates to False.

    Accepts both standard functions and coroutines for the predicate.

    Example::

        def pred(x):
            return x < 4

        async for item in filterfalse(pred, range(6)):
            ...  # 4, 5

    NrR   rP   r$   r    s      r!   filterfalserV      sA     " 8n  d 4111J1 %s,   =;7;=9==;==c                      y Nr9   )r   s    r!   groupbyrY   
  s    r4   keyc                      y rX   r9   )r   rZ   s     r!   rY   rY          	r4   c                ^  K   |d }g }t        |       }	 t        |       d{   }|g}t         ||             d{   }|2 3 d{   }t         ||             d{   }||k7  r||f |g}n|j	                  |       |}F7 h# t        $ r Y yw xY w7 ]7 T7 =6 ||f yw)a  
    Yield consecutive keys and groupings from the given iterable.

    Items will be grouped based on the key function, which defaults to
    the identity of each item.  Accepts both standard functions and
    coroutines for the key function.  Suggest sorting by the key
    function before using groupby.

    Example::

        data = ["A", "a", "b", "c", "C", "c"]

        async for key, group in groupby(data, key=str.lower):
            key  # "a", "b", "c"
            group  # ["A", "a"], ["b"], ["c", "C", "c"]

    Nc                     | S rX   r9   )xs    r!   <lambda>zgroupby.<locals>.<lambda>+  s     r4   )r   r   StopAsyncIterationr   rM   )r   rZ   groupingitr    jks          r!   rY   rY     s     ( {H	cB"X~ vH#d)$$A  dc$i((6X+vHOOD!   	%(  X+sy   B-B BB B- BB-B$B B$B-$B"%(B-B 	BB-BB- B$"B-$	B-__stopc                      y rX   r9   )r   rf   s     r!   r(   r(   C  r\   r4   __start__stepc                      y rX   r9   )r   rh   rf   ri   s       r!   r(   r(   J  r\   r4   argsc                  K   d}d}|st        d      t        |      dk(  r|\  }n4t        |      dk(  r|\  }}n t        |      dk(  r|\  }}}nt        d      |dk\  r||dk\  r|dk\  sJ t        d|      }|dk(  ryt        |       2 3 d{   \  }}||k\  r||z
  |z  dk(  r| |$|dz   |k\  s- y7 *6 yw)a
  
    Yield selected items from the given iterable.

    islice(iterable, stop)
    islice(iterable, start, stop[, step])

    Starting from the start index (or zero), stopping at the stop
    index (or until exhausted), skipping items if step > 0.

    Example::

        data = range(10)

        async for item in islice(data, 5):
            ...  # 0, 1, 2, 3, 4

        async for item in islice(data, 2, 5):
            ...  # 2, 3, 4

        async for item in islice(data, 1, 7, 2):
            ...  # 1, 3, 5

    r   r	   zmust pass stop index      ztoo many arguments givenN)r'   r)   maxr
   )r   rk   rH   rI   stopindexr    s          r!   r(   r(   Q  s     0 ED/00
4yA~	Tat	Ta tT344A:4<419$!)CCq$<Dqy&s^  keTE>uu}49J	T 1	^s6   B	B>B<B:B<B>/B>8B>:B<<B>c                v   K   t        |        d{   }t        j                  ||      D ]  }| 	 y7 %w)a?  
    Yield r length permutations of elements in the iterable.

    Simple wrapper around itertools.combinations for asyncio.
    This will consume the entire iterable before yielding values.

    Example::

        async for value in permutations(range(3)):
            ...  # (0, 1, 2), (0, 2, 1), (1, 0, 2), ...

    N)r   r<   permutationsr>   s       r!   rs   rs     s9      s)OD''a0  $rA   )repeatr/   rt   c                   K   t        j                  |D cg c]  }t        |       c}  d{   }t        j                  |d| iD ]  }| 	 yc c}w 7 )w)a  
    Yield cartesian products of all iterables.

    Simple wrapper around itertools.combinations for asyncio.
    This will consume all iterables before yielding any values.

    Example::

        async for value in product("abc", "xy"):
            ...  # ("a", "x"), ("a", "y"), ("b", "x"), ...

        async for value in product(range(3), repeat=3):
            ...  # (0, 0, 0), (0, 0, 1), (0, 0, 2), ...

    Nrt   )asynciogatherr   r<   product)rt   r/   r   poolsr@   s        r!   rx   rx     sT     $ .."=49"=>>E""E9&9  #>>s   AAAA*Aelemc                .   K   	 |dk(  ry|  |dz  }w)z
    Yield the given value repeatedly, forever or up to n times.

    Example::

        async for value in repeat(7):
            ...  # 7, 7, 7, 7, 7, 7, ...

    r   r	   Nr9   )rz   r%   s     r!   rt   rt     s(      6
	Q	 s   fnc                   K   t        |      2 3 d{   }t        |       d{   }t         | |        d{    67 17  7 6 yw)aV  
    Yield values from a function using an iterable of iterables for arguments.

    Each iterable contained within will be unpacked and consumed before
    executing the function or coroutine.

    Example::

        data = [(1, 1), (1, 1, 1), (2, 2)]

        async for value in starmap(operator.add, data):
            ...  # 2, 3, 4

    N)r   r   r   )r|   r$   r   rk   s       r!   starmapr~     sK     " (^ + +c#YD	***+* $sE   AA
AA
AAAAAA
AA
Ac                   K   t        |      2 3 d{   }t         | |             d{   r| ( y7 %7 6 yw)a(  
    Yield values from the iterable until the predicate evaluates False.

    Accepts both standard functions and coroutines for the predicate.

    Example::

        def pred(x):
            return x < 4

        async for value in takewhile(pred, range(8)):
            ...  # 0, 1, 2, 3

    NrR   rU   s      r!   	takewhiler     sD     " 8n  dYt_---J	- %s(   ><8<>:><>>c                 D    |dkD  sJ t               t        |      D cg c]  }t        j                          c}dt        dt        j                  dt
        t           f fdt        j                  fdt        j                        D              S c c}w )u  
    Return n iterators that each yield items from the given iterable.

    The first iterator lazily fetches from the original iterable, and then
    queues the values for the other iterators to yield when needed.

    Caveat: all iterators are dependent on the first iterator – if it is
    consumed more slowly than the rest, the other consumers will be blocked
    until the first iterator continues forward.  Similarly, if the first
    iterator is consumed more quickly than the rest, more memory will be
    used in keeping values in the queues until the other iterators finish
    consuming them.

    Example::

        it1, it2 = tee(range(5), n=2)

        async for value in it1:
            ...  # 0, 1, 2, 3, 4

        async for value in it2:
            ...  # 0, 1, 2, 3, 4

    r   re   qr   c           
     f  K   | dk(  rW	 t              2 3 d {   }t        j                  dd  D cg c]  }|j                  d |f       c}  d {    | K	 |j                          d {   \  }}|||u ry | *7 qc c}w 7 <6 nU# t        $ rI}t        j                  dd  D cg c]  }|j                  |d f       nc c}w c}  d {  7    d }~ww xY wt        j                  dd  D cg c]  }|j                  d f       nc c}w c}  d {  7   y 7 w)Nr   r	   )r   rv   rw   put	Exceptionget)	re   r   r@   queueeerrorr   queuessentinels	         r!   genztee.<locals>.gen	  s4    6#'9    %!..@Fqr
Ku%))T5M2K    K %&UUW}u$KH$  K $-
  nnPQPR&Tuyy!T';&T&TUUU ..FSTSUJ"W5599dH-=#>"W"WXXX  -s   D1B BBBB B
B B
B  D14D/5D1BB B D1	C(C#5C
C#CC##C((D1DD1(D+)D1c              3   6   K   | ]  \  }} ||        y wrX   r9   ).0re   r   r   s      r!   	<genexpr>ztee.<locals>.<genexpr>   s     K1#a)Ks   )
objectrangerv   Queueintr   r   builtinsr   r
   )r   r%   re   r   r   r   s   `  @@@r!   teer     s|    2 q5L5xH<A!H"Eq7==?"EFS W]] }Q/? . >>K0B0B60JKKK3 #Fs   B)	fillvaluer   c                  K   |D cg c]  }t        |       }}t        |      }d}	 t        j                  |D cg c]  }|j	                          c}ddi d{   }t        j                  |      D ]@  \  }}	t        |	t              r|dz  }| ||<   t        |       ||<   /t        |	t              s@|	 ||k\  ryt        j                  |       c c}w c c}w 7 w)au  
    Yield a tuple of items from mixed iterables until all are consumed.

    If shorter iterables are exhausted, the default value will be used
    until all iterables are exhausted.

    Example::

        a = range(3)
        b = range(5)

        async for a, b in zip_longest(a, b, fillvalue=-1):
            a  # 0, 1, 2, -1, -1
            b  # 0, 1, 2,  3,  4

    r   Treturn_exceptionsNr	   )r   r)   rv   rw   	__anext__r   r
   
isinstancer   rt   BaseExceptionr   )
r   r/   r   its	itr_countfinishedrc   valuesidxr@   s
             r!   zip_longestr   #  s     & ;?$?3T#Y$?C$?CIH
~~'*+blln+
?C
 
 #,,V4 	JC%)A's!),CE=1	 y nnV$$ 	 %@ ,
s-   C%C#C%C
C%C#AC%7-C%)r   r	   rX   )r	   ))rm   )6__doc__rv   r   r<   operatortypingr   r   r   r   r   r   r
   r   r   r   r   r   helpersr   typesr   r   r   r   r   r   r   r   r   r   addr"   r   boolr,   r.   chainr=   rC   rG   rK   rO   rS   rV   rY   r(   rs   rx   rt   r~   r   r   r   r9   r4   r!   <module>r      s  
     F F = =     19	Q*1~1F 	!n
 	
 5C=!0 4 	KN s }U1c6]7S $	Q5C=!(	Q$/$41$q A mA.> "[^ a(8 .|'21~14|'21~1, 
	Q 	M%47
2C$D 	 
	 
		Q	)!Q$/	5DG$%	 
	 =A*	Q*&{1a4'89*5d1g&'*Z 
		Q	!)#	1	 
	 
LM		Q	"%	/7}	FI	1	 
	.k!n .Xc] .}Q?O .d -1	Q$SM5C=!* *+q>#&5C=!.q S -*: "+A+"5c":+1+,|'21~104L[^ 4L 4LE-2BC2G,H 4Lp /3$%s$%(+$%5c?#$%r4   