From c570cf25efaddb76e177f028580e5f4cee9a1a41 Mon Sep 17 00:00:00 2001 From: Bryan Steinbach Date: Sun, 10 Oct 2010 14:24:01 -0700 Subject: [PATCH] D complete --- week5/d.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 5 deletions(-) diff --git a/week5/d.py b/week5/d.py index 332a0e2..d322cef 100644 --- a/week5/d.py +++ b/week5/d.py @@ -6,18 +6,88 @@ Author: Bryan Steinbach import os import sqlite3 +import webbrowser + +import numpy as np tablefn = 'poll' conn = sqlite3.connect(tablefn) -c = conn.cursor() # sanity check #c.execute('select * from polls where state = "CA"') -c.execute('select * from names where state = "CA"') -for r in c: - print r + +def getpic(name): + ''' Fetch image URL from candidate's name ''' + if name == '': return '' + c = conn.cursor() + c.execute(''' select url from pics where name = '%s' '''%name) + (url,) = c.next() + c.close() + + return url + +def retrieve_state(state): + ''' Fetch information about state and render to web page ''' + c = conn.cursor() + c.execute('''select polls.state,polls.gop,polls.dem,polls.ind,polls.date1,polls.pollster,names.gop,names.dem,names.ind,names.incpar + from polls,names,pics where polls.state = '%s' and names.state = '%s' order by polls.date1 desc limit 1'''%(state,state)) + + state,gopfrac,demfrac,indfrac,date,pollster,gopname,demname,indname,incparname = c.next() + + c.close() + + goppic = getpic(gopname) + dempic = getpic(demname) + indpic = getpic(indname) + + fracs = np.array([gopfrac,demfrac,indfrac]) + partys = ['Republican','Democratic','Independent'] + + favi = np.argmax(fracs) + favp = partys[favi] + + fn = state+'.html' + with open(fn,'w') as f: + print>>f,"" + print>>f,"" + print>>f,"

State of "+state+" polling

" + + if gopname: + print>>f,"

Republican candidate: ",gopname,"

" + print>>f,"" + print>>f,"

" + print>>f,"Polling: ",gopfrac + + print>>f,"

" + + if demname: + print>>f,"

Democratic candidate: ",demname,"

" + print>>f,"" + print>>f,"

" + print>>f,"Polling: ",demfrac + + print>>f,"

" + + if indname: + print>>f,"

Independent candidate: ",indname,"

" + print>>f,"" + print>>f,"

" + print>>f,"Polling: ",indfrac + + print>>f,"

" + + print>>f,"The incumbent party is "+incparname+"." + if favp != incparname: + print>>f,"We expect that to change to %s this election."%favp + else: + print>>f,"We don't expect that to change this election." + + print>>f,"" + print>>f,"" + + webbrowser.open(fn) -c.close() +retrieve_state('CA') -- 2.11.4.GIT