Class 12 – Monday, September 20
Tabling
An optimist thinks — Elevator close button — Really does something
Look both ways
Agenda
- Introduce web processing
- Introduce dataset processing
- Introduce nested loops
Downloads
- Module url.py
- Provides access to web data
- Program word_of_the_day.py
- Accesses a web file through the use of local module url.py
- Program secret_revealed.py
- Reveals the super power of a class mate
- Program weathering_heights.py
- You don't need a weather person to know which way the wind blows
- Provides a current forecast for a user-specified zip code
- Program going_rowing.py
- Introduces dataset processing
- Program rc_printing.py
- Introduces nested looping
To do list
- Complete current homeworks
- Review artifacts
What is data?
- Facts and figures
What is information
- Knowledge
What is a dataset
- A dataset is a list whose elements are lists. The elements of the sublists are data
- 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
Web pages
- We have created a module url.py to provide some helpful web access.
- Our introduction to interacting with the web in CS 1112 is intentionally simple. Industrial-strength web applications require familiarity with other and more powerful URL modules.
- There is an external library requests worth checking if you have further interest.
Program word_of_the_day.py
- Displays the word of the day from the CS 1112 web file
word-of-the-day
.
- Uses local module
url.py
???
Program secret_revealed.py
- Displays the secret super power of a classmate
Enter id: ???
???
Program weathering_heights.py
- Makes use of module url.py to access a weather forecast for a user-specified zipcode by querying the forecast.weather.gov website
-
Insight by examples
- https://forecast.weather.gov/zipcity.php?inputstring=22903 (Charlottesville, VA)
<!DOCTYPE html> <html class="no-js"> ...
<div class="row row-odd row-forecast"><div class="col-sm-2 forecast-label"><b>This Afternoon</b></div> <div class="col-sm-10 forecast-text"> Partly sunny, with a high near 81. East wind 6 to 8 mph. </div>
</body> </html>
- https://forecast.weather.gov/zipcity.php?inputstring=07452 (Glen Rock, NJ)
<!DOCTYPE html> <html class="no-js"> ...
</body> </html>
<div class="row row-odd row-forecast"><div class="col-sm-2 forecast-label"><b>Today</b></div> <div class="col-sm-10 forecast-text"> Showers and thunderstorms. Temperature falling to around 61 by 3pm. South wind around 15 mph becoming west northwest in the afternoon. Winds could gust as high as 25 mph. Chance of precipitation is 100%. New rainfall amounts between a half and three quarters of an inch possible. </div>
- https://forecast.weather.gov/zipcity.php?inputstring=55455 (Minneapolis, MN)
<!DOCTYPE html> <html class="no-js"> ...
<div class="row row-odd row-forecast"><div class="col-sm-2 forecast-label"><b>Today</b></div> <div class="col-sm-10 forecast-text">Sunny, with a high near 76. Southeast wind 3 to 7 mph. </div>
</body> </html>
-
Some program runs
Enter zipcode: 22903
22903 forecast: Partly sunny, with a high near 81. East wind 6 to 8 mph.
Enter zipcode: 55455
55455 forecast: Showers and thunderstorms. Temperature falling to around 61 by 3pm. South wind around 15 mph becoming west northwest in the afternoon. Winds could gust as high as 25 mph. Chance of precipitation is 100%. New rainfall amounts between three quarters and one inch possible.
Enter zipcode: 07452
07452 forecast: Sunny, with a high near 76. Southeast wind 3 to 7 mph.
-
Algorithm
- Get access to url get web content capabilities
- Define helpful constants — base query, forecast delimiters, lengths of forecast delimiteres
- Get user zip code of interest
- Create the query link to get forecast from national weather service
- Get the contents of the page found at the forecast link
- Find indexes of query front and rear delimiters
- Find indexes of forecast front and rear
- Get the forecast substring from the page
- Print the forecast
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