Added links
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# 3.1 Python Scripting
|
# 3.1 Scripting
|
||||||
|
|
||||||
In this part we look more closely at the practice of writing Python
|
In this part we look more closely at the practice of writing Python
|
||||||
scripts.
|
scripts.
|
||||||
@@ -233,7 +233,7 @@ for row in report:
|
|||||||
|
|
||||||
In this exercise, we’re going take this program and organize it a little more strongly around the use of functions.
|
In this exercise, we’re going take this program and organize it a little more strongly around the use of functions.
|
||||||
|
|
||||||
### (a) Structuring a program as a collection of functions
|
### Exercise 3.1: Structuring a program as a collection of functions
|
||||||
|
|
||||||
Modify your `report.py` program so that all major operations,
|
Modify your `report.py` program so that all major operations,
|
||||||
including calculations and output, are carried out by a collection of
|
including calculations and output, are carried out by a collection of
|
||||||
@@ -242,7 +242,7 @@ functions. Specifically:
|
|||||||
* Create a function `print_report(report)` that prints out the report.
|
* Create a function `print_report(report)` that prints out the report.
|
||||||
* Change the last part of the program so that it is nothing more than a series of function calls and no other computation.
|
* Change the last part of the program so that it is nothing more than a series of function calls and no other computation.
|
||||||
|
|
||||||
### (b) Creating a function for program execution
|
### Exercise 3.2: Creating a function for program execution
|
||||||
|
|
||||||
Take the last part of your program and package it into a single function `portfolio_report(portfolio_filename, prices_filename)`.
|
Take the last part of your program and package it into a single function `portfolio_report(portfolio_filename, prices_filename)`.
|
||||||
Have the function work so that the following function call creates the report as before:
|
Have the function work so that the following function call creates the report as before:
|
||||||
@@ -272,4 +272,4 @@ For example, try these statements interactively after running your program:
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
[Next](02_More_functions)
|
[Contents](../Contents) \| [Previous (2.7 Object Model)](../02_Working_with_data/07_Objects) \| [Next (3.2 More on Functions)](02_More_functions)
|
||||||
|
|||||||
@@ -253,7 +253,7 @@ our goal.
|
|||||||
|
|
||||||
Start this exercise by creating a new file called `fileparse.py`. This is where we will be doing our work.
|
Start this exercise by creating a new file called `fileparse.py`. This is where we will be doing our work.
|
||||||
|
|
||||||
### (a) Reading CSV Files
|
### Exercise 3.3: Reading CSV Files
|
||||||
|
|
||||||
To start, let’s just focus on the problem of reading a CSV file into a
|
To start, let’s just focus on the problem of reading a CSV file into a
|
||||||
list of dictionaries. In the file `fileparse.py`, define a simple
|
list of dictionaries. In the file `fileparse.py`, define a simple
|
||||||
@@ -300,7 +300,7 @@ Hint: `python3 -i fileparse.py`.
|
|||||||
This is great except that you can’t do any kind of useful calculation with the data because everything is represented as a string.
|
This is great except that you can’t do any kind of useful calculation with the data because everything is represented as a string.
|
||||||
We’ll fix this shortly, but let’s keep building on it.
|
We’ll fix this shortly, but let’s keep building on it.
|
||||||
|
|
||||||
### (b) Building a Column Selector
|
### Exercise 3.4: Building a Column Selector
|
||||||
|
|
||||||
In many cases, you’re only interested in selected columns from a CSV file, not all of the data.
|
In many cases, you’re only interested in selected columns from a CSV file, not all of the data.
|
||||||
Modify the `parse_csv()` function so that it optionally allows user-specified columns to be picked out as follows:
|
Modify the `parse_csv()` function so that it optionally allows user-specified columns to be picked out as follows:
|
||||||
@@ -318,7 +318,7 @@ Modify the `parse_csv()` function so that it optionally allows user-specified co
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
An example of a column selector was given in Section 2.5.
|
An example of a column selector was given in [Exercise 2.23](../02_Working_with_data/06_List_comprehension).
|
||||||
However, here’s one way to do it:
|
However, here’s one way to do it:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -394,7 +394,7 @@ When you read a row of data from the file, the indices are used to filter it:
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
### (c) Performing Type Conversion
|
### Exercise 3.5: Performing Type Conversion
|
||||||
|
|
||||||
Modify the `parse_csv()` function so that it optionally allows type-conversions to be applied to the returned data.
|
Modify the `parse_csv()` function so that it optionally allows type-conversions to be applied to the returned data.
|
||||||
For example:
|
For example:
|
||||||
@@ -410,7 +410,7 @@ For example:
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
You already explored this in Exercise 2.7. You'll need to insert the
|
You already explored this in [Exercise 2.24](../02_Working_with_data/07_Objects). You'll need to insert the
|
||||||
following fragment of code into your solution:
|
following fragment of code into your solution:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -420,7 +420,7 @@ if types:
|
|||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
### (d) Working with Headers
|
### Exercise 3.6: Working with Headers
|
||||||
|
|
||||||
Some CSV files don’t include any header information.
|
Some CSV files don’t include any header information.
|
||||||
For example, the file `prices.csv` looks like this:
|
For example, the file `prices.csv` looks like this:
|
||||||
@@ -448,7 +448,7 @@ line of data isn’t interpreted as a header line. Also, you’ll need to
|
|||||||
make sure you don’t create dictionaries as there are no longer any
|
make sure you don’t create dictionaries as there are no longer any
|
||||||
column names to use for keys.
|
column names to use for keys.
|
||||||
|
|
||||||
### (e) Picking a different column delimitier
|
### Exercise 3.7: Picking a different column delimitier
|
||||||
|
|
||||||
Although CSV files are pretty common, it’s also possible that you could encounter a file that uses a different column separator such as a tab or space.
|
Although CSV files are pretty common, it’s also possible that you could encounter a file that uses a different column separator such as a tab or space.
|
||||||
For example, the file `Data/portfolio.dat` looks like this:
|
For example, the file `Data/portfolio.dat` looks like this:
|
||||||
@@ -486,6 +486,4 @@ You can use it to parse arbitrary CSV files, select out columns of
|
|||||||
interest, perform type conversions, without having to worry too much
|
interest, perform type conversions, without having to worry too much
|
||||||
about the inner workings of files or the `csv` module.
|
about the inner workings of files or the `csv` module.
|
||||||
|
|
||||||
Nice!
|
[Contents](../Contents) \| [Previous (3.1 Scripting)](01_Script) \| [Next (3.3 Error Checking)](03_Error_checking)
|
||||||
|
|
||||||
[Next](03_Error_checking)
|
|
||||||
|
|||||||
@@ -301,7 +301,7 @@ resources are released. `with` only works with certain objects.
|
|||||||
|
|
||||||
## Exercises
|
## Exercises
|
||||||
|
|
||||||
### (a) Raising exceptions
|
### Exercise 3.8: Raising exceptions
|
||||||
|
|
||||||
The `parse_csv()` function you wrote in the last section allows
|
The `parse_csv()` function you wrote in the last section allows
|
||||||
user-specified columns to be selected, but that only works if the
|
user-specified columns to be selected, but that only works if the
|
||||||
@@ -336,7 +336,7 @@ headers, but simultaneously specifying that there are no headers).
|
|||||||
|
|
||||||
This indicates a programming error on the part of the calling code.
|
This indicates a programming error on the part of the calling code.
|
||||||
|
|
||||||
### (b) Catching exceptions
|
### Exercise 3.9: Catching exceptions
|
||||||
|
|
||||||
The `parse_csv()` function you wrote is used to process the entire
|
The `parse_csv()` function you wrote is used to process the entire
|
||||||
contents of a file. However, in the real-world, it’s possible that
|
contents of a file. However, in the real-world, it’s possible that
|
||||||
@@ -373,7 +373,7 @@ Row 7: Reason invalid literal for int() with base 10: ''
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
### (c) Silencing Errors
|
### Exercise 3.10: Silencing Errors
|
||||||
|
|
||||||
Modify the `parse_csv()` function so that parsing error messages can be silenced if explicitly desired by the user.
|
Modify the `parse_csv()` function so that parsing error messages can be silenced if explicitly desired by the user.
|
||||||
For example:
|
For example:
|
||||||
@@ -390,4 +390,4 @@ most programs. As a general rule, you shouldn’t silently ignore
|
|||||||
errors. Instead, it’s better to report problems and to give the user
|
errors. Instead, it’s better to report problems and to give the user
|
||||||
an option to the silence the error message if they choose to do so.
|
an option to the silence the error message if they choose to do so.
|
||||||
|
|
||||||
[Next](04_Modules)
|
[Contents](../Contents) \| [Previous (3.2 More on Functions)](02_More_functions) \| [Next (3.4 Modules)](04_Modules)
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ make sure you are running Python in a proper environment. Modules
|
|||||||
are usually when programmers encounter problems with the current working
|
are usually when programmers encounter problems with the current working
|
||||||
directory or with Python's path settings.
|
directory or with Python's path settings.
|
||||||
|
|
||||||
### (a) Module imports
|
### Exercise 3.11: Module imports
|
||||||
|
|
||||||
In section 3, we created a general purpose function `parse_csv()` for parsing the contents of CSV datafiles.
|
In section 3, we created a general purpose function `parse_csv()` for parsing the contents of CSV datafiles.
|
||||||
|
|
||||||
@@ -268,7 +268,7 @@ Try importing a function so that you don’t need to include the module name:
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
### (b) Using your library module
|
### Exercise 3.12: Using your library module
|
||||||
|
|
||||||
In section 2, you wrote a program `report.py` that produced a stock report like this:
|
In section 2, you wrote a program `report.py` that produced a stock report like this:
|
||||||
|
|
||||||
@@ -292,7 +292,7 @@ and `read_prices()` functions to use the `parse_csv()` function.
|
|||||||
Use the interactive example at the start of this exercise as a guide.
|
Use the interactive example at the start of this exercise as a guide.
|
||||||
Afterwards, you should get exactly the same output as before.
|
Afterwards, you should get exactly the same output as before.
|
||||||
|
|
||||||
### (c) Using more library imports
|
### Exercise 3.14: Using more library imports
|
||||||
|
|
||||||
In section 1, you wrote a program `pcost.py` that read a portfolio and computed its cost.
|
In section 1, you wrote a program `pcost.py` that read a portfolio and computed its cost.
|
||||||
|
|
||||||
@@ -314,4 +314,4 @@ also contains `read_portfolio()` and `read_prices()` functions. And
|
|||||||
finally, `pcost.py` which computes the portfolio cost, but makes use
|
finally, `pcost.py` which computes the portfolio cost, but makes use
|
||||||
of the code written for the `report.py` program.
|
of the code written for the `report.py` program.
|
||||||
|
|
||||||
[Next](05_Main_module)
|
[Contents](../Contents) \| [Previous (3.3 Error Checking)](03_Error_checking) \| [Next (3.5 Main Module)](05_Main_module)
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
## Exercises
|
## Exercises
|
||||||
|
|
||||||
### (a) `main()` functions
|
### Exercise 3.15: `main()` functions
|
||||||
|
|
||||||
In the file `report.py` add a `main()` function that accepts a list of command line options and produces the same output as before.
|
In the file `report.py` add a `main()` function that accepts a list of command line options and produces the same output as before.
|
||||||
You should be able to run it interatively like this:
|
You should be able to run it interatively like this:
|
||||||
@@ -278,7 +278,7 @@ Total cost: 44671.15
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
### (b) Making Scripts
|
### Exercise 3.16: Making Scripts
|
||||||
|
|
||||||
Modify the `report.py` and `pcost.py` programs so that they can execute as a script on the command line:
|
Modify the `report.py` and `pcost.py` programs so that they can execute as a script on the command line:
|
||||||
|
|
||||||
@@ -297,3 +297,5 @@ bash $ python3 report.py Data/portfolio.csv Data/prices.csv
|
|||||||
bash $ python3 pcost.py Data/portfolio.csv
|
bash $ python3 pcost.py Data/portfolio.csv
|
||||||
Total cost: 44671.15
|
Total cost: 44671.15
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[Contents](../Contents) \| [Previous (3.4 Modules)](04_Modules) \| [Next (3.6 Design Discussion)](06_Design_discussion)
|
||||||
@@ -85,7 +85,7 @@ Don't restrict your options. With great flexibility comes great power.
|
|||||||
|
|
||||||
## Exercise
|
## Exercise
|
||||||
|
|
||||||
### (a)From filenames to file-like objects
|
### Exercise 3.17: From filenames to file-like objects
|
||||||
|
|
||||||
In this section, you worked on a file `fileparse.py` that contained a
|
In this section, you worked on a file `fileparse.py` that contained a
|
||||||
function `parse_csv()`. The function worked like this:
|
function `parse_csv()`. The function worked like this:
|
||||||
@@ -123,10 +123,12 @@ In this new code, what happens if you pass a filename as before?
|
|||||||
With flexibility comes power and with power comes responsibility. Sometimes you'll
|
With flexibility comes power and with power comes responsibility. Sometimes you'll
|
||||||
need to be careful.
|
need to be careful.
|
||||||
|
|
||||||
### (b) Fixing existing functions
|
### Exercise 3.18: Fixing existing functions
|
||||||
|
|
||||||
Fix the `read_portfolio()` and `read_prices()` functions in the
|
Fix the `read_portfolio()` and `read_prices()` functions in the
|
||||||
`report.py` file so that they work with the modified version of
|
`report.py` file so that they work with the modified version of
|
||||||
`parse_csv()`. This should only involve a minor modification.
|
`parse_csv()`. This should only involve a minor modification.
|
||||||
Afterwards, your `report.py` and `pcost.py` programs should work
|
Afterwards, your `report.py` and `pcost.py` programs should work
|
||||||
the same way they always did.
|
the same way they always did.
|
||||||
|
|
||||||
|
[Contents](../Contents) \| [Previous (3.5 Main module)](05_Main_module) \| [Next (4 Classes)](../04_Classes_objects/00_Overview)
|
||||||
Reference in New Issue
Block a user