ó
oÙè[c           @   s@  d  Z  d d l m Z d d l m Z m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z d d l Z d d l m Z m Z m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# g Z e j d$ „ Z d% „  Z d& d' „ Z d( „  Z  d d) „ Z" d d* „ Z# d+ „  Z$ e% d, „ Z& d- „  Z' d. „  Z( d/ „  Z) d0 „  Z* d d1 „ Z+ d2 „  Z, d d3 „ Z- d4 „  Z. d5 „  Z/ d6 „  Z0 d d7 „ Z1 d d8 „ Z2 d d9 „ Z3 d d d: „ Z4 d; „  Z5 d d< „ Z6 d= „  Z7 d> „  Z8 d? „  Z9 d@ „  Z: d S(A   s  Imported from the recipes section of the itertools documentation.

All functions taken from the recipes section of the itertools library docs
[1]_.
Some backward-compatible usability improvements have been made.

.. [1] http://docs.python.org/library/itertools.html#recipes

iÿÿÿÿ(   t   deque(	   t   chaint   combinationst   countt   cyclet   groupbyt   islicet   repeatt   starmapt   teeN(   t	   randranget   samplet   choice(   t   PY2(   t   filtert   filterfalset   mapt   ranget   zipt   zip_longestt
   accumulatet	   all_equalt   consumet
   dotproductt
   first_truet   flattent   groupert   iter_exceptt   ncyclest   ntht   nth_combinationt   padnonet   pairwiset	   partitiont   powersett   prependt   quantifyt#   random_combination_with_replacementt   random_combinationt   random_permutationt   random_productt
   repeatfunct
   roundrobint   tabulatet   tailt   taket   unique_everseent   unique_justseenc         c   s_   t  |  ƒ } y t | ƒ } Wn t k
 r0 d SX| Vx" | D] } | | | ƒ } | Vq= Wd S(   s_  
    Return an iterator whose items are the accumulated results of a function
    (specified by the optional *func* argument) that takes two arguments.
    By default, returns accumulated sums with :func:`operator.add`.

        >>> list(accumulate([1, 2, 3, 4, 5]))  # Running sum
        [1, 3, 6, 10, 15]
        >>> list(accumulate([1, 2, 3], func=operator.mul))  # Running product
        [1, 2, 6]
        >>> list(accumulate([0, 1, -1, 2, 3, 2], func=max))  # Running maximum
        [0, 1, 1, 2, 3, 3]

    This function is available in the ``itertools`` module for Python 3.2 and
    greater.

    N(   t   itert   nextt   StopIteration(   t   iterablet   funct   itt   totalt   element(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   4   s    c         C   s   t  t | |  ƒ ƒ S(   s.  Return first *n* items of the iterable as a list.

        >>> take(3, range(10))
        [0, 1, 2]
        >>> take(5, range(3))
        [0, 1, 2]

    Effectively a short replacement for ``next`` based iterator consumption
    when you want more than one item, but less than the whole iterator.

    (   t   listR   (   t   nR3   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR-   R   s    i    c         C   s   t  |  t | ƒ ƒ S(   s©  Return an iterator over the results of ``func(start)``,
    ``func(start + 1)``, ``func(start + 2)``...

    *func* should be a function that accepts one integer argument.

    If *start* is not specified it defaults to 0. It will be incremented each
    time the iterator is advanced.

        >>> square = lambda x: x ** 2
        >>> iterator = tabulate(square, -3)
        >>> take(4, iterator)
        [9, 4, 1, 0]

    (   R   R   (   t   functiont   start(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR+   a   s    c         C   s   t  t | d |  ƒƒ S(   s   Return an iterator over the last *n* items of *iterable*.

        >>> t = tail(3, 'ABCDEFG')
        >>> list(t)
        ['E', 'F', 'G']

    t   maxlen(   R0   R    (   R9   R3   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR,   s   s    c         C   s<   | d k r t |  d d ƒn t t |  | | ƒ d ƒ d S(   sX  Advance *iterable* by *n* steps. If *n* is ``None``, consume it
    entirely.

    Efficiently exhausts an iterator without returning values. Defaults to
    consuming the whole iterator, but an optional second argument may be
    provided to limit consumption.

        >>> i = (x for x in range(10))
        >>> next(i)
        0
        >>> consume(i, 3)
        >>> next(i)
        4
        >>> consume(i)
        >>> next(i)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        StopIteration

    If the iterator has fewer items remaining than the provided limit, the
    whole iterator will be consumed.

        >>> i = (x for x in range(3))
        >>> consume(i, 5)
        >>> next(i)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        StopIteration

    R<   i    N(   t   NoneR    R1   R   (   t   iteratorR9   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   ~   s     c         C   s   t  t |  | d ƒ | ƒ S(   s™   Returns the nth item or a default value.

        >>> l = range(10)
        >>> nth(l, 3)
        3
        >>> nth(l, 20, "zebra")
        'zebra'

    N(   R1   R   R=   (   R3   R9   t   default(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   ¦   s    
c         C   s)   t  |  ƒ } t | t ƒ o( t | t ƒ S(   s    
    Returns ``True`` if all the elements are equal to each other.

        >>> all_equal('aaaa')
        True
        >>> all_equal('aaab')
        False

    (   R   R1   t   Truet   False(   R3   t   g(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   ³   s    
c         C   s   t  t | |  ƒ ƒ S(   sk   Return the how many times the predicate is true.

        >>> quantify([True, False, True])
        2

    (   t   sumR   (   R3   t   pred(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR$   Á   s    c         C   s   t  |  t d ƒ ƒ S(   sÿ   Returns the sequence of elements and then returns ``None`` indefinitely.

        >>> take(5, padnone(range(3)))
        [0, 1, 2, None, None]

    Useful for emulating the behavior of the built-in :func:`map` function.

    See also :func:`padded`.

    N(   R   R   R=   (   R3   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   Ë   s    c         C   s   t  j t t |  ƒ | ƒ ƒ S(   s~   Returns the sequence elements *n* times

        >>> list(ncycles(["a", "b"], 3))
        ['a', 'b', 'a', 'b', 'a', 'b']

    (   R   t   from_iterableR   t   tuple(   R3   R9   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   Ù   s    c         C   s   t  t t j |  | ƒ ƒ S(   sk   Returns the dot product of the two iterables.

        >>> dotproduct([10, 10], [20, 20])
        400

    (   RC   R   t   operatort   mul(   t   vec1t   vec2(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   ã   s    c         C   s   t  j |  ƒ S(   sÜ   Return an iterator flattening one level of nesting in a list of lists.

        >>> list(flatten([[0, 1], [2, 3]]))
        [0, 1, 2, 3]

    See also :func:`collapse`, which can flatten multiple levels of nesting.

    (   R   RE   (   t   listOfLists(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   í   s    	c         G   s5   | d k r t |  t | ƒ ƒ St |  t | | ƒ ƒ S(   sG  Call *func* with *args* repeatedly, returning an iterable over the
    results.

    If *times* is specified, the iterable will terminate after that many
    repetitions:

        >>> from operator import add
        >>> times = 4
        >>> args = 3, 5
        >>> list(repeatfunc(add, times, *args))
        [8, 8, 8, 8]

    If *times* is ``None`` the iterable will not terminate:

        >>> from random import randrange
        >>> times = None
        >>> args = 1, 11
        >>> take(6, repeatfunc(randrange, times, *args))  # doctest:+SKIP
        [2, 4, 8, 1, 8, 4]

    N(   R=   R   R   (   R4   t   timest   args(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR)   ù   s    c         C   s,   t  |  ƒ \ } } t | d ƒ t | | ƒ S(   sš   Returns an iterator of paired items, overlapping, from the original

        >>> take(4, pairwise(count()))
        [(0, 1), (1, 2), (2, 3), (3, 4)]

    N(   R	   R1   R=   R   (   R3   t   at   b(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR      s    c         C   s#   t  | ƒ g |  } t d | | Œ S(   s    Collect data into fixed-length chunks or blocks.

        >>> list(grouper(3, 'ABCDEFG', 'x'))
        [('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]

    t	   fillvalue(   R0   R   (   R9   R3   RP   RM   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR      s    c          g   s¢   t  |  ƒ } t r+ t d „  |  Dƒ ƒ } n t d „  |  Dƒ ƒ } xZ | r y x | D] } | ƒ  VqT WWqD t k
 r™ | d 8} t t | | ƒ ƒ } qD XqD Wd S(   sJ  Yields an item from each iterable, alternating between them.

        >>> list(roundrobin('ABC', 'D', 'EF'))
        ['A', 'D', 'E', 'B', 'F', 'C']

    This function produces the same output as :func:`interleave_longest`, but
    may perform better for some inputs (in particular when the number of
    iterables is small).

    c         s   s   |  ] } t  | ƒ j Vq d  S(   N(   R0   R1   (   t   .0R5   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pys	   <genexpr>9  s    c         s   s   |  ] } t  | ƒ j Vq d  S(   N(   R0   t   __next__(   RQ   R5   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pys	   <genexpr>;  s    i   N(   t   lenR   R   R2   R   (   t	   iterablest   pendingt   nextsR1   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR*   +  s    	
c         C   s.   t  | ƒ \ } } t |  | ƒ t |  | ƒ f S(   s³  
    Returns a 2-tuple of iterables derived from the input iterable.
    The first yields the items that have ``pred(item) == False``.
    The second yields the items that have ``pred(item) == True``.

        >>> is_odd = lambda x: x % 2 != 0
        >>> iterable = range(10)
        >>> even_items, odd_items = partition(is_odd, iterable)
        >>> list(even_items), list(odd_items)
        ([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])

    (   R	   R   R   (   RD   R3   t   t1t   t2(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR!   E  s    c            s9   t  |  ƒ ‰  t j ‡  f d †  t t ˆ  ƒ d ƒ Dƒ ƒ S(   sœ  Yields all possible subsets of the iterable.

        >>> list(powerset([1, 2, 3]))
        [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

    :func:`powerset` will operate on iterables that aren't :class:`set`
    instances, so repeated elements in the input will produce repeated elements
    in the output. Use :func:`unique_everseen` on the input to avoid generating
    duplicates:

        >>> seq = [1, 1, 0]
        >>> list(powerset(seq))
        [(), (1,), (1,), (0,), (1, 1), (1, 0), (1, 0), (1, 1, 0)]
        >>> from more_itertools import unique_everseen
        >>> list(powerset(unique_everseen(seq)))
        [(), (1,), (0,), (1, 0)]

    c         3   s   |  ] } t  ˆ  | ƒ Vq d  S(   N(   R   (   RQ   t   r(   t   s(    s5   lib/python2.7/site-packages/more_itertools/recipes.pys	   <genexpr>k  s    i   (   R8   R   RE   R   RS   (   R3   (    (   RZ   s5   lib/python2.7/site-packages/more_itertools/recipes.pyR"   W  s    c         c   s
  t  ƒ  } | j } g  } | j } | d k r• xÖ |  D]Z } y" | | k r[ | | ƒ | Vn  Wq4 t k
 r | | k rŽ | | ƒ | VqŽ q4 Xq4 Wnq xn |  D]f } | | ƒ } y" | | k rÏ | | ƒ | Vn  Wqœ t k
 r| | k r| | ƒ | Vqqœ Xqœ Wd S(   sj  
    Yield unique elements, preserving order.

        >>> list(unique_everseen('AAAABBBCCDAABBB'))
        ['A', 'B', 'C', 'D']
        >>> list(unique_everseen('ABBCcAD', str.lower))
        ['A', 'B', 'C', 'D']

    Sequences with a mix of hashable and unhashable items can be used.
    The function will be slower (i.e., `O(n^2)`) for unhashable items.

    N(   t   sett   addt   appendR=   t	   TypeError(   R3   t   keyt   seensett   seenset_addt   seenlistt   seenlist_addR7   t   k(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR.   n  s0    			



c         C   s(   t  t t  t j d ƒ t |  | ƒ ƒ ƒ S(   sñ   Yields elements in order, ignoring serial duplicates

        >>> list(unique_justseen('AAAABBBCCDAABBB'))
        ['A', 'B', 'C', 'D', 'A', 'B']
        >>> list(unique_justseen('ABBCcAD', str.lower))
        ['A', 'B', 'C', 'A', 'D']

    i   (   R   R1   RG   t
   itemgetterR   (   R3   R_   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR/   –  s    	c         c   sB   y* | d k	 r | ƒ  Vn  x |  ƒ  Vq WWn | k
 r= n Xd S(   sX  Yields results from a function repeatedly until an exception is raised.

    Converts a call-until-exception interface to an iterator interface.
    Like ``iter(func, sentinel)``, but uses an exception instead of a sentinel
    to end the loop.

        >>> l = [0, 1, 2]
        >>> list(iter_except(l.pop, IndexError))
        [2, 1, 0]

    N(   R=   (   R4   t	   exceptiont   first(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   ¢  s    c         C   s   t  t | |  ƒ | ƒ S(   s  
    Returns the first true value in the iterable.

    If no true value is found, returns *default*

    If *pred* is not None, returns the first item for which
    ``pred(item) == True`` .

        >>> first_true(range(10))
        1
        >>> first_true(range(10), pred=lambda x: x > 5)
        6
        >>> first_true(range(10), default='missing', pred=lambda x: x > 9)
        'missing'

    (   R1   R   (   R3   R?   RD   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR   ·  s    c          O   sC   g  |  D] } t  | ƒ ^ q | j d d ƒ } t  d „  | Dƒ ƒ S(   sÇ  Draw an item at random from each of the input iterables.

        >>> random_product('abc', range(4), 'XYZ')  # doctest:+SKIP
        ('c', 3, 'Z')

    If *repeat* is provided as a keyword argument, that many items will be
    drawn from each iterable.

        >>> random_product('abcd', range(4), repeat=2)  # doctest:+SKIP
        ('a', 2, 'd', 3)

    This equivalent to taking a random selection from
    ``itertools.product(*args, **kwarg)``.

    R   i   c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R   (   RQ   t   pool(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pys	   <genexpr>Ü  s    (   RF   t   get(   RM   t   kwdsRh   t   pools(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR(   Ë  s    /c         C   s=   t  |  ƒ } | d k r$ t | ƒ n | } t  t | | ƒ ƒ S(   sb  Return a random *r* length permutation of the elements in *iterable*.

    If *r* is not specified or is ``None``, then *r* defaults to the length of
    *iterable*.

        >>> random_permutation(range(5))  # doctest:+SKIP
        (3, 4, 0, 1, 2)

    This equivalent to taking a random selection from
    ``itertools.permutations(iterable, r)``.

    N(   RF   R=   RS   R   (   R3   RY   Rh   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR'   ß  s    c            sM   t  |  ƒ ‰  t ˆ  ƒ } t t t | ƒ | ƒ ƒ } t  ‡  f d †  | Dƒ ƒ S(   sÿ   Return a random *r* length subsequence of the elements in *iterable*.

        >>> random_combination(range(5), 3)  # doctest:+SKIP
        (2, 3, 4)

    This equivalent to taking a random selection from
    ``itertools.combinations(iterable, r)``.

    c         3   s   |  ] } ˆ  | Vq d  S(   N(    (   RQ   t   i(   Rh   (    s5   lib/python2.7/site-packages/more_itertools/recipes.pys	   <genexpr>þ  s    (   RF   RS   t   sortedR   R   (   R3   RY   R9   t   indices(    (   Rh   s5   lib/python2.7/site-packages/more_itertools/recipes.pyR&   ñ  s    
c            sT   t  |  ƒ ‰ t ˆ ƒ ‰  t ‡  f d †  t | ƒ Dƒ ƒ } t  ‡ f d †  | Dƒ ƒ S(   sS  Return a random *r* length subsequence of elements in *iterable*,
    allowing individual elements to be repeated.

        >>> random_combination_with_replacement(range(3), 5) # doctest:+SKIP
        (0, 0, 1, 2, 2)

    This equivalent to taking a random selection from
    ``itertools.combinations_with_replacement(iterable, r)``.

    c         3   s   |  ] } t  ˆ  ƒ Vq d  S(   N(   R
   (   RQ   Rl   (   R9   (    s5   lib/python2.7/site-packages/more_itertools/recipes.pys	   <genexpr>  s    c         3   s   |  ] } ˆ  | Vq d  S(   N(    (   RQ   Rl   (   Rh   (    s5   lib/python2.7/site-packages/more_itertools/recipes.pys	   <genexpr>  s    (   RF   RS   Rm   R   (   R3   RY   Rn   (    (   R9   Rh   s5   lib/python2.7/site-packages/more_itertools/recipes.pyR%     s    "c   	      C   sP  t  |  ƒ } t | ƒ } | d k  s0 | | k r9 t ‚ n  d } t | | | ƒ } x1 t d | d ƒ D] } | | | | | } qf W| d k  rŸ | | 7} n  | d k  s· | | k rÀ t ‚ n  g  } x} | rE| | | | d | d } } } x7 | | k r,| | 8} | | | | | d } } qö W| j | d | ƒ qÉ Wt  | ƒ S(   s)  Equivalent to ``list(combinations(iterable, r))[index]``.

    The subsequences of *iterable* that are of length *r* can be ordered
    lexicographically. :func:`nth_combination` computes the subsequence at
    sort position *index* directly, without computing the previous
    subsequences.

    i    i   iÿÿÿÿ(   RF   RS   t
   ValueErrort   minR   t
   IndexErrorR]   (	   R3   RY   t   indexRh   R9   t   cRd   Rl   t   result(    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR     s(    				$
!c         C   s   t  |  g | ƒ S(   s  Yield *value*, followed by the elements in *iterator*.

        >>> value = '0'
        >>> iterator = ['1', '2', '3']
        >>> list(prepend(value, iterator))
        ['0', '1', '2', '3']

    To prepend multiple values, see :func:`itertools.chain`.

    (   R   (   t   valueR>   (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyR#   6  s    (;   t   __doc__t   collectionsR    t	   itertoolsR   R   R   R   R   R   R   R   R	   RG   t   randomR
   R   R   t   sixR   t	   six.movesR   R   R   R   R   R   t   __all__R\   R   R-   R+   R,   R=   R   R   R   t   boolR$   R   R   R   R   R)   R    R   R*   R!   R"   R.   R/   R   R   R(   R'   R&   R%   R   R#   (    (    (    s5   lib/python2.7/site-packages/more_itertools/recipes.pyt   <module>	   s|   @.			(	
		
	
					(				$