ó
œ]c           @   s¿   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
 e d	 „  ƒ Z e d
 „  ƒ Z e d „ Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   contextmanagerNc         C   s(   y | ƒ  t  SWn |  k
 r# t SXd  S(   N(   t   Falset   True(   t   exct   lamda(    (    s*   lib/python2.7/site-packages/partd/utils.pyt   raises   s
    t    c         c   s—   d |  j  d ƒ }  t j |  ƒ \ } } t j | ƒ t j | ƒ z	 | VWd  t j j | ƒ r’ t j j | ƒ r‚ t	 j
 | ƒ q’ t j | ƒ n  Xd  S(   Nt   .(   t   lstript   tempfilet   mkstempt   ost   closet   removet   patht   existst   isdirt   shutilt   rmtree(   t	   extensiont   handlet   filename(    (    s*   lib/python2.7/site-packages/partd/utils.pyt   tmpfile   s    	c         C   s   t  j d t |  ƒ ƒ |  S(   sÔ    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
    t   Q(   t   structt   packt   len(   t   bytes(    (    s*   lib/python2.7/site-packages/partd/utils.pyt   frame!   s    c         c   sw   d } t  |  ƒ } t ƒ  } xU | | k  rr t j d |  | | d !ƒ d } | d 7} |  | | | !V| | 7} q Wd S(   s±    Split buffer into frames of concatenated chunks

    >>> data = frame(b'Hello') + frame(b'World')
    >>> list(framesplit(data))  # doctest: +SKIP
    [b'Hello', b'World']
    i    R   i   N(   R   t   listR   t   unpack(   R   t   it   nt   chunkst   nbytes(    (    s*   lib/python2.7/site-packages/partd/utils.pyt
   framesplit+   s     	!
c         c   sQ   t  | ƒ |  k  r | Vn3 x0 t d t  | ƒ |  ƒ D] } | | | |  !Vq3 Wd S(   sâ    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
    i    N(   R   t   range(   R    R   R   (    (    s*   lib/python2.7/site-packages/partd/utils.pyt   partition_all;   s    c          g   s!   y	 d  VWn |  k
 r n Xd  S(   N(    (   R   (    (    s*   lib/python2.7/site-packages/partd/utils.pyt   ignoringM   s    	c          o   s	   d  Vd  S(   N(    (   t   argst   kwargs(    (    s*   lib/python2.7/site-packages/partd/utils.pyt
   do_nothingU   s    c            sa   t  |  t ƒ rU ˆ r, ‡  ‡ f d †  |  Dƒ Sg  |  D] } t | ˆ  d ˆ ƒ^ q3 Sn ˆ  |  Sd S(   sá    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(   t   lazyN(   t
   nested_get(   t   .0R   (   t   collR*   (    s*   lib/python2.7/site-packages/partd/utils.pys	   <genexpr>i   s    R*   N(   t
   isinstanceR   R+   (   t   indR-   R*   R   (    (   R-   R*   s*   lib/python2.7/site-packages/partd/utils.pyR+   Z   s
    )c         c   sH   xA |  D]9 } t  | t ƒ r; x! t | ƒ D] } | Vq) Wq | Vq Wd S(   sJ  

    >>> 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   t   flatten(   t   seqt   itemt   item2(    (    s*   lib/python2.7/site-packages/partd/utils.pyR0   p   s
    c         C   sY   t  |  t ƒ r |  | St  |  t ƒ rB |  d  t |  d | ƒ f St t |  ƒ | ƒ Sd S(   s¿    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')
    iÿÿÿÿN(   R.   t   strt   tuplet   suffix(   t   keyt   term(    (    s*   lib/python2.7/site-packages/partd/utils.pyR6   Š   s
    
c         C   s_   t  | t ƒ r n* t  | t ƒ r- | f } n t | ƒ f } t  |  t ƒ sW |  f }  n  |  | S(   sÝ    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.   R5   R4   (   R7   R8   (    (    s*   lib/python2.7/site-packages/partd/utils.pyt   extendœ   s    
(   t
   contextlibR    R   R   R	   R   R   R   R   R#   R%   R&   R)   R   R+   R0   R6   R9   (    (    (    s*   lib/python2.7/site-packages/partd/utils.pyt   <module>   s    		
				