Skip to content

Commit 9db73c7

Browse files
committed
update from Atlas repo
1 parent 33d65dc commit 9db73c7

File tree

6 files changed

+159
-1
lines changed

6 files changed

+159
-1
lines changed

iterables/fibo_by_hand.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Fibonacci generator implemented "by hand" without generator objects
3+
4+
>>> from itertools import islice
5+
>>> list(islice(Fibonacci(), 15))
6+
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
7+
8+
"""
9+
10+
11+
# BEGIN FIBO_BY_HAND
12+
class Fibonacci:
13+
14+
def __iter__(self):
15+
return FibonacciGenerator()
16+
17+
18+
class FibonacciGenerator:
19+
20+
def __init__(self):
21+
self.a = 0
22+
self.b = 1
23+
24+
def __next__(self):
25+
result = self.a
26+
self.a, self.b = self.b, self.a + self.b
27+
return result
28+
29+
def __iter__(self):
30+
return self
31+
# END FIBO_BY_HAND
32+
33+
# for comparison, this is the usual implementation of a Fibonacci
34+
# generator in Python:
35+
36+
37+
def fibonacci():
38+
a, b = 0, 1
39+
while True:
40+
yield a
41+
a, b = b, a + b
42+
43+
44+
if __name__ == '__main__':
45+
46+
for x, y in zip(Fibonacci(), fibonacci()):
47+
assert x == y, '%s != %s' % (x, y)
48+
print(x)
49+
if x > 10**10:
50+
break
51+
print('etc...')

iterables/paragraph.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Paragraph: iterate over sentences and words with generator functions.
3+
The ``.words()`` generator shows the use of ``yield from``.
4+
5+
::
6+
>>> p = Paragraph("The cat. The mat. Is the cat on the mat?"
7+
... " The cat is on the mat.")
8+
>>> for s in p:
9+
... print(s)
10+
...
11+
Sentence('The cat.')
12+
Sentence('The mat.')
13+
Sentence('Is the cat on the mat?')
14+
Sentence('The cat is on the mat.')
15+
>>> list(p.words()) # doctest: +NORMALIZE_WHITESPACE
16+
['The', 'cat', 'The', 'mat', 'Is', 'the', 'cat', 'on',
17+
'the', 'mat', 'The', 'cat', 'is', 'on', 'the', 'mat']
18+
19+
20+
.. Note:: sample text from `McGuffey's First Eclectic Reader`__
21+
22+
__ http://www.gutenberg.org/cache/epub/14640/pg14640.txt
23+
"""
24+
25+
import re
26+
import reprlib
27+
28+
from sentence_gen import Sentence
29+
30+
31+
RE_SENTENCE = re.compile('([^.!?]+[.!?]+)')
32+
33+
34+
class Paragraph:
35+
36+
def __init__(self, text):
37+
self.text = text
38+
39+
def __repr__(self):
40+
return 'Paragraph(%s)' % reprlib.repr(self.text)
41+
42+
def __iter__(self):
43+
for match in RE_SENTENCE.finditer(self.text):
44+
yield Sentence(match.group().strip())
45+
46+
def words(self):
47+
for sentence in self:
48+
yield from sentence

iterables/yield_delegate_fail.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
""" Example from `Python: The Full Monty`__ -- A Tested Semantics for the
2+
Python Programming Language
3+
4+
__ http://cs.brown.edu/~sk/Publications/Papers/Published/pmmwplck-python-full-monty/
5+
6+
"The following program, [...] seems to perform a simple abstraction over the
7+
process of yielding:"
8+
9+
Citation:
10+
11+
Joe Gibbs Politz, Alejandro Martinez, Matthew Milano, Sumner Warren,
12+
Daniel Patterson, Junsong Li, Anand Chitipothu, and Shriram Krishnamurthi.
13+
2013. Python: the full monty. SIGPLAN Not. 48, 10 (October 2013), 217-232.
14+
DOI=10.1145/2544173.2509536 http://doi.acm.org/10.1145/2544173.2509536
15+
"""
16+
17+
# BEGIN YIELD_DELEGATE_FAIL
18+
def f():
19+
def do_yield(n):
20+
yield n
21+
x = 0
22+
while True:
23+
x += 1
24+
do_yield(x)
25+
# END YIELD_DELEGATE_FAIL
26+
27+
if __name__ == '__main__':
28+
print('Invoking f() results in an infinite loop')
29+
f()

iterables/yield_delegate_fix.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
""" Example adapted from ``yield_delegate_fail.py``
2+
3+
The following program performs a simple abstraction over the process of
4+
yielding.
5+
6+
"""
7+
8+
# BEGIN YIELD_DELEGATE_FIX
9+
def f():
10+
def do_yield(n):
11+
yield n
12+
x = 0
13+
while True:
14+
x += 1
15+
yield from do_yield(x)
16+
# END YIELD_DELEGATE_FIX
17+
18+
if __name__ == '__main__':
19+
print('Invoking f() now produces a generator')
20+
g = f()
21+
print(next(g))
22+
print(next(g))
23+
print(next(g))
24+

sequences/sentence.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ def __init__(self, text):
1717
def __getitem__(self, index):
1818
return self.words[index] # <2>
1919

20+
def __len__(self, index): # <3>
21+
return len(self.words)
22+
2023
def __repr__(self):
21-
return 'Sentence(%s)' % reprlib.repr(self.text) # <3>
24+
return 'Sentence(%s)' % reprlib.repr(self.text) # <4>

sequences/sentence_slice.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def __getitem__(self, position):
3434
else:
3535
return self.words[position]
3636

37+
def __len__(self, index):
38+
return len(self.words)
39+
3740
# helper functions -- implementation detail
3841
def _handle_defaults(self, position):
3942
"""handle missing or overflow/underflow start/stop"""

0 commit comments

Comments
 (0)