Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aht committed Jan 14, 2010
1 parent 81d8285 commit 0292c26
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ is both a lazy list of items and an iterator-processing function.
By default, it replaces `self.iterator` with the one returned by
``self.__call__(iter(inpipe))``.

The following are constructor of :class:`Stream`-derived classes: :func:`take`,
The following are constructors of :class:`Stream`-derived classes: :func:`take`,
:func:`drop`, :func:`takei`, :func:`dropi`, :func:`chop`, :func:`filter`,
:func:`takewhile`, :func:`dropwhile`, :func:`apply`, :func:`map`, :func:`fold`,
:func:`prepend`, :func:`tee`, :class:`ProcessPool`, :class:`ThreadPool`,
Expand Down
13 changes: 4 additions & 9 deletions example/feeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
user 0m7.026s
sys 0m0.033s
time python ./feeder.py -t # use threads
$ time python ./feeder.py -t # use threads
real 0m7.231s
user 0m7.046s
Expand All @@ -28,14 +28,9 @@
"""

def blocking_producer():
n = 0
while 1:
time.sleep(0.05)
n += 1
if n < 100:
yield 42
else:
raise StopIteration
for n in range(25):
time.sleep(0.01)
yield 42

if __name__ == '__main__':
f = lambda x: x**x**3
Expand Down
9 changes: 6 additions & 3 deletions example/pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
"""
Compute digits of pi using the Gregory series, and its accelerated variants.
For a detailed explanation, see this section of SICP:
Inspired by this section of the wizard book (SICP):
<http://mitpress.mit.edu/sicp/full-text/sicp/book/node72.html>
"""

def alt_sign(s):
"""Alternate the sign of elements of the input stream by multiply it with
"""Alternate the sign of numbers of the input stream by multiply it with
the unit alternating series 1, -1, ...
"""
return zip(s, gseq(-1, initval=1)) >> apply(operator.mul)


def Gregory(type=float):
"""Return partial sums of the Gregory series converging to atan(1) == pi/4"""
"""Return partial sums of the Gregory series converging to atan(1) == pi/4.
Yield 1 - 1/3 + 1/5 - 1/7 + ... computed with the given type.
"""
return seq(type(1), step=2) >> map(lambda x: 1/x) >> alt_sign >> fold(operator.add)


Expand Down
6 changes: 5 additions & 1 deletion example/retrieve_urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python

"""
Demonstrate the use of a ThreadPool to simultaneously retrieve web pages.
"""

import urllib2
from stream import ThreadPool

Expand All @@ -18,7 +22,7 @@ def retrieve(urls, timeout=10):
yield url, urllib2.urlopen(url, timeout=timeout).read()

if __name__ == '__main__':
retrieved = URLs >> ThreadPool(retrieve, poolsize=4)
retrieved = URLs >> ThreadPool(retrieve, poolsize=len(URLs))
for url, content in retrieved:
print '%r is %d bytes' % (url, len(content))
for url, exception in retrieved.failure:
Expand Down
9 changes: 2 additions & 7 deletions test/feeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@
## Test scenario based on ../example/feeder.py

def blocking_producer():
n = 0
while 1:
for n in range(25):
time.sleep(0.01)
n += 1
if n < 25:
yield 42
else:
raise StopIteration
yield 42

f = lambda x: x**2

Expand Down

0 comments on commit 0292c26

Please sign in to comment.