Skip to content

Commit aa868e8

Browse files
committed
ch17: update from book draft
1 parent 7065084 commit aa868e8

35 files changed

+788
-31
lines changed

17-it-generator/aritprog_v0.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def __init__(self, begin, step, end=None):
1717
self.end = end # None -> "infinite" series
1818

1919
def __iter__(self):
20-
result = type(self.begin + self.step)(self.begin)
20+
result_type = type(self.begin + self.step)
21+
result = result_type(self.begin)
2122
forever = self.end is None
2223
while forever or result < self.end:
2324
yield result

17-it-generator/aritprog_v1.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Arithmetic progression class
33
4-
# BEGIN ARITPROG_CLASS_DEMO
4+
# tag::ARITPROG_CLASS_DEMO[]
55
66
>>> ap = ArithmeticProgression(0, 1, 3)
77
>>> list(ap)
@@ -21,24 +21,25 @@
2121
>>> list(ap)
2222
[Decimal('0.0'), Decimal('0.1'), Decimal('0.2')]
2323
24-
# END ARITPROG_CLASS_DEMO
24+
# end::ARITPROG_CLASS_DEMO[]
2525
"""
2626

2727

28-
# BEGIN ARITPROG_CLASS
28+
# tag::ARITPROG_CLASS[]
2929
class ArithmeticProgression:
3030

31-
def __init__(self, begin, step, end=None): # <1>
31+
def __init__(self, begin, step, end=None): # <1>
3232
self.begin = begin
3333
self.step = step
3434
self.end = end # None -> "infinite" series
3535

3636
def __iter__(self):
37-
result = type(self.begin + self.step)(self.begin) # <2>
38-
forever = self.end is None # <3>
37+
result_type = type(self.begin + self.step) # <2>
38+
result = result_type(self.begin) # <3>
39+
forever = self.end is None # <4>
3940
index = 0
40-
while forever or result < self.end: # <4>
41-
yield result # <5>
41+
while forever or result < self.end: # <5>
42+
yield result # <6>
4243
index += 1
43-
result = self.begin + self.step * index # <6>
44-
# END ARITPROG_CLASS
44+
result = self.begin + self.step * index # <7>
45+
# end::ARITPROG_CLASS[]

17-it-generator/aritprog_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"""
2020

2121

22-
# BEGIN ARITPROG_GENFUNC
22+
# tag::ARITPROG_GENFUNC[]
2323
def aritprog_gen(begin, step, end=None):
2424
result = type(begin + step)(begin)
2525
forever = end is None
@@ -28,4 +28,4 @@ def aritprog_gen(begin, step, end=None):
2828
yield result
2929
index += 1
3030
result = begin + step * index
31-
# END ARITPROG_GENFUNC
31+
# end::ARITPROG_GENFUNC[]

17-it-generator/aritprog_v3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# BEGIN ARITPROG_ITERTOOLS
1+
# tag::ARITPROG_ITERTOOLS[]
22
import itertools
33

44

@@ -8,4 +8,4 @@ def aritprog_gen(begin, step, end=None):
88
if end is not None:
99
ap_gen = itertools.takewhile(lambda n: n < end, ap_gen)
1010
return ap_gen
11-
# END ARITPROG_ITERTOOLS
11+
# end::ARITPROG_ITERTOOLS[]

17-it-generator/fibo_by_hand.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99

1010

11-
# BEGIN FIBO_BY_HAND
11+
# tag::FIBO_BY_HAND[]
1212
class Fibonacci:
1313

1414
def __iter__(self):
@@ -28,7 +28,7 @@ def __next__(self):
2828

2929
def __iter__(self):
3030
return self
31-
# END FIBO_BY_HAND
31+
# end::FIBO_BY_HAND[]
3232

3333
# for comparison, this is the usual implementation of a Fibonacci
3434
# generator in Python:

