summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_generators.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-06-24 05:47:06 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-06-24 05:47:06 (GMT)
commitee30927b45385f0027b7a28e676efbb5bd239769 (patch)
treedb84e66b2b8fd27967ee0cad3d06ce7d9cf9b843 /Lib/test/test_generators.py
parent7e82b9cc6b6e18b9e186f29ca9cc30693011a2a3 (diff)
downloadcpython-ee30927b45385f0027b7a28e676efbb5bd239769.zip
cpython-ee30927b45385f0027b7a28e676efbb5bd239769.tar.gz
cpython-ee30927b45385f0027b7a28e676efbb5bd239769.tar.bz2
Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Good news: Some of this stuff is pretty sophisticated (read nuts), and I haven't bumped into a bug yet. Bad news: If I run the doctest in an infinite loop, memory is clearly leaking.
Diffstat (limited to 'Lib/test/test_generators.py')
-rw-r--r--Lib/test/test_generators.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index f3cba56..5ddffdf 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -405,8 +405,45 @@ can look at the head of a lazy list any number of times).
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
[200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]
[400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
+
+Heh. Here's one way to get a shared list, complete with an excruciating
+namespace renaming trick. The *pretty* part is that the times() and merge()
+functions can be reused as-is, because they only assume their stream
+arguments are iterable -- a LazyList is the same as a generator to times().
+
+>>> class LazyList:
+... def __init__(self, g):
+... self.sofar = []
+... self.fetch = g.next
+...
+... def __getitem__(self, i):
+... sofar, fetch = self.sofar, self.fetch
+... while i >= len(sofar):
+... sofar.append(fetch())
+... return sofar[i]
+
+>>> def m235():
+... yield 1
+... # Gack: m235 below actually refers to a LazyList.
+... me_times2 = times(2, m235)
+... me_times3 = times(3, m235)
+... me_times5 = times(5, m235)
+... for i in merge(merge(me_times2,
+... me_times3),
+... me_times5):
+... yield i
+
+>>> m235 = LazyList(m235())
+>>> for i in range(5):
+... print [m235[j] for j in range(15*i, 15*(i+1))]
+[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
+[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
+[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
+[200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]
+[400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
"""
+
__test__ = {"tut": tutorial_tests,
"pep": pep_tests,
"email": email_tests,