Welcome, guest | Sign In | My Account | Store | Cart

Walk a tree of generators without yielding things recursively. Check out the docstring. It has an example ;)

Python, 88 lines
 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import types

__docformat__ = "restructuredtext"


def walk_recursive_generators(generator):

    """Walk a tree of generators without yielding things recursively.

    Let's suppose you have this:

    >>> def generator0():
    ...     yield 3
    ...     yield 4
    ... 
    >>> def generator1():
    ...     yield 2
    ...     for i in generator0():
    ...         yield i
    ...     yield 5
    ... 
    >>> def generator2():
    ...     yield 1
    ...     for i in generator1():
    ...         yield i
    ...     yield 6
    ... 
    >>> for i in generator2():
    ...     print i
    ... 
    1
    2
    3
    4
    5
    6

    Notice the way the generators are recursively yielding values.  This
    library uses a technique called "bounce" that is usually used to
    implement stackless interpreters.  It lets you write:

    >>> def generator0():
    ...     yield 3
    ...     yield 4
    ... 
    >>> def generator1():
    ...     yield 2
    ...     yield generator0()
    ...     yield 5
    ... 
    >>> def generator2():
    ...     yield 1
    ...     yield generator1()
    ...     yield 6
    ... 
    >>> for i in walk_recursive_generators(generator2()):
    ...     print i
    ... 
    1
    2
    3
    4
    5
    6

    Look Ma!  No recursive yields!

    """

    stack = [generator]
    while stack:
        for x in stack[-1]:
            if isinstance(x, types.GeneratorType):
                stack.append(x)  # Recurse.
                break
            else:
                yield x
        else:
            stack.pop()


def _test():
    import doctest
    doctest.testmod()


if __name__ == "__main__":
    _test()