17-it-generator/isis2json/isis2json.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ def main(): # <4>
200200
' for bulk insert to CouchDB via POST to db/_bulk_docs')
201201
parser.add_argument(
202202
'-m', '--mongo', action='store_true',
203-
help='output individual records as separate JSON dictionaries,'
204-
' one per line for bulk insert to MongoDB via mongoimport utility')
203+
help='output individual records as separate JSON dictionaries, one'
204+
' per line for bulk insert to MongoDB via mongoimport utility')
205205
parser.add_argument(
206206
'-t', '--type', type=int, metavar='ISIS_JSON_TYPE', default=1,
207-
help='ISIS-JSON type, sets field structure: 1=string, 2=alist, 3=dict (default=1)')
207+
help='ISIS-JSON type, sets field structure: 1=string, 2=alist,'
208+
' 3=dict (default=1)')
208209
parser.add_argument(
209210
'-q', '--qty', type=int, default=DEFAULT_QTY,
210211
help='maximum quantity of records to read (default=ALL)')
@@ -220,7 +221,8 @@ def main(): # <4>
220221
help='generate an "_id" with a random UUID for each record')
221222
parser.add_argument(
222223
'-p', '--prefix', type=str, metavar='PREFIX', default='',
223-
help='concatenate prefix to every numeric field tag (ex. 99 becomes "v99")')
224+
help='concatenate prefix to every numeric field tag'
225+
' (ex. 99 becomes "v99")')
224226
parser.add_argument(
225227
'-n', '--mfn', action='store_true',
226228
help='generate an "_id" from the MFN of each record'

17-it-generator/sentence.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
"""
22
Sentence: access words by index
3+
4+
>>> text = 'To be, or not to be, that is the question'
5+
>>> s = Sentence(text)
6+
>>> len(s)
7+
10
8+
>>> s[1], s[5]
9+
('be', 'be')
10+
>>> s
11+
Sentence('To be, or no... the question')
12+
313
"""
414

15+
# tag::SENTENCE_SEQ[]
516
import re
617
import reprlib
718

@@ -17,8 +28,10 @@ def __init__(self, text):
1728
def __getitem__(self, index):
1829
return self.words[index] # <2>
1930

20-
def __len__(self, index): # <3>
31+
def __len__(self): # <3>
2132
return len(self.words)
2233

2334
def __repr__(self):
2435
return 'Sentence(%s)' % reprlib.repr(self.text) # <4>
36+
37+
# end::SENTENCE_SEQ[]

17-it-generator/sentence_gen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Sentence: iterate over words using a generator function
33
"""
44

5+
# tag::SENTENCE_GEN[]
56
import re
67
import reprlib
78

@@ -23,3 +24,5 @@ def __iter__(self):
2324
return # <3>
2425

2526
# done! <4>
27+
28+
# end::SENTENCE_GEN[]

17-it-generator/sentence_gen2.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
Sentence: iterate over words using a generator function
33
"""
44

5+
# tag::SENTENCE_GEN2[]
56
import re
67
import reprlib
78

8-
RE_WORD = re.compile(r'\w+')
9+
RE_WORD = re.compile('r\w+')
910

1011

1112
class Sentence:
@@ -19,3 +20,5 @@ def __repr__(self):
1920
def __iter__(self):
2021
for match in RE_WORD.finditer(self.text): # <2>
2122
yield match.group() # <3>
23+
24+
# end::SENTENCE_GEN2[]

17-it-generator/sentence_genexp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Sentence: iterate over words using a generator expression
33
"""
44

5-
# BEGIN SENTENCE_GENEXP
5+
# tag::SENTENCE_GENEXP[]
66
import re
77
import reprlib
88

@@ -19,7 +19,7 @@ def __repr__(self):
1919

2020
def __iter__(self):
2121
return (match.group() for match in RE_WORD.finditer(self.text))
22-
# END SENTENCE_GENEXP
22+
# end::SENTENCE_GENEXP[]
2323

2424

2525
def main():

0 commit comments

Comments
 (0)