Added links. Renumber

This commit is contained in:
David Beazley
2020-05-26 15:50:07 -05:00
parent 45c7ec1f4a
commit 79e91fd7dd
9 changed files with 33 additions and 25 deletions

View File

@@ -87,7 +87,7 @@ These are not commonly used except when writing library functions.
## Exercises ## Exercises
### (a) A simple example of variable arguments ### Exercise 7.1: A simple example of variable arguments
Try defining the following function: Try defining the following function:
@@ -106,7 +106,7 @@ Try defining the following function:
Notice how the parameter `*more` collects all of the extra arguments. Notice how the parameter `*more` collects all of the extra arguments.
### (b) Passing tuple and dicts as arguments ### Exercise 7.2: Passing tuple and dicts as arguments
Suppose you read some data from a file and obtained a tuple such as Suppose you read some data from a file and obtained a tuple such as
this: this:
@@ -146,7 +146,7 @@ Stock('GOOG', 100, 490.1)
>>> >>>
``` ```
### (c) Creating a list of instances ### Exercise 7.3: Creating a list of instances
In your `report.py` program, you created a list of instances In your `report.py` program, you created a list of instances
using code like this: using code like this:
@@ -169,7 +169,7 @@ def read_portfolio(filename):
You can simplify that code using `Stock(**d)` instead. Make that change. You can simplify that code using `Stock(**d)` instead. Make that change.
### (d) Argument pass-through ### Exercise 7.4: Argument pass-through
The `fileparse.parse_csv()` function has some options for changing the The `fileparse.parse_csv()` function has some options for changing the
file delimiter and for error reporting. Maybe you'd like to expose those file delimiter and for error reporting. Maybe you'd like to expose those
@@ -211,4 +211,4 @@ Now, try silencing the errors:
>>> >>>
``` ```
[Next](02_Anonymous_function) [Contents](../Contents) \| [Previous (6.4 Generator Expressions)](../06_Generators/04_More_generators) \| [Next (7.2 Anonymous Functions)](02_Anonymous_function)

View File

@@ -108,7 +108,7 @@ Stock('IBM', 100, 70.44)
>>> >>>
``` ```
### (a) Sorting on a field ### Exercise 7.5: Sorting on a field
Try the following statements which sort the portfolio data Try the following statements which sort the portfolio data
alphabetically by stock name. alphabetically by stock name.
@@ -129,7 +129,7 @@ In this part, the `stock_name()` function extracts the name of a stock from
a single entry in the `portfolio` list. `sort()` uses the result of a single entry in the `portfolio` list. `sort()` uses the result of
this function to do the comparison. this function to do the comparison.
### (b) Sorting on a field with lambda ### Exercise 7.6: Sorting on a field with lambda
Try sorting the portfolio according the number of shares using a Try sorting the portfolio according the number of shares using a
`lambda` expression: `lambda` expression:
@@ -158,4 +158,4 @@ Note: `lambda` is a useful shortcut because it allows you to
define a special processing function directly in the call to `sort()` as define a special processing function directly in the call to `sort()` as
opposed to having to define a separate function first (as in part a). opposed to having to define a separate function first (as in part a).
[Next](03_Returning_functions) [Contents](../Contents) \| [Previous (7.1 Variable Arguments)](01_Variable_arguments) \| [Next (7.3 Returning Functions)](03_Returning_function)

View File

@@ -119,7 +119,7 @@ You can write functions that make code.
## Exercises ## Exercises
### (a) Using Closures to Avoid Repetition ### Exercise 7.7: Using Closures to Avoid Repetition
One of the more powerful features of closures is their use in One of the more powerful features of closures is their use in
generating repetitive code. If you refer back to exercise 5.2 generating repetitive code. If you refer back to exercise 5.2
@@ -195,7 +195,7 @@ Try creating an instance and verifying that type-checking works.
>>> >>>
``` ```
### (b) Simplifying Function Calls ### Exercise 7.8: Simplifying Function Calls
In the above example, users might find calls such as In the above example, users might find calls such as
`typedproperty('shares', int)` a bit verbose to type--especially if `typedproperty('shares', int)` a bit verbose to type--especially if
@@ -226,9 +226,9 @@ Ah, that's a bit better. The main takeaway here is that closures and `lambda`
can often be used to simplify code and eliminate annoying repetition. This can often be used to simplify code and eliminate annoying repetition. This
is often good. is often good.
### (c) Putting it into practice ### Exercise 7.9: Putting it into practice
Rewrite the `Stock` class in the file `stock.py` so that it uses typed properties Rewrite the `Stock` class in the file `stock.py` so that it uses typed properties
as shown. as shown.
[Next](04_Function_decorators) [Contents](../Contents) \| [Previous (7.2 Anonymous Functions)](02_Anonymous_function) \| [Next (7.4 Decorators)](04_Function_decorators)

View File

@@ -103,7 +103,7 @@ However, the previous example is a good illustration of how their use tends to a
## Exercises ## Exercises
### (a) A decorator for timing ### Exercise 7.10: A decorator for timing
If you define a function, its name and module are stored in the If you define a function, its name and module are stored in the
`__name__` and `__module__` attributes. For example: `__name__` and `__module__` attributes. For example:
@@ -149,4 +149,4 @@ Discussion: This `@timethis` decorator can be placed in front of any
function definition. Thus, you might use it as a diagnostic tool for function definition. Thus, you might use it as a diagnostic tool for
performance tuning. performance tuning.
[Next](05_Decorated_methods) [Contents](../Contents) \| [Previous (7.3 Returning Functions)](03_Returning_functions) \| [Next (7.5 Decorated Methods)](05_Decorated_methods)

