Class 12 – Monday, March 1
Tabling
An optimist thinks — Elevator close button — Really does something
Look both ways
Agenda
- Nested loops
- Introduce dataset processing
- Introduce web processing
Downloads
- Program c_looper.py
- Program going_rowing.py
- Program rc_printing.py
- Program rc_making.py
- Program column_grabbing.py
To do list
- Complete current homeworks
Datasets
- A dataset is a list whose elements are lists.
- Datasets are sometimes called tables or data sheets
- The elements of a two-dimensional dataset are called rows. The elements of a row are called data values or cells.
- Most of the datasets that we process will come from the web.
- The datasets acquired by programs are often CSV files; that is, the values are separated by commas.
- One of the CSV dataset we will consider is the best selling fictional books of all time
Program c_looper.py
-
Task
- For a user-supplied
n
, prints a line of the form
row : 0 1 2 ... n-1
-
Some program runs
Enter number of columns: 5
row : 0 1 2 3 4
Enter number of columns: 12
row : 0 1 2 3 4 5 6 7 8 9 10 11
Program going_rowing.py
- Demonstrates looping through a dataset
-
Program run
table: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L', 'M']]
the table has 4 rows
row ['A', 'B', 'C'] has 3 columns
row ['D', 'E', 'F'] has 3 columns
row ['G', 'H', 'I'] has 3 columns
row ['J', 'K', 'L', 'M'] has 4 columns
row 0 : ['A', 'B', 'C']
row 1 : ['D', 'E', 'F']
row 2 : ['G', 'H', 'I']
row 3 : ['J', 'K', 'L', 'M']
Program rc_printing.py
- Demonstrates printing a table of values (cells) for a user-specicied number of rows and columns.
- The value of cell should be the sum of its row and column indices.
-
Some program runs
Number of rows and columns: 3 4
0 1 2 3
1 2 3 4
2 3 4 5
Number of rows and columns: 4 5
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
Program rc_making.py
- Demonstrates producing a table of values for a user-specicied number of rows and columns.
- The value of cell should be the sum of its row and column indices.
-
Some program runs
Number of rows and columns: 3 4
row 0
[0, 1, 2, 3]
row 1
[1, 2, 3, 4]
row 2
[2, 3, 4, 5]
table = [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
Number of rows and columns: 2 5
row 0
[0, 1, 2, 3, 4]
row 1
[1, 2, 3, 4, 5]
table = [[0, 1, 2, 3, 4], [1, 2, 3, 4, 5]]
Program column_grabbing.py
- For a user-specified column index produce a list of values for that column
-
Some program runs
table = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L', 'M']]
Enter column of interest: 0
row ['A', 'B', 'C'] : column 0 cell: A
row ['D', 'E', 'F'] : column 0 cell: D
row ['G', 'H', 'I'] : column 0 cell: G
row ['J', 'K', 'L', 'M'] : column 0 cell: J
Column 0 : ['A', 'D', 'G', 'J']
table = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L', 'M']]
Enter column of interest: 2
row ['A', 'B', 'C'] : column 2 cell: C
row ['D', 'E', 'F'] : column 2 cell: F
row ['G', 'H', 'I'] : column 2 cell: I
row ['J', 'K', 'L', 'M'] : column 2 cell: L
Column 2 : ['C', 'F', 'I', 'L']
Web pages
Our introduction to interacting with the web in CS 1112 is intentionally simple. Industrial-strength web applications also require familiarity with other and more powerful URL modules. There is an external library requests worth checking if you have further interest.
For now the only thing we is access to the module urllib.request
. The module supports working with URLs.
import urllib.request
- The only thing we care about in the modudle is its function
urllib.request.urlopen()
that returns a connector to a URL resource (think web page). Sample usage:
stream = urllib.request.urlopen( link )
- If you care (and I do not), officially the value returned by
urlopen()
is anhttp.client.HTTPResponse
.
- All we care about is that a
stream
returned byurlopen()
has a functionread()
to get the contents of the web resource indicated bylink
.
page = stream.read()
- The contents provided by
read()
is a string encoded in a web format rather than as regular text. We can be decode it with string functiondecode()
.
text = page.decode( 'UTF-8' )
The above assignment sets text to be the decoded contents of the url resource named by
link
; that istext
is a string equally the contents of the url resource indicted by;ink
.
- The four statements form a template for getting the contents of a URL resource in string format.
import urllib.request # get module access
stream = urllib.request.urlopen( link ) # open connector to the link web resource
page = stream.read() # read contents of the resource
text = page.decode( 'UTF-8' ) # decode contents as normal text string
- What happens next is problem-dependent.
Program master_plan.py
- Displays the word of the day from the CS 1112 web file
word-of-the-day
.
???
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |