U
    /e                     @   s   d dl mZ d dlZd dlZd dlZd dlZdd ZedddZdd	 Zd
d Z	dd Z
dddZdd Zdd Zdd ZdS )    )contextmanagerNc                 C   s(   z|  W dS  | k
r"   Y dS X d S )NFT )excZlamdar   r   //tmp/pip-unpacked-wheel-ziavv1t7/partd/utils.pyraises   s
    r    c              	   c   sp   d|  d } t| \}}t| t| z
|V  W 5 tj|rjtj|r`t	
| n
t| X d S )N.)lstriptempfilemkstemposcloseremovepathexistsisdirshutilrmtree)	extensionhandlefilenamer   r   r   tmpfile   s    


r   c                 C   s   t dt| |  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)bytesr   r   r   frame!   s    r   c                 c   s^   d}t | }t }||k rZtd| ||d  d }|d7 }| |||  V  ||7 }q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nbytesr   r   r   
framesplit+   s     r%   c                 c   s@   t || k r|V  n(tdt || D ]}||||   V  q$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!   r   r   r   partition_all;   s    r'   Fc                    sD   t | tr8r" fdd| D S  fdd| D S n |  S d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   s   | ]}t | d V  qdS )lazyN
nested_get.0r!   collr)   r   r   	<genexpr>\   s     znested_get.<locals>.<genexpr>c                    s   g | ]}t | d qS )r(   r*   r,   r.   r   r   
<listcomp>^   s     znested_get.<locals>.<listcomp>N)
isinstancer   )indr/   r)   r   r.   r   r+   M   s
    
r+   c                 c   s.   | D ]$}t |tr"t|E dH  q|V  qd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)r2   r   flatten)seqitemr   r   r   r4   c   s    
r4   c                 C   sJ   t | tr| | S t | tr8| dd t| d |f S tt| |S d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)r2   strtuplesuffixkeyZtermr   r   r   r:   |   s
    


r:   c                 C   s@   t |trnt |tr|f}n
t|f}t | ts8| f} | | 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')
    )r2   r9   r8   r;   r   r   r   extend   s    




r=   )r   )F)
contextlibr   r   r   r
   r   r   r   r   r%   r'   r+   r4   r:   r=   r   r   r   r   <module>   s   