View File

@@ -121,7 +121,7 @@ Start this exercise by defining a `Date` class. For example:
>>> >>>
``` ```
### (a) Class Methods ### Exercise 7.11: Class Methods
A common use of class methods is to provide alternate constructors A common use of class methods is to provide alternate constructors
(epecially since Python doesn't support overloaded methods). Modify (epecially since Python doesn't support overloaded methods). Modify
@@ -161,7 +161,7 @@ Yow!
>>> >>>
``` ```
### (b) Class Methods in Practice ### Exercise 7.12: Class Methods in Practice
In your `report.py` and `portfolio.py` files, the creation of a `Portfolio` In your `report.py` and `portfolio.py` files, the creation of a `Portfolio`
object is a bit muddled. For example, the `report.py` program has code like this: object is a bit muddled. For example, the `report.py` program has code like this:
@@ -258,3 +258,4 @@ To use this new Portfolio class, you can now write code like this:
Make these changes to the `Portfolio` class and modify the `report.py` Make these changes to the `Portfolio` class and modify the `report.py`
code to use the class method. code to use the class method.
[Contents](../Contents) \| [Previous (7.4 Decorators)](04_Function_decorators) \| [Next (8 Testing and Debugging)](../08_Testing_debugging/00_Overview)

View File

@@ -207,7 +207,7 @@ for Exercise 7.3. If, for some reason, that's not working,
you might want to copy the solution from `Solutions/7_3` to your working you might want to copy the solution from `Solutions/7_3` to your working
directory. directory.
### (a) Writing Unit Tests ### Exercise 8.1: Writing Unit Tests
In a separate file `test_stock.py`, write a set a unit tests In a separate file `test_stock.py`, write a set a unit tests
for the `Stock` class. To get you started, here is a small for the `Stock` class. To get you started, here is a small
@@ -261,4 +261,4 @@ class TestStock(unittest.TestCase):
s.shares = '100' s.shares = '100'
``` ```
[Next](02_Logging) [Contents](../Contents) \| [Previous (7.5 Decorated Methods)](../07_Advanced_Topics/05_Decorated_methods) \| [Next (8.2 Logging)](02_Logging)

View File

@@ -125,7 +125,7 @@ However, the code that uses logging doesn't have to worry about that.
## Exercises ## Exercises
### (a) Adding logging to a module ### Exercise 8.2: Adding logging to a module
In Exercise 3.3, you added some error handling to the In Exercise 3.3, you added some error handling to the
`fileparse.parse_csv()` function. It looked like this: `fileparse.parse_csv()` function. It looked like this:
@@ -282,7 +282,7 @@ Turn off all, but the most critical logging messages:
>>> >>>
``` ```
### (b) Adding Logging to a Program ### Exercise 8.3: Adding Logging to a Program
To add logging to an application, you need to have some mechanism to To add logging to an application, you need to have some mechanism to
initialize the logging module in the main module. One way to initialize the logging module in the main module. One way to
@@ -302,4 +302,4 @@ logging.basicConfig(
Again, you'd need to put this someplace in the startup steps of your Again, you'd need to put this someplace in the startup steps of your
program. program.
[Next](03_Debugging) [Contents](../Contents) \| [Previous (8.1 Testing)](01_Testing) \| [Next (8.3 Debugging)](03_Debugging)

View File

@@ -145,3 +145,10 @@ For breakpoints location is one of the following.
(Pdb) b module.foo # Function foo() in a module (Pdb) b module.foo # Function foo() in a module
``` ```
## Exercises
### Exercise 8.4: Bugs? What Bugs?
It runs. Ship it!
[Contents](../Contents) \| [Previous (8.2 Logging)](02_Logging) \| [Next (9 Packages)](../09_Packages/00_Overview)

View File

@@ -245,7 +245,7 @@ typedproperty.py # Typed class properties
In this exercise, we're going to clean up the code and put it into In this exercise, we're going to clean up the code and put it into
a common package. a common package.
### (a) Making a simple package ### Exercise 9.1: Making a simple package
Make a directory called `porty/` and put all of the above Python Make a directory called `porty/` and put all of the above Python
files into it. Additionally create an empty `__init__.py` file and files into it. Additionally create an empty `__init__.py` file and
@@ -298,7 +298,7 @@ from .fileparse import parse_csv
... ...
``` ```
### (b) Making an application directory ### Exercise 9.2: Making an application directory
Putting all of your code into a "package" isn't often enough for an Putting all of your code into a "package" isn't often enough for an
application. Sometimes there are supporting files, documentation, application. Sometimes there are supporting files, documentation,
@@ -358,7 +358,7 @@ shell % python3 -m porty.report portfolio.csv prices.csv txt
shell % shell %
``` ```
### (c) Top-level Scripts ### Exercise 9.3: Top-level Scripts
Using the `python -m` command is often a bit weird. You may want to Using the `python -m` command is often a bit weird. You may want to
write a top level script that simply deals with the oddities of packages. write a top level script that simply deals with the oddities of packages.
@@ -412,4 +412,4 @@ porty-app/
typedproperty.py typedproperty.py
``` ```
[Next](02_Third_party) [Contents](../Contents) \| [Previous (8.3 Debugging)](../08_Testing_debugging/03_Debugging) \| [Next (9.2 Third Party Packages)](02_Third_party)