Links and renumbering
This commit is contained in:
@@ -86,7 +86,7 @@ for s in port:
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) Iteration Illustrated
|
||||
### Exercise 6.1: Iteration Illustrated
|
||||
|
||||
Create the following list:
|
||||
|
||||
@@ -137,7 +137,7 @@ the `__next__()` method of an iterator. Try using it on a file:
|
||||
Keep calling `next(f)` until you reach the end of the
|
||||
file. Watch what happens.
|
||||
|
||||
### (b) Supporting Iteration
|
||||
### Exercise 6.2: Supporting Iteration
|
||||
|
||||
On occasion, you might want to make one of your own objects support
|
||||
iteration--especially if your object wraps around an existing
|
||||
@@ -249,7 +249,7 @@ Test it to make sure it works:
|
||||
>>>
|
||||
```
|
||||
|
||||
### (d) Making a more proper container
|
||||
### Exercise 6.3: Making a more proper container
|
||||
|
||||
If making a container class, you often want to do more than just
|
||||
iteration. Modify the `Portfolio` class so that it has some other
|
||||
@@ -310,4 +310,4 @@ Python normally work. For container objects, supporting iteration,
|
||||
indexing, containment, and other kinds of operators is an important
|
||||
part of this.
|
||||
|
||||
[Next](02_Customizing_iteration)
|
||||
[Contents](../Contents) \| [Previous (5.2 Encapsulation)](../05_Classes_objects/02_Classes_encapsulation) \| [Next (6.2 Customizing Iteration)](02_Customizing_iteration)
|
||||
|
||||
@@ -99,7 +99,7 @@ File "<stdin>", line 1, in ? StopIteration
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) A Simple Generator
|
||||
### Exercise 6.4: A Simple Generator
|
||||
|
||||
If you ever find yourself wanting to customize iteration, you should
|
||||
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
|
||||
custom processing in a function and use it to feed a for-loop.
|
||||
The next example looks at a more unusual case.
|
||||
|
||||
### (b) Monitoring a streaming data source
|
||||
### Exercise 6.5: Monitoring a streaming data source
|
||||
|
||||
Generators can be an interesting way to monitor real-time data sources
|
||||
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
|
||||
see if more data has been added (`readline()` will either
|
||||
return new data or an empty string).
|
||||
|
||||
### (c) Using a generator to produce data
|
||||
### Exercise 6.6: Using a generator to produce data
|
||||
|
||||
If you look at the code in part (b), the first part of the code is producing
|
||||
lines of data whereas the statements at the end of the `while` loop are consuming
|
||||
@@ -229,7 +229,7 @@ if __name__ == '__main__':
|
||||
print(f'{name:>10s} {price:>10.2f} {change:>10.2f}')
|
||||
```
|
||||
|
||||
### (d) Watching your portfolio
|
||||
### Exercise 6.7: Watching your portfolio
|
||||
|
||||
Modify the `follow.py` program so that it watches the stream of stock
|
||||
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.
|
||||
example, you could use it to watch server logs, debugging logs, and other similar data sources.
|
||||
That's kind of cool.
|
||||
|
||||
[Next](03_Producers_consumers)
|
||||
[Contents](../Contents) \| [Previous (6.1 Iteration Protocol)](01_Iteration_protocol) \| [Next (6.3 Producer/Consumer)](03_Producers_consumers)
|
||||
|
||||
@@ -101,7 +101,7 @@ You will notice that data incrementally flows through the different functions.
|
||||
For this exercise the `stocksim.py` program should still be running in the background.
|
||||
You’re going to use the `follow()` function you wrote in the previous exercise.
|
||||
|
||||
### (a) Setting up a simple pipeline
|
||||
### Exercise 6.8: Setting up a simple pipeline
|
||||
|
||||
Let's see the pipelining idea in action. Write the following
|
||||
function:
|
||||
@@ -132,7 +132,7 @@ to it as an argument. Now, try this:
|
||||
It might take awhile for output to appear, but eventually you
|
||||
should see some lines containing data for IBM.
|
||||
|
||||
### (b) Setting up a more complex pipeline
|
||||
### Exercise 6.9: Setting up a more complex pipeline
|
||||
|
||||
Take the pipelining idea a few steps further by performing
|
||||
more actions.
|
||||
@@ -156,7 +156,7 @@ Well, that's interesting. What you're seeing here is that the output of the
|
||||
`follow()` function has been piped into the `csv.reader()` function and we're
|
||||
now getting a sequence of split rows.
|
||||
|
||||
### (c) Making more pipeline components
|
||||
### Exercise 6.10: Making more pipeline components
|
||||
|
||||
Let's extend the whole idea into a larger pipeline. In a separate file `ticker.py`,
|
||||
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:
|
||||
...
|
||||
```
|
||||
|
||||
### (d) Filtering data
|
||||
### Exercise 6.11: Filtering data
|
||||
|
||||
Write a function that filters data. For example:
|
||||
|
||||
@@ -262,7 +262,7 @@ for row in rows:
|
||||
print(row)
|
||||
```
|
||||
|
||||
### (e) Putting it all together
|
||||
### Exercise 6.12: Putting it all together
|
||||
|
||||
In the `ticker.py` program, write a function `ticker(portfile, logfile, fmt)`
|
||||
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
|
||||
series of pipeline stages into a single function call (for example,
|
||||
the `parse_stock_data()` function).
|
||||
|
||||
[Next](04_More_generators)
|
||||
[Contents](../Contents) \| [Previous (6.2 Customizing Iteration)](02_Customizing_iteration) \| [Next (6.4 Generator Expressions)](04_More_generators)
|
||||
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ More information at [Generator Tricks for Systems Programmers](http://www.dabeaz
|
||||
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.
|
||||
This exercise continues to build upon that. Make sure the `Data/stocksim.py` is still running.
|
||||
|
||||
### (a) Generator Expressions
|
||||
### Exercise 6.13: Generator Expressions
|
||||
|
||||
Generator expressions are a generator version of a list comprehension.
|
||||
For example:
|
||||
@@ -134,7 +134,7 @@ Thus, if you try another for-loop, you get nothing:
|
||||
>>>
|
||||
```
|
||||
|
||||
### (b) Generator Expressions in Function Arguments
|
||||
### Exercise 6.14: Generator Expressions in Function Arguments
|
||||
|
||||
Generator expressions are sometimes placed into function arguments.
|
||||
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
|
||||
involving list comprehensions. Try replacing these with
|
||||
generator expressions.
|
||||
|
||||
### (c) Code simplification
|
||||
### Exercise 6.15: Code simplification
|
||||
|
||||
Generators expressions are often a useful replacement for
|
||||
small generator functions. For example, instead of writing a
|
||||
@@ -177,3 +177,4 @@ Modify the `ticker.py` program to use generator expressions
|
||||
as appropriate.
|
||||
|
||||
|
||||
[Contents](../Contents) \| [Previous (6.3 Producer/Consumer)](03_Producers_consumers) \| [Next (7 Advanced Topics)](../07_Advanced_Topics/00_Overview)
|
||||
Reference in New Issue
Block a user