blob: 0e9d0607399d79100788731983a596f4ea46927f [file] [log] [blame]
Tim Peters5ba58662001-07-16 02:29:45 +00001from __future__ import generators
2
Tim Peters6ba5f792001-06-23 20:45:43 +00003tutorial_tests = """
Tim Peters1def3512001-06-23 20:27:04 +00004Let's try a simple generator:
5
6 >>> def f():
7 ... yield 1
8 ... yield 2
9
Tim Petersb9e9ff12001-06-24 03:44:52 +000010 >>> for i in f():
11 ... print i
12 1
13 2
Tim Peters1def3512001-06-23 20:27:04 +000014 >>> g = f()
15 >>> g.next()
16 1
17 >>> g.next()
18 2
Tim Petersea2e97a2001-06-24 07:10:02 +000019
Tim Peters2106ef02001-06-25 01:30:12 +000020"Falling off the end" stops the generator:
Tim Petersea2e97a2001-06-24 07:10:02 +000021
Tim Peters1def3512001-06-23 20:27:04 +000022 >>> g.next()
23 Traceback (most recent call last):
24 File "<stdin>", line 1, in ?
25 File "<stdin>", line 2, in g
26 StopIteration
27
Tim Petersea2e97a2001-06-24 07:10:02 +000028"return" also stops the generator:
Tim Peters1def3512001-06-23 20:27:04 +000029
30 >>> def f():
31 ... yield 1
32 ... return
33 ... yield 2 # never reached
34 ...
35 >>> g = f()
36 >>> g.next()
37 1
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
47
48"raise StopIteration" stops the generator too:
49
50 >>> def f():
51 ... yield 1
Tim Peters34463652001-07-12 22:43:41 +000052 ... raise StopIteration
Tim Peters1def3512001-06-23 20:27:04 +000053 ... yield 2 # never reached
54 ...
55 >>> g = f()
56 >>> g.next()
57 1
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
66
67However, they are not exactly equivalent:
68
69 >>> def g1():
70 ... try:
71 ... return
72 ... except:
73 ... yield 1
74 ...
75 >>> list(g1())
76 []
77
78 >>> def g2():
79 ... try:
80 ... raise StopIteration
81 ... except:
82 ... yield 42
83 >>> print list(g2())
84 [42]
85
86This may be surprising at first:
87
88 >>> def g3():
89 ... try:
90 ... return
91 ... finally:
92 ... yield 1
93 ...
94 >>> list(g3())
95 [1]
96
97Let's create an alternate range() function implemented as a generator:
98
99 >>> def yrange(n):
100 ... for i in range(n):
101 ... yield i
102 ...
103 >>> list(yrange(5))
104 [0, 1, 2, 3, 4]
105
106Generators always return to the most recent caller:
107
108 >>> def creator():
109 ... r = yrange(5)
110 ... print "creator", r.next()
111 ... return r
112 ...
113 >>> def caller():
114 ... r = creator()
115 ... for i in r:
116 ... print "caller", i
117 ...
118 >>> caller()
119 creator 0
120 caller 1
121 caller 2
122 caller 3
123 caller 4
124
125Generators can call other generators:
126
127 >>> def zrange(n):
128 ... for i in yrange(n):
129 ... yield i
130 ...
131 >>> list(zrange(5))
132 [0, 1, 2, 3, 4]
133
134"""
135
Tim Peters6ba5f792001-06-23 20:45:43 +0000136# The examples from PEP 255.
137
138pep_tests = """
139
Tim Peterse5614632001-08-15 04:41:19 +0000140Specification: Yield
141
142 Restriction: A generator cannot be resumed while it is actively
143 running:
144
145 >>> def g():
146 ... i = me.next()
147 ... yield i
148 >>> me = g()
149 >>> me.next()
150 Traceback (most recent call last):
151 ...
152 File "<string>", line 2, in g
153 ValueError: generator already executing
154
Tim Peters6ba5f792001-06-23 20:45:43 +0000155Specification: Return
156
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,
160
161 >>> def f1():
162 ... try:
163 ... return
164 ... except:
165 ... yield 1
166 >>> print list(f1())
167 []
168
169 because, as in any function, return simply exits, but
170
171 >>> def f2():
172 ... try:
173 ... raise StopIteration
174 ... except:
175 ... yield 42
176 >>> print list(f2())
177 [42]
178
179 because StopIteration is captured by a bare "except", as is any
180 exception.
181
182Specification: Generators and Exception Propagation
183
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
200 >>>
201
202Specification: Try/Except/Finally
203
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]
228 >>>
229
Tim Peters6ba5f792001-06-23 20:45:43 +0000230Guido's binary tree example.
231
232 >>> # A binary tree class.
233 >>> class Tree:
234 ...
235 ... def __init__(self, label, left=None, right=None):
236 ... self.label = label
237 ... self.left = left
238 ... self.right = right
239 ...
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
247 ...
248 ... def __iter__(self):
249 ... return inorder(self)
250
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:]))
258
259 >>> # Show it off: create a tree.
260 >>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
261
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
270
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
277
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
293
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
298
299"""
300
Tim Petersb2bc6a92001-06-24 10:14:27 +0000301# Examples from Iterator-List and Python-Dev and c.l.py.
Tim Peters6ba5f792001-06-23 20:45:43 +0000302
303email_tests = """
304
305The difference between yielding None and returning it.
306
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]
314
315Ensure that explicitly raising StopIteration acts like any other exception
316in try/except, not like a return.
317
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]
Tim Petersb9e9ff12001-06-24 03:44:52 +0000327
Tim Petersb2bc6a92001-06-24 10:14:27 +0000328Next one was posted to c.l.py.
329
330>>> def gcomb(x, k):
331... "Generate all combinations of k elements from list x."
332...
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
347
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
3530-combs of [1, 2, 3, 4]:
354 []
3551-combs of [1, 2, 3, 4]:
356 [1]
357 [2]
358 [3]
359 [4]
3602-combs of [1, 2, 3, 4]:
361 [1, 2]
362 [1, 3]
363 [1, 4]
364 [2, 3]
365 [2, 4]
366 [3, 4]
3673-combs of [1, 2, 3, 4]:
368 [1, 2, 3]
369 [1, 2, 4]
370 [1, 3, 4]
371 [2, 3, 4]
3724-combs of [1, 2, 3, 4]:
373 [1, 2, 3, 4]
3745-combs of [1, 2, 3, 4]:
Tim Peters3e7b1a02001-06-25 19:46:25 +0000375
Tim Peterse77f2e22001-06-26 22:24:51 +0000376From the Iterators list, about the types of these things.
Tim Peters3e7b1a02001-06-25 19:46:25 +0000377
378>>> def g():
379... yield 1
380...
381>>> type(g)
382<type 'function'>
383>>> i = g()
384>>> type(i)
385<type 'generator'>
Tim Peters5d2b77c2001-09-03 05:47:38 +0000386>>> [s for s in dir(i) if not s.startswith('_')]
Tim Peterse77f2e22001-06-26 22:24:51 +0000387['gi_frame', 'gi_running', 'next']
Tim Peters3e7b1a02001-06-25 19:46:25 +0000388>>> print i.next.__doc__
Tim Peters6d6c1a32001-08-02 04:15:00 +0000389x.next() -> the next value, or raise StopIteration
Tim Peters3e7b1a02001-06-25 19:46:25 +0000390>>> iter(i) is i
3911
392>>> import types
393>>> isinstance(i, types.GeneratorType)
3941
Tim Peterse77f2e22001-06-26 22:24:51 +0000395
396And more, added later.
397
398>>> i.gi_running
3990
400>>> type(i.gi_frame)
401<type 'frame'>
402>>> i.gi_running = 42
403Traceback (most recent call last):
404 ...
Guido van Rossum61cf7802001-08-10 21:25:24 +0000405TypeError: readonly attribute
Tim Peterse77f2e22001-06-26 22:24:51 +0000406>>> def g():
407... yield me.gi_running
408>>> me = g()
409>>> me.gi_running
4100
411>>> me.next()
4121
413>>> me.gi_running
4140
Tim Peters35302662001-07-02 01:38:33 +0000415
416A clever union-find implementation from c.l.py, due to David Eppstein.
417Sent: Friday, June 29, 2001 12:16 PM
418To: python-list@python.org
419Subject: Re: PEP 255: Simple Generators
420
421>>> class disjointSet:
422... def __init__(self, name):
423... self.name = name
424... self.parent = None
425... self.generator = self.generate()
426...
427... def generate(self):
428... while not self.parent:
429... yield self
430... for x in self.parent.generator:
431... yield x
432...
433... def find(self):
434... return self.generator.next()
435...
436... def union(self, parent):
437... if self.parent:
438... raise ValueError("Sorry, I'm not a root!")
439... self.parent = parent
440...
441... def __str__(self):
442... return self.name
Tim Peters35302662001-07-02 01:38:33 +0000443
444>>> names = "ABCDEFGHIJKLM"
445>>> sets = [disjointSet(name) for name in names]
446>>> roots = sets[:]
447
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
462A->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
463merged D into G
464A->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
465merged C into F
466A->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
467merged L into A
468A->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
469merged H into E
470A->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
471merged B into E
472A->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
473merged J into G
474A->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
475merged E into G
476A->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
477merged M into G
478A->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
479merged I into K
480A->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
481merged K into A
482A->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
483merged F into A
484A->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
485merged A into G
486A->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
Tim Peters6ba5f792001-06-23 20:45:43 +0000487"""
488
Tim Peters0f9da0a2001-06-23 21:01:47 +0000489# Fun tests (for sufficiently warped notions of "fun").
490
491fun_tests = """
492
493Build up to a recursive Sieve of Eratosthenes generator.
494
495>>> def firstn(g, n):
496... return [g.next() for i in range(n)]
497
498>>> def intsfrom(i):
499... while 1:
500... yield i
501... i += 1
502
503>>> firstn(intsfrom(5), 7)
504[5, 6, 7, 8, 9, 10, 11]
505
506>>> def exclude_multiples(n, ints):
507... for i in ints:
508... if i % n:
509... yield i
510
511>>> firstn(exclude_multiples(3, intsfrom(1)), 6)
512[1, 2, 4, 5, 7, 8]
513
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
520
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]
Tim Petersb9e9ff12001-06-24 03:44:52 +0000524
Tim Petersf6ed0742001-06-27 07:17:57 +0000525
Tim Petersb9e9ff12001-06-24 03:44:52 +0000526Another famous problem: generate all integers of the form
527 2**i * 3**j * 5**k
528in increasing order, where i,j,k >= 0. Trickier than it may look at first!
529Try writing it without generators, and correctly, and without generating
5303 internal results for each result output.
531
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]
537
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()
552
Tim Petersf6ed0742001-06-27 07:17:57 +0000553The following works, but is doing a whale of a lot of redundant work --
554it's not clear how to get the internal uses of m235 to share a single
555generator. Note that me_times2 (etc) each need to see every element in the
556result 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).
Tim Petersb9e9ff12001-06-24 03:44:52 +0000558
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
568
Tim Petersf6ed0742001-06-27 07:17:57 +0000569Don't print "too many" of these -- the implementation above is extremely
570inefficient: each call of m235() leads to 3 recursive calls, and in
571turn each of those 3 more, and so on, and so on, until we've descended
572enough levels to satisfy the print stmts. Very odd: when I printed 5
573lines of results below, this managed to screw up Win98's malloc in "the
574usual" way, i.e. the heap grew over 4Mb so Win98 started fragmenting
575address space, and it *looked* like a very slow leak.
576
Tim Petersb9e9ff12001-06-24 03:44:52 +0000577>>> result = m235()
Tim Petersf6ed0742001-06-27 07:17:57 +0000578>>> for i in range(3):
Tim Petersb9e9ff12001-06-24 03:44:52 +0000579... 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]
Tim Petersee309272001-06-24 05:47:06 +0000583
584Heh. Here's one way to get a shared list, complete with an excruciating
585namespace renaming trick. The *pretty* part is that the times() and merge()
586functions can be reused as-is, because they only assume their stream
587arguments are iterable -- a LazyList is the same as a generator to times().
588
589>>> class LazyList:
590... def __init__(self, g):
591... self.sofar = []
592... self.fetch = g.next
593...
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]
599
600>>> def m235():
601... yield 1
Tim Petersea2e97a2001-06-24 07:10:02 +0000602... # Gack: m235 below actually refers to a LazyList.
Tim Petersee309272001-06-24 05:47:06 +0000603... 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
610
Tim Petersf6ed0742001-06-27 07:17:57 +0000611Print as many of these as you like -- *this* implementation is memory-
Neil Schemenauerb20e9db2001-07-12 13:26:41 +0000612efficient.
Tim Petersf6ed0742001-06-27 07:17:57 +0000613
Tim Petersee309272001-06-24 05:47:06 +0000614>>> 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]
Tim Petersf6ed0742001-06-27 07:17:57 +0000622
Tim Petersf6ed0742001-06-27 07:17:57 +0000623
624Ye olde Fibonacci generator, LazyList style.
625
626>>> def fibgen(a, b):
627...
628... def sum(g, h):
629... while 1:
630... yield g.next() + h.next()
631...
632... def tail(g):
633... g.next() # throw first away
634... for x in g:
635... yield x
636...
637... yield a
638... yield b
639... for s in sum(iter(fib),
640... tail(iter(fib))):
641... yield s
642
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]
Tim Peters0f9da0a2001-06-23 21:01:47 +0000646"""
647
Tim Petersb6c3cea2001-06-26 03:36:28 +0000648# syntax_tests mostly provokes SyntaxErrors. Also fiddling with #if 0
649# hackery.
Tim Petersee309272001-06-24 05:47:06 +0000650
Tim Petersea2e97a2001-06-24 07:10:02 +0000651syntax_tests = """
652
653>>> def f():
654... return 22
655... yield 1
656Traceback (most recent call last):
657 ...
658SyntaxError: 'return' with argument inside generator (<string>, line 2)
659
660>>> def f():
661... yield 1
662... return 22
663Traceback (most recent call last):
664 ...
665SyntaxError: 'return' with argument inside generator (<string>, line 3)
666
667"return None" is not the same as "return" in a generator:
668
669>>> def f():
670... yield 1
671... return None
672Traceback (most recent call last):
673 ...
674SyntaxError: 'return' with argument inside generator (<string>, line 3)
675
676This one is fine:
677
678>>> def f():
679... yield 1
680... return
681
682>>> def f():
683... try:
684... yield 1
685... finally:
686... pass
687Traceback (most recent call last):
688 ...
689SyntaxError: 'yield' not allowed in a 'try' block with a 'finally' clause (<string>, line 3)
690
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
701Traceback (most recent call last):
702 ...
703SyntaxError: 'yield' not allowed in a 'try' block with a 'finally' clause (<string>, line 6)
704
705But this is fine:
706
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]
Tim Petersb6c3cea2001-06-26 03:36:28 +0000723
724>>> def f():
Tim Peters08a898f2001-06-28 01:52:22 +0000725... yield
726Traceback (most recent call last):
727SyntaxError: invalid syntax
728
729>>> def f():
730... if 0:
731... yield
732Traceback (most recent call last):
733SyntaxError: invalid syntax
734
735>>> def f():
Tim Petersb6c3cea2001-06-26 03:36:28 +0000736... if 0:
737... yield 1
738>>> type(f())
739<type 'generator'>
740
741>>> def f():
742... if "":
743... yield None
744>>> type(f())
745<type 'generator'>
746
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'>
770
771>>> def f():
772... if 0:
773... def g():
774... yield 1
775...
776>>> type(f())
Guido van Rossum297abad2001-08-16 08:32:39 +0000777<type 'NoneType'>
Tim Petersb6c3cea2001-06-26 03:36:28 +0000778
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())
Guido van Rossum297abad2001-08-16 08:32:39 +0000787<type 'NoneType'>
Tim Peters08a898f2001-06-28 01:52:22 +0000788
789>>> def f():
790... if 0:
791... return
792... if 0:
793... yield 2
794>>> type(f())
795<type 'generator'>
796
797
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
808Traceback (most recent call last):
809SyntaxError: 'return' with argument inside generator (<string>, line 8)
Tim Petersea2e97a2001-06-24 07:10:02 +0000810"""
811
Tim Petersbe4f0a72001-06-29 02:41:16 +0000812# 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:
816#
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
823#
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.
831
832def conjoin(gs):
833
834 values = [None] * len(gs)
835
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
843
844 for x in gen(0):
845 yield x
846
Tim Petersc468fd22001-06-30 07:29:44 +0000847# 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.
852
853def conjoin(gs):
854
855 n = len(gs)
856 values = [None] * n
857
858 # Do one loop nest at time recursively, until the # of loop nests
859 # remaining is divisible by 3.
860
861 def gen(i, values=values):
862 if i >= n:
863 yield values
864
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
870
871 else:
872 for x in _gen3(i):
873 yield x
874
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.
878
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]
883
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
890
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
899
900 for x in gen(0):
901 yield x
902
unknown31569562001-07-04 22:11:22 +0000903# 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
Tim Peters9a8c8e22001-07-13 09:12:12 +0000911# much harder to achieve. OTOH, this is much slower (up to a factor of 2)
912# than the fancy unrolled recursive conjoin.
unknown31569562001-07-04 22:11:22 +0000913
914def flat_conjoin(gs): # rename to conjoin to run tests with this instead
915 n = len(gs)
916 values = [None] * n
917 iters = [None] * n
Tim Peters9a8c8e22001-07-13 09:12:12 +0000918 _StopIteration = StopIteration # make local because caught a *lot*
unknown31569562001-07-04 22:11:22 +0000919 i = 0
Tim Peters9a8c8e22001-07-13 09:12:12 +0000920 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
unknown31569562001-07-04 22:11:22 +0000929 else:
Tim Peters9a8c8e22001-07-13 09:12:12 +0000930 assert i == n
931 yield values
unknown31569562001-07-04 22:11:22 +0000932
Tim Peters9a8c8e22001-07-13 09:12:12 +0000933 # Backtrack until an older iterator can be resumed.
934 i -= 1
unknown31569562001-07-04 22:11:22 +0000935 while i >= 0:
936 try:
937 values[i] = iters[i]()
Tim Peters9a8c8e22001-07-13 09:12:12 +0000938 # Success! Start fresh at next level.
unknown31569562001-07-04 22:11:22 +0000939 i += 1
940 break
Tim Peters9a8c8e22001-07-13 09:12:12 +0000941 except _StopIteration:
942 # Continue backtracking.
943 i -= 1
944 else:
945 assert i < 0
946 break
unknown31569562001-07-04 22:11:22 +0000947
Tim Petersbe4f0a72001-06-29 02:41:16 +0000948# A conjoin-based N-Queens solver.
949
950class Queens:
951 def __init__(self, n):
952 self.n = n
953 rangen = range(n)
954
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.
962
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]
972
973 def rowgen(rowuses=rowuses):
974 for j in rangen:
975 uses = rowuses[j]
Tim Petersc468fd22001-06-30 07:29:44 +0000976 if uses & self.used == 0:
977 self.used |= uses
978 yield j
979 self.used &= ~uses
Tim Petersbe4f0a72001-06-29 02:41:16 +0000980
981 self.rowgenerators.append(rowgen)
982
983 # Generate solutions.
984 def solve(self):
985 self.used = 0
986 for row2col in conjoin(self.rowgenerators):
987 yield row2col
988
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
999
unknown31569562001-07-04 22:11:22 +00001000# 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
Tim Peters9a8c8e22001-07-13 09:12:12 +00001003# creating 10s of thousands of generators then!), and is lengthy.
unknown31569562001-07-04 22:11:22 +00001004
1005class Knights:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001006 def __init__(self, m, n, hard=0):
1007 self.m, self.n = m, n
unknown31569562001-07-04 22:11:22 +00001008
Tim Peters9a8c8e22001-07-13 09:12:12 +00001009 # solve() will set up succs[i] to be a list of square #i's
1010 # successors.
1011 succs = self.succs = []
unknown31569562001-07-04 22:11:22 +00001012
Tim Peters9a8c8e22001-07-13 09:12:12 +00001013 # 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.
unknown31569562001-07-04 22:11:22 +00001016
Tim Peters9a8c8e22001-07-13 09:12:12 +00001017 def remove_from_successors(i0, len=len):
unknown31569562001-07-04 22:11:22 +00001018 # 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]:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001028 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
unknown31569562001-07-04 22:11:22 +00001035 return ne0 == 0 and ne1 < 2
1036
Tim Peters9a8c8e22001-07-13 09:12:12 +00001037 # Put i0 back in each of its successor's successor lists.
1038
1039 def add_to_successors(i0):
unknown31569562001-07-04 22:11:22 +00001040 for i in succs[i0]:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001041 succs[i].append(i0)
unknown31569562001-07-04 22:11:22 +00001042
1043 # Generate the first move.
1044 def first():
Tim Peters9a8c8e22001-07-13 09:12:12 +00001045 if m < 1 or n < 1:
unknown31569562001-07-04 22:11:22 +00001046 return
1047
unknown31569562001-07-04 22:11:22 +00001048 # Since we're looking for a cycle, it doesn't matter where we
1049 # start. Starting in a corner makes the 2nd move easy.
Tim Peters9a8c8e22001-07-13 09:12:12 +00001050 corner = self.coords2index(0, 0)
1051 remove_from_successors(corner)
unknown31569562001-07-04 22:11:22 +00001052 self.lastij = corner
1053 yield corner
Tim Peters9a8c8e22001-07-13 09:12:12 +00001054 add_to_successors(corner)
unknown31569562001-07-04 22:11:22 +00001055
1056 # Generate the second moves.
1057 def second():
Tim Peters9a8c8e22001-07-13 09:12:12 +00001058 corner = self.coords2index(0, 0)
unknown31569562001-07-04 22:11:22 +00001059 assert self.lastij == corner # i.e., we started in the corner
Tim Peters9a8c8e22001-07-13 09:12:12 +00001060 if m < 3 or n < 3:
unknown31569562001-07-04 22:11:22 +00001061 return
Tim Peters9a8c8e22001-07-13 09:12:12 +00001062 assert len(succs[corner]) == 2
1063 assert self.coords2index(1, 2) in succs[corner]
1064 assert self.coords2index(2, 1) in succs[corner]
unknown31569562001-07-04 22:11:22 +00001065 # Only two choices. Whichever we pick, the other must be the
Tim Peters9a8c8e22001-07-13 09:12:12 +00001066 # square picked on move m*n, as it's the only way to get back
unknown31569562001-07-04 22:11:22 +00001067 # 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):
Tim Peters9a8c8e22001-07-13 09:12:12 +00001070 this = self.coords2index(i, j)
1071 final = self.coords2index(3-i, 3-j)
unknown31569562001-07-04 22:11:22 +00001072 self.final = final
unknown31569562001-07-04 22:11:22 +00001073
Tim Peters9a8c8e22001-07-13 09:12:12 +00001074 remove_from_successors(this)
1075 succs[final].append(corner)
unknown31569562001-07-04 22:11:22 +00001076 self.lastij = this
1077 yield this
Tim Peters9a8c8e22001-07-13 09:12:12 +00001078 succs[final].remove(corner)
1079 add_to_successors(this)
unknown31569562001-07-04 22:11:22 +00001080
Tim Peters9a8c8e22001-07-13 09:12:12 +00001081 # Generate moves 3 thru m*n-1.
1082 def advance(len=len):
unknown31569562001-07-04 22:11:22 +00001083 # 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]:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001087 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))
unknown31569562001-07-04 22:11:22 +00001093 else:
1094 candidates.sort()
1095
1096 for e, i in candidates:
1097 if i != self.final:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001098 if remove_from_successors(i):
unknown31569562001-07-04 22:11:22 +00001099 self.lastij = i
1100 yield i
Tim Peters9a8c8e22001-07-13 09:12:12 +00001101 add_to_successors(i)
unknown31569562001-07-04 22:11:22 +00001102
Tim Peters9a8c8e22001-07-13 09:12:12 +00001103 # Generate moves 3 thru m*n-1. Alternative version using a
unknown31569562001-07-04 22:11:22 +00001104 # stronger (but more expensive) heuristic to order successors.
Tim Peters9a8c8e22001-07-13 09:12:12 +00001105 # 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):
unknown31569562001-07-04 22:11:22 +00001109 # 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]:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001115 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))
unknown31569562001-07-04 22:11:22 +00001123 else:
1124 candidates.sort()
1125
1126 for e, d, i in candidates:
1127 if i != self.final:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001128 if remove_from_successors(i):
unknown31569562001-07-04 22:11:22 +00001129 self.lastij = i
1130 yield i
Tim Peters9a8c8e22001-07-13 09:12:12 +00001131 add_to_successors(i)
unknown31569562001-07-04 22:11:22 +00001132
1133 # Generate the last move.
1134 def last():
1135 assert self.final in succs[self.lastij]
1136 yield self.final
1137
Tim Peters9a8c8e22001-07-13 09:12:12 +00001138 if m*n < 4:
1139 self.squaregenerators = [first]
unknown31569562001-07-04 22:11:22 +00001140 else:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001141 self.squaregenerators = [first, second] + \
1142 [hard and advance_hard or advance] * (m*n - 3) + \
unknown31569562001-07-04 22:11:22 +00001143 [last]
1144
Tim Peters9a8c8e22001-07-13 09:12:12 +00001145 def coords2index(self, i, j):
1146 assert 0 <= i < self.m
1147 assert 0 <= j < self.n
1148 return i * self.n + j
1149
1150 def index2coords(self, index):
1151 assert 0 <= index < self.m * self.n
1152 return divmod(index, self.n)
1153
1154 def _init_board(self):
1155 succs = self.succs
1156 del succs[:]
1157 m, n = self.m, self.n
1158 c2i = self.coords2index
1159
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)
1169
unknown31569562001-07-04 22:11:22 +00001170 # Generate solutions.
1171 def solve(self):
Tim Peters9a8c8e22001-07-13 09:12:12 +00001172 self._init_board()
1173 for x in conjoin(self.squaregenerators):
unknown31569562001-07-04 22:11:22 +00001174 yield x
1175
1176 def printsolution(self, x):
Tim Peters9a8c8e22001-07-13 09:12:12 +00001177 m, n = self.m, self.n
1178 assert len(x) == m*n
1179 w = len(str(m*n))
unknown31569562001-07-04 22:11:22 +00001180 format = "%" + str(w) + "d"
1181
Tim Peters9a8c8e22001-07-13 09:12:12 +00001182 squares = [[None] * n for i in range(m)]
unknown31569562001-07-04 22:11:22 +00001183 k = 1
1184 for i in x:
Tim Peters9a8c8e22001-07-13 09:12:12 +00001185 i1, j1 = self.index2coords(i)
unknown31569562001-07-04 22:11:22 +00001186 squares[i1][j1] = format % k
1187 k += 1
1188
1189 sep = "+" + ("-" * w + "+") * n
1190 print sep
Tim Peters9a8c8e22001-07-13 09:12:12 +00001191 for i in range(m):
unknown31569562001-07-04 22:11:22 +00001192 row = squares[i]
1193 print "|" + "|".join(row) + "|"
1194 print sep
1195
Tim Petersbe4f0a72001-06-29 02:41:16 +00001196conjoin_tests = """
1197
1198Generate the 3-bit binary numbers in order. This illustrates dumbest-
1199possible use of conjoin, just to generate the full cross-product.
1200
unknown31569562001-07-04 22:11:22 +00001201>>> for c in conjoin([lambda: iter((0, 1))] * 3):
Tim Petersbe4f0a72001-06-29 02:41:16 +00001202... 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]
1211
Tim Petersc468fd22001-06-30 07:29:44 +00001212For efficiency in typical backtracking apps, conjoin() yields the same list
1213object each time. So if you want to save away a full account of its
1214generated sequence, you need to copy its results.
1215
1216>>> def gencopy(iterator):
1217... for x in iterator:
1218... yield x[:]
1219
1220>>> for n in range(10):
unknown31569562001-07-04 22:11:22 +00001221... all = list(gencopy(conjoin([lambda: iter((0, 1))] * n)))
Tim Petersc468fd22001-06-30 07:29:44 +00001222... print n, len(all), all[0] == [0] * n, all[-1] == [1] * n
12230 1 1 1
12241 2 1 1
12252 4 1 1
12263 8 1 1
12274 16 1 1
12285 32 1 1
12296 64 1 1
12307 128 1 1
12318 256 1 1
12329 512 1 1
1233
Tim Petersbe4f0a72001-06-29 02:41:16 +00001234And run an 8-queens solver.
1235
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)
1244Solution 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+-+-+-+-+-+-+-+-+
1262Solution 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+-+-+-+-+-+-+-+-+
1280
1281>>> print count, "solutions in all."
128292 solutions in all.
unknown31569562001-07-04 22:11:22 +00001283
1284And run a Knight's Tour on a 10x10 board. Note that there are about
128520,000 solutions even on a 6x6 board, so don't dare run this to exhaustion.
1286
Tim Peters9a8c8e22001-07-13 09:12:12 +00001287>>> k = Knights(10, 10)
unknown31569562001-07-04 22:11:22 +00001288>>> 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
1297Solution 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+---+---+---+---+---+---+---+---+---+---+
1319Solution 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+---+---+---+---+---+---+---+---+---+---+
Tim Petersbe4f0a72001-06-29 02:41:16 +00001341"""
1342
Tim Petersf6ed0742001-06-27 07:17:57 +00001343__test__ = {"tut": tutorial_tests,
1344 "pep": pep_tests,
1345 "email": email_tests,
1346 "fun": fun_tests,
Tim Petersbe4f0a72001-06-29 02:41:16 +00001347 "syntax": syntax_tests,
1348 "conjoin": conjoin_tests}
Tim Peters1def3512001-06-23 20:27:04 +00001349
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.
1354def test_main():
1355 import doctest, test_generators
Tim Petersa1d54552001-07-12 22:55:42 +00001356 if 0: # change to 1 to run forever (to check for leaks)
1357 while 1:
Tim Peters2106ef02001-06-25 01:30:12 +00001358 doctest.master = None
1359 doctest.testmod(test_generators)
Tim Petersa1d54552001-07-12 22:55:42 +00001360 print ".",
Tim Peters2106ef02001-06-25 01:30:12 +00001361 else:
1362 doctest.testmod(test_generators)
Tim Peters1def3512001-06-23 20:27:04 +00001363
1364# This part isn't needed for regrtest, but for running the test directly.
1365if __name__ == "__main__":
1366 test_main()