Welcome, guest | Sign In | My Account | Store | Cart
def recursiveLooper(iterators, pos=0):
    """ Implements the same functionality as nested for loops, but is 
        more dynamic. iterators can either be a list of methods which
        return iterables, a list of iterables, or a combination of both.
    """
    nextLoop, v = None, []
    try:
        gen = iter(iterators[pos]())
    except TypeError:
        gen = iter(iterators[pos])
        
    while True:
        try:
            yield v + nextLoop.next()
        except (StopIteration, AttributeError):
            v = [gen.next(),]
                
            if pos < len(iterators) - 1:
                nextLoop = recursiveLooper(iterators, pos + 1)
            else:
                yield v

# Some examples of how to use it:
def gen():
    for x in [1, 2]:
        yield x

for x in gen():
    for y in gen():
        for z in gen():
            print x, y, z

print "-" * 10

for x, y, z in recursiveLooper([gen,] * 3):
    print x, y, z

print "-" * 10

for x, y, z in recursiveLooper([[1, 2],] * 3):
    print x, y, z

Diff to Previous Revision

--- revision 2 2010-10-01 00:39:13
+++ revision 3 2010-10-01 00:40:26
@@ -1,7 +1,7 @@
 def recursiveLooper(iterators, pos=0):
     """ Implements the same functionality as nested for loops, but is 
         more dynamic. iterators can either be a list of methods which
-        return iterables or a list of iterables (or a combination).
+        return iterables, a list of iterables, or a combination of both.
     """
     nextLoop, v = None, []
     try:

History