6.5. Documentation of the wxWeather Program

See also

An overall discussion of the design of the wxWeather program along with the Python module requirements and a link to download the program is found in the previous section (Example of Parsing HTML Web Pages with Html5lib).

6.5.1. Screen Shots

Here is a short description of how to use the wxWeather program along with screen shots from the program.

The initial forecast location (zip code) is hard coded to 67401 for Salina, Kansas, but it can easily be changed in the source code.

../_images/forecast.png

When the program starts, it shows the current conditions, immediate forecast and a quick five day forecast in a table.

If you later want to see an updated forecast and current conditions, click on the Reload Forecast button.

../_images/newzip.png

If you want to see the forecast and current conditions for a different location, click on the “Change Forecast Location” button. Here, I enter the zip code for the other campus of Kansas State University.

../_images/changedlocation.png

Clicking the OK button displays the forecast for the new location.

../_images/badzip.png

This screen is displayed, if a mistake is made while entering the zip code.

6.5.2. Source Code Descriptions

Here is a little bit of documentation about the more important functions and classes from the wxWeather program. This documentation is auto-generated from the Python source code and doc strings contained in the source code.

File: wxWeather.py

Author: Tim Bower, Copyright 2009, Open Source Apache License – See non-docstring source code comments for license details.

HTML parser - extract the current conditions and 5-day forecast from www.wunderground.com and display it in a simple wxPython graphical window.

A component of Project 3 for Network Programming Class.

class wxWeather.WeatherPanel(parent, id)

The main GUI class for wxWeather – no real reason why this code is at the panel level instead of the frame level with the panel being just one of the objects in the frame. I started with wxHTML, which does it this way.

class WeatherPanel inherits wx.Panel and adds a wx.html.HtmlWindow to diplay HTML content. The HTML content comes from getWeather(), which gets data from from wuunderground and reformats it.

OnExit(event)

Call back from clicking the close window (X) icon on the frame. Close the application by Destroying the object

OnNewZip(event)

Call back from Change Forecast Location button

Onreload(event)

Call back from Reload Forecast button

load_weather(zipcode=None)

Validate zip code and call getWeather()

class wxWeather.mytable

As the HTML is parsed, any tables get stored here. This just helps to orgaize and later retrieve the data.

addData(data)

New data to add to the current table cell. Items are stored in a list. Thus, a 2 dimensional HTML table is stored as a Python 3-dimensional list.

getHTML()

Return the table data formated as HTML. Returns a list of strings. This is debugging code - not used in the final program.

getTableData()
Return type:a 3-dimensional list of strings [[[] .. ] .. ]

Returns the text data held in the table

printTable()

A generic table printer, depending on the data, it may or or may not get the job done, but could help with debugging. In most cases, you will want to use getTableData() and format the data per your needs.

tdEnd()

A td end-tag (end of column)

tdTag()

A td start-tag (new column)

trEnd()

A tr end-tag (end of row)

trTag()

A tr start-tag (new row)

txtLen()

Get the length of the table – useful to guess which table we are working with.

txtMatch(st)

Boolean: Determine if table data holds a desired string

xpose()

Transpose a table – rows and columns switch. This is probably the trickiest line of code used in this class. Without using the nested list comprehension, it would be much longer.

class wxWeather.Current_conditions

Class to collect and report on the current weather conditions, which are spread out over multiple tables.

get_html()

Return current conditions data with HTML formatting.

needs_values

A boolean to test to see if we are still looking for current condition data.

set_currents(data)
Parameters:data – a list of (variable, value) tuples.

Collect current weather condition information from token data.

set_pending(data)

This is exactly why parsing HTML is ugly, messy work. In some cases, the start tag processed in set_currents() tips us off to look for a current condition value in the next character token that we come across. The data in this token should be what we are looking for.

wxWeather.getWeather(zipcode='67401')
Parameters:zipcode – 5 digit zip code string (optional)

Grab a page from wunderground and parse it, strip out a quick summary and return the summary with simple HTML formating.

wxWeather.getWUTable(tbl)

If the current table is what we want, return it as HTML formated as we want it. This function will the first to be modified when wuWeather ever changes the format of the HTML.