Skip to content

Commit 45c7ec1

Browse files
committed
Links and renumbering
1 parent 9c032f7 commit 45c7ec1

10 files changed

Lines changed: 49 additions & 42 deletions

Notes/04_Classes_objects/01_Class.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ If you want to operate on an instance, you always have to refer too it explicitl
110110

111111
## Exercises
112112

113-
### (a) Objects as Data Structures
113+
### Exercise 4.1: Objects as Data Structures
114114

115115
In section 2 and 3, we worked with data represented as tuples and dictionaries.
116116
For example, a holding of stock could be represented as a tuple like this:
@@ -182,7 +182,7 @@ dictionary, just with somewhat different syntax.
182182
For example, instead of writing `s['name']` or `s['price']`, you now
183183
write `s.name` and `s.price`.
184184

185-
### (b) Reading Data into a List of Objects
185+
### Exercise 4.2: Reading Data into a List of Objects
186186

187187
In your `stock.py` program, write a function
188188
`read_portfolio(filename)` that reads portfolio data from a file into
@@ -226,7 +226,7 @@ Try a list comprehension:
226226

227227
Again, notice the similarity between `Stock` objects and dictionaries. They’re basically the same idea, but the syntax for accessing values differs.
228228

229-
### (c) Adding some Methods
229+
### Exercise 4.3: Adding some Methods
230230

231231
With classes, you can attach functions to your objects. These are
232232
known as methods and are functions that operate on the data stored
@@ -250,4 +250,4 @@ work like this:
250250
>>>
251251
```
252252

253-
[Next](02_Inheritance)
253+
[Contents](../Contents) \| [Previous (3.6 Design discussion)](../03_Program_organization/06_Design_discussion) \| [Next (4.2 Inheritance)](02_Inheritance)

Notes/04_Classes_objects/02_Inheritance.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ We're not going to explore multiple inheritance further in this course.
220220

221221
## Exercises
222222

223-
### (a) Print Portfolio
223+
### Exercise 4.4: Print Portfolio
224224

225225
A major use of inheritance is in writing code that’s meant to be extended or customized in various ways—especially in libraries or frameworks.
226226
To illustrate, start by adding the following function to your `stock.py` program:
@@ -263,7 +263,7 @@ When you run your `stock.py`, you should get this output:
263263
IBM 100 70.44
264264
```
265265

266-
### (b) An Extensibility Problem
266+
### Exercise 4.5: An Extensibility Problem
267267

268268
Suppose that you wanted to modify the `print_portfolio()` function to
269269
support a variety of different output formats such as plain-text,
@@ -329,7 +329,7 @@ if __name__ == '__main__':
329329
When you run this new code, your program will immediately crash with a `NotImplementedError` exception.
330330
That’s not too exciting, but continue to the next part.
331331

332-
### (c) Using Inheritance to Produce Different Output
332+
### Exercise 4.6: Using Inheritance to Produce Different Output
333333

334334
The `TableFormatter` class you defined in part (a) is meant to be extended via inheritance.
335335
In fact, that’s the whole idea. To illustrate, define a class `TextTableFormatter` like this:
@@ -430,7 +430,7 @@ Using a similar idea, define a class `HTMLTableFormatter` that produces a table
430430

431431
Test your code by modifying the main program to create a `HTMLTableFormatter` object instead of a `CSVTableFormatter` object.
432432

433-
### (d) Polymorphism in Action
433+
### Exercise 4.7: Polymorphism in Action
434434

435435
A major feature of object-oriented programming is that you can plug an
436436
object into a program and it will work without having to change any of
@@ -499,4 +499,4 @@ That said, understanding what happened in this exercise will take you
499499
pretty far in terms of using most library modules and knowing
500500
what inheritance is good for (extensibility).
501501

502-
[Next](03_Special_methods)
502+
[Contents](../Contents) \| [Previous (4.1 Classes)](01_Class) \| [Next (4.3 Special methods)](03_Special_methods)

Notes/04_Classes_objects/03_Special_methods.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ x = getattr(obj, 'x', None)
202202

203203
## Exercises
204204

205-
### (a) Better output for printing objects
205+
### Exercise 4.8: Better output for printing objects
206206

207207
All Python objects have two string representations. The first
208208
representation is created by string conversion via `str()` (which is
@@ -248,7 +248,7 @@ See what happens when you read a portfolio of stocks and view the resulting list
248248
>>>
249249
```
250250

251-
### (b) An example of using `getattr()`
251+
### Exercise 4.9: An example of using `getattr()`
252252

253253
In Exercise 4.2 you worked with a function `print_portfolio()` that made a table for a stock portfolio.
254254
That function was hard-coded to only work with stock data—-how limiting! You can do so much more if you use functions such as `getattr()`.
@@ -306,7 +306,7 @@ format. Here’s how it should work:
306306
>>>
307307
```
308308

309-
### (c) Exercise Bonus: Column Formatting
309+
### Exercise 4.10: Exercise Bonus: Column Formatting
310310

311311
Modify the `print_table()` function in part (B) so that it also
312312
accepts a list of format specifiers for formatting the contents of
@@ -329,4 +329,5 @@ each column.
329329
>>>
330330
```
331331

332-
[Next](04_Defining_exceptions)
332+
[Contents](../Contents) \| [Previous (4.2 Inheritance)](02_Inheritance) \| [Next (4.4 Exceptions)](04_Defining_exceptions)
333+

Notes/04_Classes_objects/04_Defining_exceptions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ProtocolError(NetworkError):
2222

2323
## Exercises
2424

25-
### (a) Defining a custom exception
25+
### Exercise 4.11: Defining a custom exception
2626

2727
It is often good practice for libraries to define their own exceptions.
2828

@@ -47,3 +47,5 @@ Traceback (most recent call last):
4747
FormatError: Unknown table format xls
4848
>>>
4949
```
50+
51+
[Contents](../Contents) \| [Previous (4.3 Special methods)](03_Special_methods) \| [Next (5 Object Model)](../05_Object_model/00_Overview)

Notes/05_Object_model/01_Dicts_revisited.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ Frameworks / libraries sometimes use it for advanced features involving composit
362362

363363
## Exercises
364364

365-
In Exercise 4.1, you defined a class `Stock` that represented a holding of stock.
365+
In Section 4, you defined a class `Stock` that represented a holding of stock.
366366
In this exercise, we will use that class.
367367

368-
### (a) Representation of Instances
368+
### Exercise 5.1: Representation of Instances
369369

370370
At the interactive shell, inspect the underlying dictionaries of the two instances you created:
371371

@@ -380,7 +380,7 @@ At the interactive shell, inspect the underlying dictionaries of the two instanc
380380
>>>
381381
```
382382

383-
### (b) Modification of Instance Data
383+
### Exercise 5.2: Modification of Instance Data
384384

385385
Try setting a new attribute on one of the above instances:
386386

@@ -412,7 +412,7 @@ top of a dictionary. *Note: it should be emphasized that direct
412412
manipulation of the dictionary is uncommon—you should always write
413413
your code to use the (.) syntax.*
414414

415-
### (c) The role of classes
415+
### Exercise 5.3: The role of classes
416416

417417
The definitions that make up a class definition are shared by all instances of that class.
418418
Notice, that all instances have a link back to their associated class:
@@ -517,7 +517,7 @@ It is shared by all of the instances that get created.
517517
>>>
518518
```
519519

520-
### (d) Bound Methods
520+
### Exercise 5.4: Bound Methods
521521

522522
A subtle feature of Python is that invoking a method actually involves
523523
two steps and something known as a bound method.
@@ -568,7 +568,7 @@ For example, calling `s(25)` actually does this:
568568
>>>
569569
```
570570

571-
### (e) Inheritance
571+
### Exercise 5.5: Inheritance
572572

573573
Make a new class that inherits from `Stock`.
574574

@@ -617,4 +617,5 @@ Here’s how the `cost()` method of instance `n` above would be found:
617617
>>>
618618
```
619619

620-
[Next](02_Classes_encapsulation)
620+
[Contents](../Contents) \| [Previous (4.4 Exceptions)](../04_Classes_objects/04_Defining_exceptions) \| [Next (5.2 Encapsulation)](02_Classes_encapsulation)
621+

Notes/05_Object_model/02_Classes_encapsulation.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ day-to-day coding.
252252

253253
## Exercises
254254

255-
### (a) Simple properties
255+
### Exercise 5.6: Simple properties
256256

257257
Properties are a useful way to add "computed attributes" to an object.
258258
In Exercise 4.1, you created an object `Stock`. Notice that on your
@@ -289,7 +289,7 @@ Try calling `s.cost()` as a function and observe that it doesn’t work now that
289289
>>>
290290
```
291291

292-
### (b) Properties and Setters
292+
### Exercise 5.7: Properties and Setters
293293

294294
Modify the `shares` attribute so that the value is stored in a private
295295
attribute and that a pair of property functions are used to ensure
@@ -306,7 +306,7 @@ TypeError: expected an integer
306306
>>>
307307
```
308308

309-
### (c) Adding slots
309+
### Exercise 5.8: Adding slots
310310

311311
Modify the `Stock` class so that it has a `__slots__` attribute.
312312
Then, verify that new attributes can’t be added:
@@ -333,3 +333,5 @@ What happens if you try to inspect the underlying dictionary of `s` above?
333333
It should be noted that `__slots__` is most commonly used as an
334334
optimization on classes that serve as data structures. Using slots
335335
will make such programs use far-less memory and run a bit faster.
336+
337+
[Contents](../Contents) \| [Previous (5.1 Dictionaries Revisited)](01_Dicts_revisited) \| [Next (6 Generators)](../06_Generators/00_Overview)

Notes/06_Generators/01_Iteration_protocol.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ for s in port:
8686

8787
## Exercises
8888

89-
### (a) Iteration Illustrated
89+
### Exercise 6.1: Iteration Illustrated
9090

9191
Create the following list:
9292

@@ -137,7 +137,7 @@ the `__next__()` method of an iterator. Try using it on a file:
137137
Keep calling `next(f)` until you reach the end of the
138138
file. Watch what happens.
139139

140-
### (b) Supporting Iteration
140+
### Exercise 6.2: Supporting Iteration
141141

142142
On occasion, you might want to make one of your own objects support
143143
iteration--especially if your object wraps around an existing
@@ -249,7 +249,7 @@ Test it to make sure it works:
249249
>>>
250250
```
251251

252-
### (d) Making a more proper container
252+
### Exercise 6.3: Making a more proper container
253253

254254
If making a container class, you often want to do more than just
255255
iteration. Modify the `Portfolio` class so that it has some other
@@ -310,4 +310,4 @@ Python normally work. For container objects, supporting iteration,
310310
indexing, containment, and other kinds of operators is an important
311311
part of this.
312312

313-
[Next](02_Customizing_iteration)
313+
[Contents](../Contents) \| [Previous (5.2 Encapsulation)](../05_Classes_objects/02_Classes_encapsulation) \| [Next (6.2 Customizing Iteration)](02_Customizing_iteration)

Notes/06_Generators/02_Customizing_iteration.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ File "<stdin>", line 1, in ? StopIteration
9999

100100
## Exercises
101101

102-
### (a) A Simple Generator
102+
### Exercise 6.4: A Simple Generator
103103

104104
If you ever find yourself wanting to customize iteration, you should
105105
always think generator functions. They're easy to write---make
@@ -139,7 +139,7 @@ This is kind of interesting--the idea that you can hide a bunch of
139139
custom processing in a function and use it to feed a for-loop.
140140
The next example looks at a more unusual case.
141141

142-
### (b) Monitoring a streaming data source
142+
### Exercise 6.5: Monitoring a streaming data source
143143

144144
Generators can be an interesting way to monitor real-time data sources
145145
such as log files or stock market feeds. In this part, we'll
@@ -197,7 +197,7 @@ this case, we are using it to repeatedly probe the end of the file to
197197
see if more data has been added (`readline()` will either
198198
return new data or an empty string).
199199

200-
### (c) Using a generator to produce data
200+
### Exercise 6.6: Using a generator to produce data
201201

202202
If you look at the code in part (b), the first part of the code is producing
203203
lines of data whereas the statements at the end of the `while` loop are consuming
@@ -229,7 +229,7 @@ if __name__ == '__main__':
229229
print(f'{name:>10s} {price:>10.2f} {change:>10.2f}')
230230
```
231231

232-
### (d) Watching your portfolio
232+
### Exercise 6.7: Watching your portfolio
233233

234234
Modify the `follow.py` program so that it watches the stream of stock
235235
data and prints a ticker showing information for only those stocks
@@ -262,4 +262,4 @@ is now this completely general purpose utility that you can use in any program.
262262
example, you could use it to watch server logs, debugging logs, and other similar data sources.
263263
That's kind of cool.
264264

265-
[Next](03_Producers_consumers)
265+
[Contents](../Contents) \| [Previous (6.1 Iteration Protocol)](01_Iteration_protocol) \| [Next (6.3 Producer/Consumer)](03_Producers_consumers)

Notes/06_Generators/03_Producers_consumers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ You will notice that data incrementally flows through the different functions.
101101
For this exercise the `stocksim.py` program should still be running in the background.
102102
You’re going to use the `follow()` function you wrote in the previous exercise.
103103

104-
### (a) Setting up a simple pipeline
104+
### Exercise 6.8: Setting up a simple pipeline
105105

106106
Let's see the pipelining idea in action. Write the following
107107
function:
@@ -132,7 +132,7 @@ to it as an argument. Now, try this:
132132
It might take awhile for output to appear, but eventually you
133133
should see some lines containing data for IBM.
134134

135-
### (b) Setting up a more complex pipeline
135+
### Exercise 6.9: Setting up a more complex pipeline
136136

137137
Take the pipelining idea a few steps further by performing
138138
more actions.
@@ -156,7 +156,7 @@ Well, that's interesting. What you're seeing here is that the output of the
156156
`follow()` function has been piped into the `csv.reader()` function and we're
157157
now getting a sequence of split rows.
158158

159-
### (c) Making more pipeline components
159+
### Exercise 6.10: Making more pipeline components
160160

161161
Let's extend the whole idea into a larger pipeline. In a separate file `ticker.py`,
162162
start by creating a function that reads a CSV file as you did above:
@@ -237,7 +237,7 @@ Run your program again. You should now a stream of dictionaries like this:
237237
...
238238
```
239239

240-
### (d) Filtering data
240+
### Exercise 6.11: Filtering data
241241

242242
Write a function that filters data. For example:
243243

@@ -262,7 +262,7 @@ for row in rows:
262262
print(row)
263263
```
264264

265-
### (e) Putting it all together
265+
### Exercise 6.12: Putting it all together
266266

267267
In the `ticker.py` program, write a function `ticker(portfile, logfile, fmt)`
268268
that creates a real-time stock ticker from a given portfolio, logfile,
@@ -296,6 +296,6 @@ pipelines. In addition, you can create functions that package a
296296
series of pipeline stages into a single function call (for example,
297297
the `parse_stock_data()` function).
298298

299-
[Next](04_More_generators)
299+
[Contents](../Contents) \| [Previous (6.2 Customizing Iteration)](02_Customizing_iteration) \| [Next (6.4 Generator Expressions)](04_More_generators)
300300

301301

Notes/06_Generators/04_More_generators.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ More information at [Generator Tricks for Systems Programmers](http://www.dabeaz
104104
In the previous exercises, you wrote some code that followed lines being written to a log file and parsed them into a sequence of rows.
105105
This exercise continues to build upon that. Make sure the `Data/stocksim.py` is still running.
106106

107-
### (a) Generator Expressions
107+
### Exercise 6.13: Generator Expressions
108108

109109
Generator expressions are a generator version of a list comprehension.
110110
For example:
@@ -134,7 +134,7 @@ Thus, if you try another for-loop, you get nothing:
134134
>>>
135135
```
136136

137-
### (b) Generator Expressions in Function Arguments
137+
### Exercise 6.14: Generator Expressions in Function Arguments
138138

139139
Generator expressions are sometimes placed into function arguments.
140140
It looks a little weird at first, but try this experiment:
@@ -154,7 +154,7 @@ In your `portfolio.py` file, you performed a few calculations
154154
involving list comprehensions. Try replacing these with
155155
generator expressions.
156156

157-
### (c) Code simplification
157+
### Exercise 6.15: Code simplification
158158

159159
Generators expressions are often a useful replacement for
160160
small generator functions. For example, instead of writing a
@@ -177,3 +177,4 @@ Modify the `ticker.py` program to use generator expressions
177177
as appropriate.
178178

179179

180+
[Contents](../Contents) \| [Previous (6.3 Producer/Consumer)](03_Producers_consumers) \| [Next (7 Advanced Topics)](../07_Advanced_Topics/00_Overview)

0 commit comments

Comments
 (0)