This commit was manufactured by cvs2svn to create tag 'r221'.
[python/dscho.git] / Lib / test / test_generators.py
blob2c319e5db982901f4dee8df50a4faa4942af12f1
1 from __future__ import generators
3 tutorial_tests = """
4 Let's try a simple generator:
6 >>> def f():
7 ... yield 1
8 ... yield 2
10 >>> for i in f():
11 ... print i
14 >>> g = f()
15 >>> g.next()
17 >>> g.next()
20 "Falling off the end" stops the generator:
22 >>> g.next()
23 Traceback (most recent call last):
24 File "<stdin>", line 1, in ?
25 File "<stdin>", line 2, in g
26 StopIteration
28 "return" also stops the generator:
30 >>> def f():
31 ... yield 1
32 ... return
33 ... yield 2 # never reached
34 ...
35 >>> g = f()
36 >>> g.next()
38 >>> g.next()
39 Traceback (most recent call last):
40 File "<stdin>", line 1, in ?
41 File "<stdin>", line 3, in f
42 StopIteration
43 >>> g.next() # once stopped, can't be resumed
44 Traceback (most recent call last):
45 File "<stdin>", line 1, in ?
46 StopIteration
48 "raise StopIteration" stops the generator too:
50 >>> def f():
51 ... yield 1
52 ... raise StopIteration
53 ... yield 2 # never reached
54 ...
55 >>> g = f()
56 >>> g.next()
58 >>> g.next()
59 Traceback (most recent call last):
60 File "<stdin>", line 1, in ?
61 StopIteration
62 >>> g.next()
63 Traceback (most recent call last):
64 File "<stdin>", line 1, in ?
65 StopIteration
67 However, they are not exactly equivalent:
69 >>> def g1():
70 ... try:
71 ... return
72 ... except:
73 ... yield 1
74 ...
75 >>> list(g1())
78 >>> def g2():
79 ... try:
80 ... raise StopIteration
81 ... except:
82 ... yield 42
83 >>> print list(g2())
84 [42]
86 This may be surprising at first:
88 >>> def g3():
89 ... try:
90 ... return
91 ... finally:
92 ... yield 1
93 ...
94 >>> list(g3())
95 [1]
97 Let's create an alternate range() function implemented as a generator:
99 >>> def yrange(n):
100 ... for i in range(n):
101 ... yield i
103 >>> list(yrange(5))
104 [0, 1, 2, 3, 4]
106 Generators always return to the most recent caller:
108 >>> def creator():
109 ... r = yrange(5)
110 ... print "creator", r.next()
111 ... return r
113 >>> def caller():
114 ... r = creator()
115 ... for i in r:
116 ... print "caller", i
118 >>> caller()
119 creator 0
120 caller 1
121 caller 2
122 caller 3
123 caller 4
125 Generators can call other generators:
127 >>> def zrange(n):
128 ... for i in yrange(n):
129 ... yield i
131 >>> list(zrange(5))
132 [0, 1, 2, 3, 4]
136 # The examples from PEP 255.
138 pep_tests = """
140 Specification: Yield
142 Restriction: A generator cannot be resumed while it is actively
143 running:
145 >>> def g():
146 ... i = me.next()
147 ... yield i
148 >>> me = g()
149 >>> me.next()
150 Traceback (most recent call last):
152 File "<string>", line 2, in g
153 ValueError: generator already executing
155 Specification: Return
157 Note that return isn't always equivalent to raising StopIteration: the
158 difference lies in how enclosing try/except constructs are treated.
159 For example,
161 >>> def f1():
162 ... try:
163 ... return
164 ... except:
165 ... yield 1
166 >>> print list(f1())
169 because, as in any function, return simply exits, but
171 >>> def f2():
172 ... try:
173 ... raise StopIteration
174 ... except:
175 ... yield 42
176 >>> print list(f2())
177 [42]
179 because StopIteration is captured by a bare "except", as is any
180 exception.
182 Specification: Generators and Exception Propagation
184 >>> def f():
185 ... return 1//0
186 >>> def g():
187 ... yield f() # the zero division exception propagates
188 ... yield 42 # and we'll never get here
189 >>> k = g()
190 >>> k.next()
191 Traceback (most recent call last):
192 File "<stdin>", line 1, in ?
193 File "<stdin>", line 2, in g
194 File "<stdin>", line 2, in f
195 ZeroDivisionError: integer division or modulo by zero
196 >>> k.next() # and the generator cannot be resumed
197 Traceback (most recent call last):
198 File "<stdin>", line 1, in ?
199 StopIteration
202 Specification: Try/Except/Finally
204 >>> def f():
205 ... try:
206 ... yield 1
207 ... try:
208 ... yield 2
209 ... 1//0
210 ... yield 3 # never get here
211 ... except ZeroDivisionError:
212 ... yield 4
213 ... yield 5
214 ... raise
215 ... except:
216 ... yield 6
217 ... yield 7 # the "raise" above stops this
218 ... except:
219 ... yield 8
220 ... yield 9
221 ... try:
222 ... x = 12
223 ... finally:
224 ... yield 10
225 ... yield 11
226 >>> print list(f())
227 [1, 2, 4, 5, 8, 9, 10, 11]
230 Guido's binary tree example.
232 >>> # A binary tree class.
233 >>> class Tree:
235 ... def __init__(self, label, left=None, right=None):
236 ... self.label = label
237 ... self.left = left
238 ... self.right = right
240 ... def __repr__(self, level=0, indent=" "):
241 ... s = level*indent + `self.label`
242 ... if self.left:
243 ... s = s + "\\n" + self.left.__repr__(level+1, indent)
244 ... if self.right:
245 ... s = s + "\\n" + self.right.__repr__(level+1, indent)
246 ... return s
248 ... def __iter__(self):
249 ... return inorder(self)
251 >>> # Create a Tree from a list.
252 >>> def tree(list):
253 ... n = len(list)
254 ... if n == 0:
255 ... return []
256 ... i = n // 2
257 ... return Tree(list[i], tree(list[:i]), tree(list[i+1:]))
259 >>> # Show it off: create a tree.
260 >>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
262 >>> # A recursive generator that generates Tree leaves in in-order.
263 >>> def inorder(t):
264 ... if t:
265 ... for x in inorder(t.left):
266 ... yield x
267 ... yield t.label
268 ... for x in inorder(t.right):
269 ... yield x
271 >>> # Show it off: create a tree.
272 ... t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
273 ... # Print the nodes of the tree in in-order.
274 ... for x in t:
275 ... print x,
276 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
278 >>> # A non-recursive generator.
279 >>> def inorder(node):
280 ... stack = []
281 ... while node:
282 ... while node.left:
283 ... stack.append(node)
284 ... node = node.left
285 ... yield node.label
286 ... while not node.right:
287 ... try:
288 ... node = stack.pop()
289 ... except IndexError:
290 ... return
291 ... yield node.label
292 ... node = node.right
294 >>> # Exercise the non-recursive generator.
295 >>> for x in t:
296 ... print x,
297 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
301 # Examples from Iterator-List and Python-Dev and c.l.py.
303 email_tests = """
305 The difference between yielding None and returning it.
307 >>> def g():
308 ... for i in range(3):
309 ... yield None
310 ... yield None
311 ... return
312 >>> list(g())
313 [None, None, None, None]
315 Ensure that explicitly raising StopIteration acts like any other exception
316 in try/except, not like a return.
318 >>> def g():
319 ... yield 1
320 ... try:
321 ... raise StopIteration
322 ... except:
323 ... yield 2
324 ... yield 3
325 >>> list(g())
326 [1, 2, 3]
328 Next one was posted to c.l.py.
330 >>> def gcomb(x, k):
331 ... "Generate all combinations of k elements from list x."
333 ... if k > len(x):
334 ... return
335 ... if k == 0:
336 ... yield []
337 ... else:
338 ... first, rest = x[0], x[1:]
339 ... # A combination does or doesn't contain first.
340 ... # If it does, the remainder is a k-1 comb of rest.
341 ... for c in gcomb(rest, k-1):
342 ... c.insert(0, first)
343 ... yield c
344 ... # If it doesn't contain first, it's a k comb of rest.
345 ... for c in gcomb(rest, k):
346 ... yield c
348 >>> seq = range(1, 5)
349 >>> for k in range(len(seq) + 2):
350 ... print "%d-combs of %s:" % (k, seq)
351 ... for c in gcomb(seq, k):
352 ... print " ", c
353 0-combs of [1, 2, 3, 4]:
355 1-combs of [1, 2, 3, 4]:
360 2-combs of [1, 2, 3, 4]:
361 [1, 2]
362 [1, 3]
363 [1, 4]
364 [2, 3]
365 [2, 4]
366 [3, 4]
367 3-combs of [1, 2, 3, 4]:
368 [1, 2, 3]
369 [1, 2, 4]
370 [1, 3, 4]
371 [2, 3, 4]
372 4-combs of [1, 2, 3, 4]:
373 [1, 2, 3, 4]
374 5-combs of [1, 2, 3, 4]:
376 From the Iterators list, about the types of these things.
378 >>> def g():
379 ... yield 1
381 >>> type(g)
382 <type 'function'>
383 >>> i = g()
384 >>> type(i)
385 <type 'generator'>
386 >>> [s for s in dir(i) if not s.startswith('_')]
387 ['gi_frame', 'gi_running', 'next']
388 >>> print i.next.__doc__
389 x.next() -> the next value, or raise StopIteration
390 >>> iter(i) is i
392 >>> import types
393 >>> isinstance(i, types.GeneratorType)
396 And more, added later.
398 >>> i.gi_running
400 >>> type(i.gi_frame)
401 <type 'frame'>
402 >>> i.gi_running = 42
403 Traceback (most recent call last):
405 TypeError: readonly attribute
406 >>> def g():
407 ... yield me.gi_running
408 >>> me = g()
409 >>> me.gi_running
411 >>> me.next()
413 >>> me.gi_running
416 A clever union-find implementation from c.l.py, due to David Eppstein.
417 Sent: Friday, June 29, 2001 12:16 PM
418 To: python-list@python.org
419 Subject: Re: PEP 255: Simple Generators
421 >>> class disjointSet:
422 ... def __init__(self, name):
423 ... self.name = name
424 ... self.parent = None
425 ... self.generator = self.generate()
427 ... def generate(self):
428 ... while not self.parent:
429 ... yield self
430 ... for x in self.parent.generator:
431 ... yield x
433 ... def find(self):
434 ... return self.generator.next()
436 ... def union(self, parent):
437 ... if self.parent:
438 ... raise ValueError("Sorry, I'm not a root!")
439 ... self.parent = parent
441 ... def __str__(self):
442 ... return self.name
444 >>> names = "ABCDEFGHIJKLM"
445 >>> sets = [disjointSet(name) for name in names]
446 >>> roots = sets[:]
448 >>> import random
449 >>> random.seed(42)
450 >>> while 1:
451 ... for s in sets:
452 ... print "%s->%s" % (s, s.find()),
453 ... print
454 ... if len(roots) > 1:
455 ... s1 = random.choice(roots)
456 ... roots.remove(s1)
457 ... s2 = random.choice(roots)
458 ... s1.union(s2)
459 ... print "merged", s1, "into", s2
460 ... else:
461 ... break
462 A->A B->B C->C D->D E->E F->F G->G H->H I->I J->J K->K L->L M->M
463 merged D into G
464 A->A B->B C->C D->G E->E F->F G->G H->H I->I J->J K->K L->L M->M
465 merged C into F
466 A->A B->B C->F D->G E->E F->F G->G H->H I->I J->J K->K L->L M->M
467 merged L into A
468 A->A B->B C->F D->G E->E F->F G->G H->H I->I J->J K->K L->A M->M
469 merged H into E
470 A->A B->B C->F D->G E->E F->F G->G H->E I->I J->J K->K L->A M->M
471 merged B into E
472 A->A B->E C->F D->G E->E F->F G->G H->E I->I J->J K->K L->A M->M
473 merged J into G
474 A->A B->E C->F D->G E->E F->F G->G H->E I->I J->G K->K L->A M->M
475 merged E into G
476 A->A B->G C->F D->G E->G F->F G->G H->G I->I J->G K->K L->A M->M
477 merged M into G
478 A->A B->G C->F D->G E->G F->F G->G H->G I->I J->G K->K L->A M->G
479 merged I into K
480 A->A B->G C->F D->G E->G F->F G->G H->G I->K J->G K->K L->A M->G
481 merged K into A
482 A->A B->G C->F D->G E->G F->F G->G H->G I->A J->G K->A L->A M->G
483 merged F into A
484 A->A B->G C->A D->G E->G F->A G->G H->G I->A J->G K->A L->A M->G
485 merged A into G
486 A->G B->G C->G D->G E->G F->G G->G H->G I->G J->G K->G L->G M->G
489 # Fun tests (for sufficiently warped notions of "fun").
491 fun_tests = """
493 Build up to a recursive Sieve of Eratosthenes generator.
495 >>> def firstn(g, n):
496 ... return [g.next() for i in range(n)]
498 >>> def intsfrom(i):
499 ... while 1:
500 ... yield i
501 ... i += 1
503 >>> firstn(intsfrom(5), 7)
504 [5, 6, 7, 8, 9, 10, 11]
506 >>> def exclude_multiples(n, ints):
507 ... for i in ints:
508 ... if i % n:
509 ... yield i
511 >>> firstn(exclude_multiples(3, intsfrom(1)), 6)
512 [1, 2, 4, 5, 7, 8]
514 >>> def sieve(ints):
515 ... prime = ints.next()
516 ... yield prime
517 ... not_divisible_by_prime = exclude_multiples(prime, ints)
518 ... for p in sieve(not_divisible_by_prime):
519 ... yield p
521 >>> primes = sieve(intsfrom(2))
522 >>> firstn(primes, 20)
523 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
526 Another famous problem: generate all integers of the form
527 2**i * 3**j * 5**k
528 in increasing order, where i,j,k >= 0. Trickier than it may look at first!
529 Try writing it without generators, and correctly, and without generating
530 3 internal results for each result output.
532 >>> def times(n, g):
533 ... for i in g:
534 ... yield n * i
535 >>> firstn(times(10, intsfrom(1)), 10)
536 [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
538 >>> def merge(g, h):
539 ... ng = g.next()
540 ... nh = h.next()
541 ... while 1:
542 ... if ng < nh:
543 ... yield ng
544 ... ng = g.next()
545 ... elif ng > nh:
546 ... yield nh
547 ... nh = h.next()
548 ... else:
549 ... yield ng
550 ... ng = g.next()
551 ... nh = h.next()
553 The following works, but is doing a whale of a lot of redundant work --
554 it's not clear how to get the internal uses of m235 to share a single
555 generator. Note that me_times2 (etc) each need to see every element in the
556 result sequence. So this is an example where lazy lists are more natural
557 (you can look at the head of a lazy list any number of times).
559 >>> def m235():
560 ... yield 1
561 ... me_times2 = times(2, m235())
562 ... me_times3 = times(3, m235())
563 ... me_times5 = times(5, m235())
564 ... for i in merge(merge(me_times2,
565 ... me_times3),
566 ... me_times5):
567 ... yield i
569 Don't print "too many" of these -- the implementation above is extremely
570 inefficient: each call of m235() leads to 3 recursive calls, and in
571 turn each of those 3 more, and so on, and so on, until we've descended
572 enough levels to satisfy the print stmts. Very odd: when I printed 5
573 lines of results below, this managed to screw up Win98's malloc in "the
574 usual" way, i.e. the heap grew over 4Mb so Win98 started fragmenting
575 address space, and it *looked* like a very slow leak.
577 >>> result = m235()
578 >>> for i in range(3):
579 ... print firstn(result, 15)
580 [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
581 [25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
582 [81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
584 Heh. Here's one way to get a shared list, complete with an excruciating
585 namespace renaming trick. The *pretty* part is that the times() and merge()
586 functions can be reused as-is, because they only assume their stream
587 arguments are iterable -- a LazyList is the same as a generator to times().
589 >>> class LazyList:
590 ... def __init__(self, g):
591 ... self.sofar = []
592 ... self.fetch = g.next
594 ... def __getitem__(self, i):
595 ... sofar, fetch = self.sofar, self.fetch
596 ... while i >= len(sofar):
597 ... sofar.append(fetch())
598 ... return sofar[i]
600 >>> def m235():
601 ... yield 1
602 ... # Gack: m235 below actually refers to a LazyList.
603 ... me_times2 = times(2, m235)
604 ... me_times3 = times(3, m235)
605 ... me_times5 = times(5, m235)
606 ... for i in merge(merge(me_times2,
607 ... me_times3),
608 ... me_times5):
609 ... yield i
611 Print as many of these as you like -- *this* implementation is memory-
612 efficient.
614 >>> m235 = LazyList(m235())
615 >>> for i in range(5):
616 ... print [m235[j] for j in range(15*i, 15*(i+1))]
617 [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
618 [25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
619 [81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
620 [200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]
621 [400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
624 Ye olde Fibonacci generator, LazyList style.
626 >>> def fibgen(a, b):
628 ... def sum(g, h):
629 ... while 1:
630 ... yield g.next() + h.next()
632 ... def tail(g):
633 ... g.next() # throw first away
634 ... for x in g:
635 ... yield x
637 ... yield a
638 ... yield b
639 ... for s in sum(iter(fib),
640 ... tail(iter(fib))):
641 ... yield s
643 >>> fib = LazyList(fibgen(1, 2))
644 >>> firstn(iter(fib), 17)
645 [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
648 # syntax_tests mostly provokes SyntaxErrors. Also fiddling with #if 0
649 # hackery.
651 syntax_tests = """
653 >>> def f():
654 ... return 22
655 ... yield 1
656 Traceback (most recent call last):
658 SyntaxError: 'return' with argument inside generator (<string>, line 2)
660 >>> def f():
661 ... yield 1
662 ... return 22
663 Traceback (most recent call last):
665 SyntaxError: 'return' with argument inside generator (<string>, line 3)
667 "return None" is not the same as "return" in a generator:
669 >>> def f():
670 ... yield 1
671 ... return None
672 Traceback (most recent call last):
674 SyntaxError: 'return' with argument inside generator (<string>, line 3)
676 This one is fine:
678 >>> def f():
679 ... yield 1
680 ... return
682 >>> def f():
683 ... try:
684 ... yield 1
685 ... finally:
686 ... pass
687 Traceback (most recent call last):
689 SyntaxError: 'yield' not allowed in a 'try' block with a 'finally' clause (<string>, line 3)
691 >>> def f():
692 ... try:
693 ... try:
694 ... 1//0
695 ... except ZeroDivisionError:
696 ... yield 666 # bad because *outer* try has finally
697 ... except:
698 ... pass
699 ... finally:
700 ... pass
701 Traceback (most recent call last):
703 SyntaxError: 'yield' not allowed in a 'try' block with a 'finally' clause (<string>, line 6)
705 But this is fine:
707 >>> def f():
708 ... try:
709 ... try:
710 ... yield 12
711 ... 1//0
712 ... except ZeroDivisionError:
713 ... yield 666
714 ... except:
715 ... try:
716 ... x = 12
717 ... finally:
718 ... yield 12
719 ... except:
720 ... return
721 >>> list(f())
722 [12, 666]
724 >>> def f():
725 ... yield
726 Traceback (most recent call last):
727 SyntaxError: invalid syntax
729 >>> def f():
730 ... if 0:
731 ... yield
732 Traceback (most recent call last):
733 SyntaxError: invalid syntax
735 >>> def f():
736 ... if 0:
737 ... yield 1
738 >>> type(f())
739 <type 'generator'>
741 >>> def f():
742 ... if "":
743 ... yield None
744 >>> type(f())
745 <type 'generator'>
747 >>> def f():
748 ... return
749 ... try:
750 ... if x==4:
751 ... pass
752 ... elif 0:
753 ... try:
754 ... 1//0
755 ... except SyntaxError:
756 ... pass
757 ... else:
758 ... if 0:
759 ... while 12:
760 ... x += 1
761 ... yield 2 # don't blink
762 ... f(a, b, c, d, e)
763 ... else:
764 ... pass
765 ... except:
766 ... x = 1
767 ... return
768 >>> type(f())
769 <type 'generator'>
771 >>> def f():
772 ... if 0:
773 ... def g():
774 ... yield 1
776 >>> type(f())
777 <type 'NoneType'>
779 >>> def f():
780 ... if 0:
781 ... class C:
782 ... def __init__(self):
783 ... yield 1
784 ... def f(self):
785 ... yield 2
786 >>> type(f())
787 <type 'NoneType'>
789 >>> def f():
790 ... if 0:
791 ... return
792 ... if 0:
793 ... yield 2
794 >>> type(f())
795 <type 'generator'>
798 >>> def f():
799 ... if 0:
800 ... lambda x: x # shouldn't trigger here
801 ... return # or here
802 ... def f(i):
803 ... return 2*i # or here
804 ... if 0:
805 ... return 3 # but *this* sucks (line 8)
806 ... if 0:
807 ... yield 2 # because it's a generator
808 Traceback (most recent call last):
809 SyntaxError: 'return' with argument inside generator (<string>, line 8)
812 # conjoin is a simple backtracking generator, named in honor of Icon's
813 # "conjunction" control structure. Pass a list of no-argument functions
814 # that return iterable objects. Easiest to explain by example: assume the
815 # function list [x, y, z] is passed. Then conjoin acts like:
817 # def g():
818 # values = [None] * 3
819 # for values[0] in x():
820 # for values[1] in y():
821 # for values[2] in z():
822 # yield values
824 # So some 3-lists of values *may* be generated, each time we successfully
825 # get into the innermost loop. If an iterator fails (is exhausted) before
826 # then, it "backtracks" to get the next value from the nearest enclosing
827 # iterator (the one "to the left"), and starts all over again at the next
828 # slot (pumps a fresh iterator). Of course this is most useful when the
829 # iterators have side-effects, so that which values *can* be generated at
830 # each slot depend on the values iterated at previous slots.
832 def conjoin(gs):
834 values = [None] * len(gs)
836 def gen(i, values=values):
837 if i >= len(gs):
838 yield values
839 else:
840 for values[i] in gs[i]():
841 for x in gen(i+1):
842 yield x
844 for x in gen(0):
845 yield x
847 # That works fine, but recursing a level and checking i against len(gs) for
848 # each item produced is inefficient. By doing manual loop unrolling across
849 # generator boundaries, it's possible to eliminate most of that overhead.
850 # This isn't worth the bother *in general* for generators, but conjoin() is
851 # a core building block for some CPU-intensive generator applications.
853 def conjoin(gs):
855 n = len(gs)
856 values = [None] * n
858 # Do one loop nest at time recursively, until the # of loop nests
859 # remaining is divisible by 3.
861 def gen(i, values=values):
862 if i >= n:
863 yield values
865 elif (n-i) % 3:
866 ip1 = i+1
867 for values[i] in gs[i]():
868 for x in gen(ip1):
869 yield x
871 else:
872 for x in _gen3(i):
873 yield x
875 # Do three loop nests at a time, recursing only if at least three more
876 # remain. Don't call directly: this is an internal optimization for
877 # gen's use.
879 def _gen3(i, values=values):
880 assert i < n and (n-i) % 3 == 0
881 ip1, ip2, ip3 = i+1, i+2, i+3
882 g, g1, g2 = gs[i : ip3]
884 if ip3 >= n:
885 # These are the last three, so we can yield values directly.
886 for values[i] in g():
887 for values[ip1] in g1():
888 for values[ip2] in g2():
889 yield values
891 else:
892 # At least 6 loop nests remain; peel off 3 and recurse for the
893 # rest.
894 for values[i] in g():
895 for values[ip1] in g1():
896 for values[ip2] in g2():
897 for x in _gen3(ip3):
898 yield x
900 for x in gen(0):
901 yield x
903 # And one more approach: For backtracking apps like the Knight's Tour
904 # solver below, the number of backtracking levels can be enormous (one
905 # level per square, for the Knight's Tour, so that e.g. a 100x100 board
906 # needs 10,000 levels). In such cases Python is likely to run out of
907 # stack space due to recursion. So here's a recursion-free version of
908 # conjoin too.
909 # NOTE WELL: This allows large problems to be solved with only trivial
910 # demands on stack space. Without explicitly resumable generators, this is
911 # much harder to achieve. OTOH, this is much slower (up to a factor of 2)
912 # than the fancy unrolled recursive conjoin.
914 def flat_conjoin(gs): # rename to conjoin to run tests with this instead
915 n = len(gs)
916 values = [None] * n
917 iters = [None] * n
918 _StopIteration = StopIteration # make local because caught a *lot*
919 i = 0
920 while 1:
921 # Descend.
922 try:
923 while i < n:
924 it = iters[i] = gs[i]().next
925 values[i] = it()
926 i += 1
927 except _StopIteration:
928 pass
929 else:
930 assert i == n
931 yield values
933 # Backtrack until an older iterator can be resumed.
934 i -= 1
935 while i >= 0:
936 try:
937 values[i] = iters[i]()
938 # Success! Start fresh at next level.
939 i += 1
940 break
941 except _StopIteration:
942 # Continue backtracking.
943 i -= 1
944 else:
945 assert i < 0
946 break
948 # A conjoin-based N-Queens solver.
950 class Queens:
951 def __init__(self, n):
952 self.n = n
953 rangen = range(n)
955 # Assign a unique int to each column and diagonal.
956 # columns: n of those, range(n).
957 # NW-SE diagonals: 2n-1 of these, i-j unique and invariant along
958 # each, smallest i-j is 0-(n-1) = 1-n, so add n-1 to shift to 0-
959 # based.
960 # NE-SW diagonals: 2n-1 of these, i+j unique and invariant along
961 # each, smallest i+j is 0, largest is 2n-2.
963 # For each square, compute a bit vector of the columns and
964 # diagonals it covers, and for each row compute a function that
965 # generates the possiblities for the columns in that row.
966 self.rowgenerators = []
967 for i in rangen:
968 rowuses = [(1L << j) | # column ordinal
969 (1L << (n + i-j + n-1)) | # NW-SE ordinal
970 (1L << (n + 2*n-1 + i+j)) # NE-SW ordinal
971 for j in rangen]
973 def rowgen(rowuses=rowuses):
974 for j in rangen:
975 uses = rowuses[j]
976 if uses & self.used == 0:
977 self.used |= uses
978 yield j
979 self.used &= ~uses
981 self.rowgenerators.append(rowgen)
983 # Generate solutions.
984 def solve(self):
985 self.used = 0
986 for row2col in conjoin(self.rowgenerators):
987 yield row2col
989 def printsolution(self, row2col):
990 n = self.n
991 assert n == len(row2col)
992 sep = "+" + "-+" * n
993 print sep
994 for i in range(n):
995 squares = [" " for j in range(n)]
996 squares[row2col[i]] = "Q"
997 print "|" + "|".join(squares) + "|"
998 print sep
1000 # A conjoin-based Knight's Tour solver. This is pretty sophisticated
1001 # (e.g., when used with flat_conjoin above, and passing hard=1 to the
1002 # constructor, a 200x200 Knight's Tour was found quickly -- note that we're
1003 # creating 10s of thousands of generators then!), and is lengthy.
1005 class Knights:
1006 def __init__(self, m, n, hard=0):
1007 self.m, self.n = m, n
1009 # solve() will set up succs[i] to be a list of square #i's
1010 # successors.
1011 succs = self.succs = []
1013 # Remove i0 from each of its successor's successor lists, i.e.
1014 # successors can't go back to i0 again. Return 0 if we can
1015 # detect this makes a solution impossible, else return 1.
1017 def remove_from_successors(i0, len=len):
1018 # If we remove all exits from a free square, we're dead:
1019 # even if we move to it next, we can't leave it again.
1020 # If we create a square with one exit, we must visit it next;
1021 # else somebody else will have to visit it, and since there's
1022 # only one adjacent, there won't be a way to leave it again.
1023 # Finelly, if we create more than one free square with a
1024 # single exit, we can only move to one of them next, leaving
1025 # the other one a dead end.
1026 ne0 = ne1 = 0
1027 for i in succs[i0]:
1028 s = succs[i]
1029 s.remove(i0)
1030 e = len(s)
1031 if e == 0:
1032 ne0 += 1
1033 elif e == 1:
1034 ne1 += 1
1035 return ne0 == 0 and ne1 < 2
1037 # Put i0 back in each of its successor's successor lists.
1039 def add_to_successors(i0):
1040 for i in succs[i0]:
1041 succs[i].append(i0)
1043 # Generate the first move.
1044 def first():
1045 if m < 1 or n < 1:
1046 return
1048 # Since we're looking for a cycle, it doesn't matter where we
1049 # start. Starting in a corner makes the 2nd move easy.
1050 corner = self.coords2index(0, 0)
1051 remove_from_successors(corner)
1052 self.lastij = corner
1053 yield corner
1054 add_to_successors(corner)
1056 # Generate the second moves.
1057 def second():
1058 corner = self.coords2index(0, 0)
1059 assert self.lastij == corner # i.e., we started in the corner
1060 if m < 3 or n < 3:
1061 return
1062 assert len(succs[corner]) == 2
1063 assert self.coords2index(1, 2) in succs[corner]
1064 assert self.coords2index(2, 1) in succs[corner]
1065 # Only two choices. Whichever we pick, the other must be the
1066 # square picked on move m*n, as it's the only way to get back
1067 # to (0, 0). Save its index in self.final so that moves before
1068 # the last know it must be kept free.
1069 for i, j in (1, 2), (2, 1):
1070 this = self.coords2index(i, j)
1071 final = self.coords2index(3-i, 3-j)
1072 self.final = final
1074 remove_from_successors(this)
1075 succs[final].append(corner)
1076 self.lastij = this
1077 yield this
1078 succs[final].remove(corner)
1079 add_to_successors(this)
1081 # Generate moves 3 thru m*n-1.
1082 def advance(len=len):
1083 # If some successor has only one exit, must take it.
1084 # Else favor successors with fewer exits.
1085 candidates = []
1086 for i in succs[self.lastij]:
1087 e = len(succs[i])
1088 assert e > 0, "else remove_from_successors() pruning flawed"
1089 if e == 1:
1090 candidates = [(e, i)]
1091 break
1092 candidates.append((e, i))
1093 else:
1094 candidates.sort()
1096 for e, i in candidates:
1097 if i != self.final:
1098 if remove_from_successors(i):
1099 self.lastij = i
1100 yield i
1101 add_to_successors(i)
1103 # Generate moves 3 thru m*n-1. Alternative version using a
1104 # stronger (but more expensive) heuristic to order successors.
1105 # Since the # of backtracking levels is m*n, a poor move early on
1106 # can take eons to undo. Smallest square board for which this
1107 # matters a lot is 52x52.
1108 def advance_hard(vmid=(m-1)/2.0, hmid=(n-1)/2.0, len=len):
1109 # If some successor has only one exit, must take it.
1110 # Else favor successors with fewer exits.
1111 # Break ties via max distance from board centerpoint (favor
1112 # corners and edges whenever possible).
1113 candidates = []
1114 for i in succs[self.lastij]:
1115 e = len(succs[i])
1116 assert e > 0, "else remove_from_successors() pruning flawed"
1117 if e == 1:
1118 candidates = [(e, 0, i)]
1119 break
1120 i1, j1 = self.index2coords(i)
1121 d = (i1 - vmid)**2 + (j1 - hmid)**2
1122 candidates.append((e, -d, i))
1123 else:
1124 candidates.sort()
1126 for e, d, i in candidates:
1127 if i != self.final:
1128 if remove_from_successors(i):
1129 self.lastij = i
1130 yield i
1131 add_to_successors(i)
1133 # Generate the last move.
1134 def last():
1135 assert self.final in succs[self.lastij]
1136 yield self.final
1138 if m*n < 4:
1139 self.squaregenerators = [first]
1140 else:
1141 self.squaregenerators = [first, second] + \
1142 [hard and advance_hard or advance] * (m*n - 3) + \
1143 [last]
1145 def coords2index(self, i, j):
1146 assert 0 <= i < self.m
1147 assert 0 <= j < self.n
1148 return i * self.n + j
1150 def index2coords(self, index):
1151 assert 0 <= index < self.m * self.n
1152 return divmod(index, self.n)
1154 def _init_board(self):
1155 succs = self.succs
1156 del succs[:]
1157 m, n = self.m, self.n
1158 c2i = self.coords2index
1160 offsets = [( 1, 2), ( 2, 1), ( 2, -1), ( 1, -2),
1161 (-1, -2), (-2, -1), (-2, 1), (-1, 2)]
1162 rangen = range(n)
1163 for i in range(m):
1164 for j in rangen:
1165 s = [c2i(i+io, j+jo) for io, jo in offsets
1166 if 0 <= i+io < m and
1167 0 <= j+jo < n]
1168 succs.append(s)
1170 # Generate solutions.
1171 def solve(self):
1172 self._init_board()
1173 for x in conjoin(self.squaregenerators):
1174 yield x
1176 def printsolution(self, x):
1177 m, n = self.m, self.n
1178 assert len(x) == m*n
1179 w = len(str(m*n))
1180 format = "%" + str(w) + "d"
1182 squares = [[None] * n for i in range(m)]
1183 k = 1
1184 for i in x:
1185 i1, j1 = self.index2coords(i)
1186 squares[i1][j1] = format % k
1187 k += 1
1189 sep = "+" + ("-" * w + "+") * n
1190 print sep
1191 for i in range(m):
1192 row = squares[i]
1193 print "|" + "|".join(row) + "|"
1194 print sep
1196 conjoin_tests = """
1198 Generate the 3-bit binary numbers in order. This illustrates dumbest-
1199 possible use of conjoin, just to generate the full cross-product.
1201 >>> for c in conjoin([lambda: iter((0, 1))] * 3):
1202 ... print c
1203 [0, 0, 0]
1204 [0, 0, 1]
1205 [0, 1, 0]
1206 [0, 1, 1]
1207 [1, 0, 0]
1208 [1, 0, 1]
1209 [1, 1, 0]
1210 [1, 1, 1]
1212 For efficiency in typical backtracking apps, conjoin() yields the same list
1213 object each time. So if you want to save away a full account of its
1214 generated sequence, you need to copy its results.
1216 >>> def gencopy(iterator):
1217 ... for x in iterator:
1218 ... yield x[:]
1220 >>> for n in range(10):
1221 ... all = list(gencopy(conjoin([lambda: iter((0, 1))] * n)))
1222 ... print n, len(all), all[0] == [0] * n, all[-1] == [1] * n
1223 0 1 1 1
1224 1 2 1 1
1225 2 4 1 1
1226 3 8 1 1
1227 4 16 1 1
1228 5 32 1 1
1229 6 64 1 1
1230 7 128 1 1
1231 8 256 1 1
1232 9 512 1 1
1234 And run an 8-queens solver.
1236 >>> q = Queens(8)
1237 >>> LIMIT = 2
1238 >>> count = 0
1239 >>> for row2col in q.solve():
1240 ... count += 1
1241 ... if count <= LIMIT:
1242 ... print "Solution", count
1243 ... q.printsolution(row2col)
1244 Solution 1
1245 +-+-+-+-+-+-+-+-+
1246 |Q| | | | | | | |
1247 +-+-+-+-+-+-+-+-+
1248 | | | | |Q| | | |
1249 +-+-+-+-+-+-+-+-+
1250 | | | | | | | |Q|
1251 +-+-+-+-+-+-+-+-+
1252 | | | | | |Q| | |
1253 +-+-+-+-+-+-+-+-+
1254 | | |Q| | | | | |
1255 +-+-+-+-+-+-+-+-+
1256 | | | | | | |Q| |
1257 +-+-+-+-+-+-+-+-+
1258 | |Q| | | | | | |
1259 +-+-+-+-+-+-+-+-+
1260 | | | |Q| | | | |
1261 +-+-+-+-+-+-+-+-+
1262 Solution 2
1263 +-+-+-+-+-+-+-+-+
1264 |Q| | | | | | | |
1265 +-+-+-+-+-+-+-+-+
1266 | | | | | |Q| | |
1267 +-+-+-+-+-+-+-+-+
1268 | | | | | | | |Q|
1269 +-+-+-+-+-+-+-+-+
1270 | | |Q| | | | | |
1271 +-+-+-+-+-+-+-+-+
1272 | | | | | | |Q| |
1273 +-+-+-+-+-+-+-+-+
1274 | | | |Q| | | | |
1275 +-+-+-+-+-+-+-+-+
1276 | |Q| | | | | | |
1277 +-+-+-+-+-+-+-+-+
1278 | | | | |Q| | | |
1279 +-+-+-+-+-+-+-+-+
1281 >>> print count, "solutions in all."
1282 92 solutions in all.
1284 And run a Knight's Tour on a 10x10 board. Note that there are about
1285 20,000 solutions even on a 6x6 board, so don't dare run this to exhaustion.
1287 >>> k = Knights(10, 10)
1288 >>> LIMIT = 2
1289 >>> count = 0
1290 >>> for x in k.solve():
1291 ... count += 1
1292 ... if count <= LIMIT:
1293 ... print "Solution", count
1294 ... k.printsolution(x)
1295 ... else:
1296 ... break
1297 Solution 1
1298 +---+---+---+---+---+---+---+---+---+---+
1299 | 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|
1300 +---+---+---+---+---+---+---+---+---+---+
1301 | 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|
1302 +---+---+---+---+---+---+---+---+---+---+
1303 | 59|100| 73| 36| 41| 56| 39| 32| 9| 6|
1304 +---+---+---+---+---+---+---+---+---+---+
1305 | 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|
1306 +---+---+---+---+---+---+---+---+---+---+
1307 | 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|
1308 +---+---+---+---+---+---+---+---+---+---+
1309 | 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|
1310 +---+---+---+---+---+---+---+---+---+---+
1311 | 87| 98| 91| 80| 77| 84| 53| 46| 65| 44|
1312 +---+---+---+---+---+---+---+---+---+---+
1313 | 90| 23| 88| 95| 70| 79| 68| 83| 14| 17|
1314 +---+---+---+---+---+---+---+---+---+---+
1315 | 97| 92| 21| 78| 81| 94| 19| 16| 45| 66|
1316 +---+---+---+---+---+---+---+---+---+---+
1317 | 22| 89| 96| 93| 20| 69| 82| 67| 18| 15|
1318 +---+---+---+---+---+---+---+---+---+---+
1319 Solution 2
1320 +---+---+---+---+---+---+---+---+---+---+
1321 | 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|
1322 +---+---+---+---+---+---+---+---+---+---+
1323 | 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|
1324 +---+---+---+---+---+---+---+---+---+---+
1325 | 59|100| 73| 36| 41| 56| 39| 32| 9| 6|
1326 +---+---+---+---+---+---+---+---+---+---+
1327 | 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|
1328 +---+---+---+---+---+---+---+---+---+---+
1329 | 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|
1330 +---+---+---+---+---+---+---+---+---+---+
1331 | 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|
1332 +---+---+---+---+---+---+---+---+---+---+
1333 | 87| 98| 89| 80| 77| 84| 53| 46| 65| 44|
1334 +---+---+---+---+---+---+---+---+---+---+
1335 | 90| 23| 92| 95| 70| 79| 68| 83| 14| 17|
1336 +---+---+---+---+---+---+---+---+---+---+
1337 | 97| 88| 21| 78| 81| 94| 19| 16| 45| 66|
1338 +---+---+---+---+---+---+---+---+---+---+
1339 | 22| 91| 96| 93| 20| 69| 82| 67| 18| 15|
1340 +---+---+---+---+---+---+---+---+---+---+
1343 __test__ = {"tut": tutorial_tests,
1344 "pep": pep_tests,
1345 "email": email_tests,
1346 "fun": fun_tests,
1347 "syntax": syntax_tests,
1348 "conjoin": conjoin_tests}
1350 # Magic test name that regrtest.py invokes *after* importing this module.
1351 # This worms around a bootstrap problem.
1352 # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
1353 # so this works as expected in both ways of running regrtest.
1354 def test_main(verbose=None):
1355 import doctest, test_support, test_generators
1356 if 0: # change to 1 to run forever (to check for leaks)
1357 while 1:
1358 doctest.master = None
1359 test_support.run_doctest(test_generators, verbose)
1360 print ".",
1361 else:
1362 test_support.run_doctest(test_generators, verbose)
1364 # This part isn't needed for regrtest, but for running the test directly.
1365 if __name__ == "__main__":
1366 test_main(1)