
    n%@e                     |    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dS )    )contextmanagerNc                 4    	  |             dS # | $ r Y dS w xY w)NFT )exclamdas     +lib/python3.11/site-packages/partd/utils.pyraisesr	      s9    u   tts   
  c              #   p  K   d|                      d          z   } t          j        |           \  }}t          j        |           t          j        |           	 |V  t          j                            |          rKt          j                            |          rt          j
        |           d S t          j        |           d S d S # t          j                            |          rIt          j                            |          rt          j
        |           w t          j        |           w w xY w)N.)lstriptempfilemkstemposcloseremovepathexistsisdirshutilrmtree)	extensionhandlefilenames      r   tmpfiler      s     i&&s+++I'	22FHHVIh$7>>(## 	$w}}X&& $h'''''	(#####		$ 	$27>>(## 	$w}}X&& $h''''	(####		$s   C A*D5c                 L    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JJ''%//    c              #      K   d}t          |           }t                      }||k     rGt          j        d| ||dz                      d         }|dz  }| |||z            V  ||z  }||k     EdS dS )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,   +   s       	
As5zz1VVF
a%%sE!AaC%L11!4	QAq6zM""""	V	 a%%%%%%r#   c              #      K   t          |          | k     r|V  dS t          dt          |          |           D ]}|||| z            V  dS )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/   ;   sj       5zzA~~q#e**a(( 	  	 A1Q3-	  	 r#   Fc                     t          | t                    r rfd| D             S fd| D             S |          S )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          |           V  dS )lazyN
nested_get.0r(   collr3   s     r   	<genexpr>znested_get.<locals>.<genexpr>\   s2      @@qJq$T222@@@@@@r#   c                 4    g | ]}t          |           S )r2   r4   r6   s     r   
<listcomp>znested_get.<locals>.<listcomp>^   s(    @@@qJq$T222@@@r#   )
isinstancer&   )indr8   r3   s    ``r   r5   r5   M   se     #t  	A@@@@@C@@@@@@@@@C@@@@Cyr#   c              #   r   K   | D ]1}t          |t                    rt          |          E d{V  -|V  2dS )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)r<   r&   flatten)seqitems     r   r?   r?   c   s_      $   dD!! 	t}}$$$$$$$$JJJJ	 r#   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)r<   strtuplesuffixkeyterms     r   rF   rF   |   sl     #s &Tz	C		 &3B3x6#b'400222c#hh%%%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')
    )r<   rE   rD   rG   s     r   extendrK      sd     $ 	D#		 wD		|c5!! f:r#   )r
   )F)
contextlibr   r   r   r   r   r	   r   r"   r,   r/   r5   r?   rF   rK   r   r#   r   <module>rM      s    % % % % % % 				      $ $ $ $ 0 0 0        $   ,  2& & &$    r#   