B
    P\                 @   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
edd Zedd ZdddZdd Zdd Zdd ZdS )    )contextmanagerNc             C   s$   y
|  dS  | k
r   dS X d S )NFT )excZlamdar   r   *lib/python3.7/site-packages/partd/utils.pyraises   s
    r    c          	   c   sp   d|  d } t| \}}t| t| z
|V  W d tj|rjtj|r`t	
| n
t| X d S )N.)lstriptempfileZmkstemposcloseremovepathexistsisdirshutilZrmtree)	extensionZ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)structZpacklen)bytesr   r   r   frame!   s    r   c             c   sb   d}t | }t }xJ||k r\td| ||d  d }|d7 }| |||  V  ||7 }qW 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   Zunpack)r   inZchunksnbytesr   r   r   
framesplit+   s     
r   c             c   sD   t || k r|V  n,x*tdt || D ]}||||   V  q&W 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'))
    ['He', 'll', 'o']

    See Also:
        toolz.partition_all
    r   N)r   range)r   r   r   r   r   r   partition_all;   s    r!   c              g   s$   y
d V  W n | k
r   Y nX d S )Nr   )r   r   r   r   ignoringM   s    
r"   c              o   s
   d V  d S )Nr   )argskwargsr   r   r   
do_nothingU   s    r%   Fc                sF   t | tr:r" fdd| D S  fdd| D S tS  |  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>i   s    znested_get.<locals>.<genexpr>c                s   g | ]}t | d qS ))r&   )r'   )r(   r   )r)   r&   r   r   
<listcomp>k   s    znested_get.<locals>.<listcomp>N)
isinstancer   seq)Zindr)   r&   r   )r)   r&   r   r'   Z   s    
r'   c             c   s<   x6| D ].}t |tr.xt|D ]
}|V  qW q|V  qW 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)r,   r   flatten)r-   itemZitem2r   r   r   r.   q   s
    

r.   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)r,   strtuplesuffix)keytermr   r   r   r3      s
    


r3   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')
    )r,   r2   r1   )r4   r5   r   r   r   extend   s    




r6   )r   )F)
contextlibr   r   r   r
   r   r   r   r   r   r!   r"   r%   r'   r.   r3   r6   r   r   r   r   <module>   s    

