o
    Nrf                     @   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  | y   Y dS w )NFT )excZlamdar   r   T/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/partd/utils.pyraises   s   r    c              	   c   s    d|  d } t| \}}t| t| z |V  W tj|r8tj|r1t	
| d S t| d S d S tj|rRtj|rLt	
| w t| w w )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   sj    d}t | }t }||k r3td| ||d  d }|d7 }| |||  V  ||7 }||k sd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nbytesr   r   r   
framesplit+   s   r%   c                 c   sD    t || k r|V  dS 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                    s>   t | trr fdd| D S  fd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   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>)
isinstancer   )indr/   r)   r   r.   r   r+   M   s
   
r+   c                 c   s0    | 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   sF   t | tr	| | S t | tr| dd t| d |f 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)r2   strtuplesuffixkeytermr   r   r   r:   |   s
   


r:   c                 C   s@   t |trnt |tr|f}nt|f}t | ts| 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    

