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.
Example 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.
<$nopage>
001 1 #!/usr/bin/env python
002 2
003 3 from urllib import urlretrieve
004 4 from string import strip
005 5
006 6 def firstnonblank(lines):
007 7 for eachLine in lines:
008 8 if strip(eachLine) == '':
009 9 continue <$nopage>
010 10 else: <$nopage>
011 11 return eachLine
012 12
013 13 def firstlast(webpage):
014 14 f = open(webpage)
015 15 lines = f.readlines()
016 16 f.close()
017 17 print firstnonblank(lines),
018 18 lines.reverse()
019 19 print firstnonblank(lines),
020 20
021 21 def download(url='http://www', \
022 22 process=firstlast):
023 23 try: <$nopage>
024 24 retval = urlretrieve(url)[0]
025 25 except IOError:
026 26 retval = None
027 27 if retval: # do some
028 processing
029 28 process(retval)
030 29
031 30 if __name__ == '__main__':
032 31 download()
033 <$nopage>
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>