Skip to content
Browse files

Format doc into ReST, Add LISENCE

  • Loading branch information...
1 parent c76032e commit 6491525e16925107bee16eaca0a1a9d91c0324ba @aht committed Nov 20, 2009
Showing with 82 additions and 59 deletions.
  1. +19 −0 LICENSE
  2. +2 −1 README
  3. +4 −2 setup.py
  4. +57 −56 stream.py
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2009 Anh Hai Trinh
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
3 README
@@ -27,4 +27,5 @@ To make sure it works with your Python version, run all doctests:
DOCUMENTATION
=============
-./stream.py is well-documented with plenty of short and long examples.
+./stream.py is well-documented with plenty of short and long examples.
+
View
6 setup.py
@@ -12,20 +12,22 @@
classifiers = """
Development Status :: 3 - Alpha
Intended Audience :: Developers
+License :: OSI Approved :: MIT License
Operating System :: OS Independent
Programming Language :: Python
Topic :: Software Development :: Libraries :: Python Modules
+Topic :: Utilities
"""
setup(
name = 'stream',
version = stream.__version__,
description = stream.__doc__.split('\n')[0],
- long_description = stream.__doc__.split('\n\n')[1],
+ long_description = '\n'.join(stream.__doc__.split('\n')[2:]),
author = 'Anh Hai Trinh',
author_email = 'moc.liamg@hnirt.iah.hna:otliam'[::-1],
+ keywords='lazy iterable iterator generator stream data flow functional processing',
url = 'http://github.com/aht/stream.py',
- license='MIT',
platforms=['any'],
classifiers=filter(None, classifiers.split("\n")),
py_modules = ['stream']
View
113 stream.py
@@ -1,4 +1,7 @@
-"""Lazily-evaluated stream with pipelining via the '>>' operator.
+"""Lazily-evaluated stream with pipelining via the '>>' operator
+
+Introduction
+============
Streams are generalized iterables with a pipelining mechanism to enable
data-flow programming.
@@ -19,19 +22,19 @@
Finally, the output is fed to an accumulator, which can be any function
of one iterable argument.
-Generators: anything iterable
- * from this module: seq, gseq, repeatcall, chaincall
+**Generators**: anything iterable
+ + from this module: seq, gseq, repeatcall, chaincall
-Processors
- * by index: take, drop, cut
- * by condition: filter, takewhile, dropwhile
- * by transformation: map, apply, fold
- * special purpose: attrgetter, methodcaller, splitter
+**Processors**:
+ + by index: take, drop, cut
+ + by condition: filter, takewhile, dropwhile
+ + by transformation: map, apply, fold
+ + special purpose: attrgetter, methodcaller, splitter
-Combinators: prepend, takei, dropi, tee, flatten
+**Combinators**: prepend, takei, dropi, tee, flatten
-Accumulators: item, maximum, minimum, reduce
- * from Python: list, sum, dict, ...
+**Accumulators**: item, maximum, minimum, reduce
+ + from Python: list, sum, dict, ...
take() and item[] work similarly, except for notation and the fact that
item[] returns a list whereas take() returns a stream which can be further
@@ -40,75 +43,73 @@
Values are computed only when an accumulator forces some or all evaluation
(not when the stream are set up).
-Example: better itertools.slice
------
+Examples
+========
+
+Better itertools.slice
+----------------------
+::
from itertools import count
c = count()
- c >> item[1:10:2]
-->[1, 3, 5, 7, 9]
- c >> item[:5]
-->[10, 11, 12, 13, 14]
+ c >> item[1:10:2] #-> [1, 3, 5, 7, 9]
+ c >> item[:5] #-> [10, 11, 12, 13, 14]
-
-Example: String processing
------
+String processing
+-----------------
Grep some lines matching a regex from a file, cut out the 3rd field
-separated by ':' or '.', strip leading zeroes, then save as a list.
+separated by ':' or '.', strip leading zeroes, then save as a list::
- import re
- s = open('file').xreadlines() \
- >> filter(re.compile(regex).search) \
- >> map(splitter(':|\.')) \
- >> map(methodcaller('strip', '0')) \
- >> list
+ import re
+ s = open('file').xreadlines() \
+ >> filter(re.compile(regex).search) \
+ >> map(splitter(':|\.')) \
+ >> map(methodcaller('strip', '0')) \
+ >> list
-Example: Partial sums
------
-Compute the first few partial sums of the geometric series 1 + 1/2 + 1/4 + ...
+Partial sums
+------------
+Compute the first few partial sums of the geometric series 1 + 1/2 + 1/4 + ..::
- gseq(0.5) >> fold(lambda x, y: x+y) >> item[:5]
-->[1, 1.5, 1.75, 1.875, 1.9375]
+ gseq(0.5) >> fold(lambda x, y: x+y) >> item[:5]
+ #->[1, 1.5, 1.75, 1.875, 1.9375]
-Example: Random Walk
------
+Random Walk in 2D
+-----------------
Generate an infinite stream of coordinates representing the position of
-a random walker in 2D.
+a random walker in 2D::
- from random import choice
- vectoradd = lambda u,v: zip(u, v) >> map(sum) >> list
- def rw():
- return repeatcall(choice, [[1,0], [0,1], [-1,0], [0,-1]]) >> fold(vectoradd, [0, 0])
- walk = rw()
- walk >> item[:10]
-->[[0, 0], ...]
+ from random import choice
+ vectoradd = lambda u,v: zip(u, v) >> map(sum) >> list
+ rw = lambda: repeatcall(choice, [[1,0], [0,1], [-1,0], [0,-1]]) >> fold(vectoradd, [0, 0])
+ walk = rw()
+ walk >> take(10)
+ #->Stream([[0, 0], ...])
Here calling choice repeatedly yields the series of unit vectors
representing the directions that the walker takes, then these vectors
are gradually added to get a series of coordinates.
-Question: What is the farthest point that he wanders upto the first return to
- the origin?
-
- vectorlen = lambda v: v >> map(lambda x: x**2) >> sum
- rw() >> drop(1) >> takewhile(lambda v: v != [0, 0]) >> maximum(key=vectorlen)
-->[?, ?]
+What is the farthest point that he wanders upto the first return to the
+origin?::
-Note that this might not terminate! The first coordinate which is [0,
-0] needs to be dropped otherwise takewhile will truncate immediately.
+ vectorlen = lambda v: v >> map(lambda x: x**2) >> sum
+ rw() >> drop(1) >> takewhile(lambda v: v != [0, 0]) >> maximum(key=vectorlen)
-We can actually probe into the stream, like this,
+Note that this might not terminate! The first coordinate which is [0, 0]
+needs to be dropped otherwise takewhile will truncate immediately.
- probe = takeall
- rw() >> drop(1) >> takewhile(lambda v: v != [0, 0]) >> tee(probe) >> maximum(key=vectorlen)
- probe
-->Stream([[0, 0], ...])
+We can also probe into the stream, like this::
+ probe = takeall
+ rw() >> drop(1) >> takewhile(lambda v: v != [0, 0]) >> tee(probe) >> maximum(key=vectorlen)
+ probe
+ #->Stream([[0, 0], ...])
"""
-__version__ = '0.5'
+__version__ = '0.5.1'
__author__ = 'Anh Hai Trinh'
__email__ = 'moc.liamg@hnirt.iah.hna:otliam'[::-1]
__all__ = [

0 comments on commit 6491525

Please sign in to comment.
Something went wrong with that request. Please try again.