update from Atlas repo

This commit is contained in:
Luciano Ramalho 2014-12-21 09:01:11 -02:00
parent 33d65dc590
commit 9db73c75ef
6 changed files with 159 additions and 1 deletions

51
iterables/fibo_by_hand.py Normal file
View File

@ -0,0 +1,51 @@
"""
Fibonacci generator implemented "by hand" without generator objects
>>> from itertools import islice
>>> list(islice(Fibonacci(), 15))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
"""
# BEGIN FIBO_BY_HAND
class Fibonacci:
def __iter__(self):
return FibonacciGenerator()
class FibonacciGenerator:
def __init__(self):
self.a = 0
self.b = 1
def __next__(self):
result = self.a
self.a, self.b = self.b, self.a + self.b
return result
def __iter__(self):
return self
# END FIBO_BY_HAND
# for comparison, this is the usual implementation of a Fibonacci
# generator in Python:
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
if __name__ == '__main__':
for x, y in zip(Fibonacci(), fibonacci()):
assert x == y, '%s != %s' % (x, y)
print(x)
if x > 10**10:
break
print('etc...')

48
iterables/paragraph.py Normal file
View File

@ -0,0 +1,48 @@
"""
Paragraph: iterate over sentences and words with generator functions.
The ``.words()`` generator shows the use of ``yield from``.
::
>>> p = Paragraph("The cat. The mat. Is the cat on the mat?"
... " The cat is on the mat.")
>>> for s in p:
... print(s)
...
Sentence('The cat.')
Sentence('The mat.')
Sentence('Is the cat on the mat?')
Sentence('The cat is on the mat.')
>>> list(p.words()) # doctest: +NORMALIZE_WHITESPACE
['The', 'cat', 'The', 'mat', 'Is', 'the', 'cat', 'on',
'the', 'mat', 'The', 'cat', 'is', 'on', 'the', 'mat']
.. Note:: sample text from `McGuffey's First Eclectic Reader`__
__ http://www.gutenberg.org/cache/epub/14640/pg14640.txt
"""
import re
import reprlib
from sentence_gen import Sentence
RE_SENTENCE = re.compile('([^.!?]+[.!?]+)')
class Paragraph:
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Paragraph(%s)' % reprlib.repr(self.text)
def __iter__(self):
for match in RE_SENTENCE.finditer(self.text):
yield Sentence(match.group().strip())
def words(self):
for sentence in self:
yield from sentence

View File

@ -0,0 +1,29 @@
""" Example from `Python: The Full Monty`__ -- A Tested Semantics for the
Python Programming Language
__ http://cs.brown.edu/~sk/Publications/Papers/Published/pmmwplck-python-full-monty/
"The following program, [...] seems to perform a simple abstraction over the
process of yielding:"
Citation:
Joe Gibbs Politz, Alejandro Martinez, Matthew Milano, Sumner Warren,
Daniel Patterson, Junsong Li, Anand Chitipothu, and Shriram Krishnamurthi.
2013. Python: the full monty. SIGPLAN Not. 48, 10 (October 2013), 217-232.
DOI=10.1145/2544173.2509536 http://doi.acm.org/10.1145/2544173.2509536
"""
# BEGIN YIELD_DELEGATE_FAIL
def f():
def do_yield(n):
yield n
x = 0
while True:
x += 1
do_yield(x)
# END YIELD_DELEGATE_FAIL
if __name__ == '__main__':
print('Invoking f() results in an infinite loop')
f()

View File

@ -0,0 +1,24 @@
""" Example adapted from ``yield_delegate_fail.py``
The following program performs a simple abstraction over the process of
yielding.
"""
# BEGIN YIELD_DELEGATE_FIX
def f():
def do_yield(n):
yield n
x = 0
while True:
x += 1
yield from do_yield(x)
# END YIELD_DELEGATE_FIX
if __name__ == '__main__':
print('Invoking f() now produces a generator')
g = f()
print(next(g))
print(next(g))
print(next(g))

View File

@ -17,5 +17,8 @@ class Sentence:
def __getitem__(self, index):
return self.words[index] # <2>
def __len__(self, index): # <3>
return len(self.words)
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text) # <3>
return 'Sentence(%s)' % reprlib.repr(self.text) # <4>

View File

@ -34,6 +34,9 @@ class SentenceSlice:
else:
return self.words[position]
def __len__(self, index):
return len(self.words)
# helper functions -- implementation detail
def _handle_defaults(self, position):
"""handle missing or overflow/underflow start/stop"""