Default Function Object Argument Example

We will now present yet another example of where a default argument may prove beneficial. The grabweb.py script, given in Example 11.2 is a simple script whose main purpose is to grab a web page from the Internet and temporarily store it to a local file for analysis. This type of application can be used to test the integrity of a website's pages or to monitor the load on a server (by measuring connectability or download speed). The process() function can be anything we want, presenting an infinite number of uses. The one we chose for this exercise displays the first and last non-blank lines of the retrieved web page. Although this particular example may not prove too useful in the real world, you can imagine what kinds of applications you can build on top of this code.

Listing 11.2. Grabbing Web Pages (grabweb.py)

This script downloads a webpage (defaults to local www server) and displays the first and last non-blank lines of the HTML file. Flexibility is added due to both default arguments of thedownload() function which will allow overriding with different URLs or specification of a different processing function.

1  #!/usr/bin/env python
2
3  from urllib import urlretrieve
4  from string import strip
5
6  def firstnonblank(lines):
7      for eachLine in lines:
8          if strip(eachLine) == '':
9              continue
10         else:
11             return eachLine
12
13 def firstlast(webpage):
14     f = open(webpage)
15     lines = f.readlines()
16     f.close()
17     print firstnonblank(lines),
18     lines.reverse()
19     print firstnonblank(lines),
20
21 def download(url='http://www', 
22         process=firstlast):
23     try:
24         retval = urlretrieve(url)[0]
25     except IOError:
26         retval = None
27     if retval:        # do some
           processing
28         process(retval)
29
30 if __name__ == '__main__':
31     download()

Running this script in our environment gives the following output, although your mileage will definitely vary since you will be viewing a completely different web page altogether.

% grabweb.py
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final
//EN">
</HTML>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset