File: recipes.pyx

package info (click to toggle)
python-cytoolz 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 532 kB
  • sloc: python: 3,913; makefile: 34
file content (60 lines) | stat: -rw-r--r-- 1,600 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from cpython.sequence cimport PySequence_Tuple
from cytoolz.itertoolz cimport frequencies, pluck

from itertools import groupby


__all__ = ['countby', 'partitionby']


cpdef object countby(object key, object seq):
    """
    Count elements of a collection by a key function

    >>> countby(len, ['cat', 'mouse', 'dog'])
    {3: 2, 5: 1}

    >>> def iseven(x): return x % 2 == 0
    >>> countby(iseven, [1, 2, 3])  # doctest:+SKIP
    {True: 1, False: 2}

    See Also:
        groupby
    """
    if not callable(key):
        return frequencies(pluck(key, seq))
    return frequencies(map(key, seq))


cdef class partitionby:
    """ partitionby(func, seq)

    Partition a sequence according to a function

    Partition `s` into a sequence of lists such that, when traversing
    `s`, every time the output of `func` changes a new list is started
    and that and subsequent items are collected into that list.

    >>> is_space = lambda c: c == " "
    >>> list(partitionby(is_space, "I have space"))
    [('I',), (' ',), ('h', 'a', 'v', 'e'), (' ',), ('s', 'p', 'a', 'c', 'e')]

    >>> is_large = lambda x: x > 10
    >>> list(partitionby(is_large, [1, 2, 1, 99, 88, 33, 99, -1, 5]))
    [(1, 2, 1), (99, 88, 33, 99), (-1, 5)]

    See also:
        partition
        groupby
        itertools.groupby
    """
    def __cinit__(self, object func, object seq):
        self.iter_groupby = groupby(seq, key=func)

    def __iter__(self):
        return self

    def __next__(self):
        cdef object key, val
        key, val = next(self.iter_groupby)
        return PySequence_Tuple(val)