Exercise 9.1 - Solution

(a) Template strings

# stockreport.py

import sys
import urllib
import csv
import string

output = string.Template("""
<p>
<b>$symbol ($name)</b><br>
<font size=+2><b>$price</b></font>
$change ($percent)
$date $time<br>
<table>
   <tr>
      <td>Open:</td><td>$open</td><td>Volume:</td><td>$volume</td>
   </tr>
   <tr>
      <td>High:</td><td>$high</td><td>Low:</td><td>$low</td>
   </tr>
</table>
</p>
""")

if len(sys.argv) != 2:
    print >>sys.stderr,"Usage: %s symbol" % sys.argv[0]
    raise SystemExit(1)

symbol = sys.argv[1]

u      = urllib.urlopen("http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1p2ohgv&s="+symbol)
colnames = ['symbol','name','price','date','time','change','percent','open','high','low','volume']
stocks = csv.DictReader(u,colnames)

for s in stocks:
    print output.substitute(s)

(b) Using Template Files

First create a file stockreport_template.html that contains the following:

<p>
<b>$symbol ($name)</b><br>
<font size=+2><b>$price</b></font>
$change ($percent)
$date $time<br>
<table>
   <tr>
      <td>Open:</td><td>$open</td><td>Volume:</td><td>$volume</td>
   </tr>
   <tr>
      <td>High:</td><td>$high</td><td>Low:</td><td>$low</td>
   </tr>
</table>
</p>

Now, here is the finished code:

# stockreport.py

import sys
import urllib
import csv
import string

if len(sys.argv) != 2:
    print >>sys.stderr,"Usage: %s symbol" % sys.argv[0]
    raise SystemExit(1)

symbol = sys.argv[1]
output = string.Template(open("stockreport_template.html").read())

u      = urllib.urlopen("http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1p2ohgv&s="+symbol)
colnames = ['symbol','name','price','date','time','change','percent','open','high','low','volume']
stocks = csv.DictReader(u,colnames)

for s in stocks:
    print output.substitute(s)

[ Back ]