
    g                     t    d dl mZ d dlZd dlZd dlZd dlZd Zedd       Zd Zd Z	d Z
ddZd	 Zd
 Zd Zy)    )contextmanagerNc                 *    	  |        y# | $ r Y yw xY w)NFT )exclamdas     +lib/python3.12/site-packages/partd/utils.pyraisesr	      s!     s   
 c              #   t  K   d| j                  d      z   } t        j                  |       \  }}t        j                  |       t        j
                  |       	 | t        j                  j                  |      rKt        j                  j                  |      rt        j                  |       y t        j
                  |       y y # t        j                  j                  |      rKt        j                  j                  |      rt        j                  |       w t        j
                  |       w w xY ww)N.)lstriptempfilemkstemposcloseremovepathexistsisdirshutilrmtree)	extensionhandlefilenames      r   tmpfiler      s     i&&s++I''	2FHHHVIIh$77>>(#ww}}X&h'		(#	 $277>>(#ww}}X&h'		(#	 $s    AD8C	 A+D8	A,D55D8c                 F    t        j                  dt        |             | z   S )z Pack the length of the bytes in front of the bytes

    TODO: This does a full copy.  This should maybe be inlined somehow
    wherever this gets used instead.  My laptop shows a data bandwidth of
    2GB/s
    Q)structpacklen)bytess    r   framer!   !   s     ;;sCJ'%//    c              #      K   d}t        |       }t               }||k  r:t        j                  d| ||dz          d   }|dz  }| |||z     ||z  }||k  r9yyw)z Split buffer into frames of concatenated chunks

    >>> data = frame(b'Hello') + frame(b'World')
    >>> list(framesplit(data))  # doctest: +SKIP
    [b'Hello', b'World']
    r   r      N)r   listr   unpack)r    inchunksnbytess        r   
framesplitr+   +   sn      	
As5z1VF
a%sE!AaCL1!4	QAq6z""	V	 a%s   AAAc              #   |   K   t        |      | k  r| yt        dt        |      |       D ]  }|||| z      yw)z Partition bytes into evenly sized blocks

    The final block holds the remainder and so may not be of equal size

    >>> list(partition_all(2, b'Hello'))
    [b'He', b'll', b'o']

    See Also:
        toolz.partition_all
    r   N)r   range)r(   r    r'   s      r   partition_allr.   ;   s?      5zA~q#e*a(A1Q3- )s   :<c                     t        | t              r*rfd| D        S | D cg c]  }t        |       c}S |    S c c}w )z Get nested index from collection

    Examples
    --------

    >>> nested_get(1, 'abc')
    'b'
    >>> nested_get([1, 0], 'abc')
    ['b', 'a']
    >>> nested_get([[1, 0], [0, 1]], 'abc')
    [['b', 'a'], ['a', 'b']]
    c              3   :   K   | ]  }t        |         yw)lazyN)
nested_get).0r'   collr2   s     r   	<genexpr>znested_get.<locals>.<genexpr>\   s     @CqJq$T22Cs   r1   )
isinstancer%   r3   )indr5   r2   r'   s    `` r   r3   r3   M   sK     #t@C@@<?@CqJq$T2C@@Cy As   Ac              #   n   K   | D ]*  }t        |t              rt        |      E d{    '| , y7 w)aJ  

    >>> list(flatten([1]))
    [1]

    >>> list(flatten([[1, 2], [1, 2]]))
    [1, 2, 1, 2]

    >>> list(flatten([[[1], [2]], [[1], [2]]]))
    [1, 2, 1, 2]

    >>> list(flatten(((1, 2), (1, 2)))) # Don't flatten tuples
    [(1, 2), (1, 2)]

    >>> list(flatten((1, 2, [3, 4]))) # support heterogeneous
    [1, 2, 3, 4]
    N)r7   r%   flatten)seqitems     r   r:   r:   c   s1     $ dD!t}$$J	 $s   $535c                     t        | t              r| |z   S t        | t              r| dd t        | d   |      fz   S t        t        |       |      S )z suffix a key with a suffix

    Works if they key is a string or a tuple

    >>> suffix('x', '.dtype')
    'x.dtype'
    >>> suffix(('a', 'b', 'c'), '.dtype')
    ('a', 'b', 'c.dtype')
    N)r7   strtuplesuffixkeyterms     r   rA   rA   |   sR     #sTz	C	3Bx6#b'40222c#h%%r"   c                     t        |t              rn t        |t              r|f}nt        |      f}t        | t              s| f} | |z   S )z extend a key with a another element in a tuple

    Works if they key is a string or a tuple

    >>> extend('x', '.dtype')
    ('x', '.dtype')
    >>> extend(('a', 'b', 'c'), '.dtype')
    ('a', 'b', 'c', '.dtype')
    )r7   r@   r?   rB   s     r   extendrF      sH     $	D#	wD	|c5!f:r"   ) )F)
contextlibr   r   r   r   r   r	   r   r!   r+   r.   r3   r:   rA   rF   r   r"   r   <module>rI      sP    % 	    $ $ 0  $,2&$r"   