Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 11. Functions > Default Function Object Argument Example

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()


					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial