3 Show web page of polling data from one state
4 Author: Bryan Steinbach
16 conn
= sqlite3
.connect(tablefn
)
20 #c.execute('select * from polls where state = "CA"')
23 ''' Fetch image URL from candidate's name '''
24 if name
== '': return ''
26 c
.execute(''' select url from pics where name = '%s' '''%name
)
32 def retrieve_state(state
):
33 ''' Fetch information about state and render to web page '''
35 c
.execute('''select polls.state,polls.gop,polls.dem,polls.ind,polls.date1,polls.pollster,names.gop,names.dem,names.ind,names.incpar
36 from polls,names,pics where polls.state = '%s' and names.state = '%s' order by polls.date1 desc limit 1'''%(state
,state
))
38 state
,gopfrac
,demfrac
,indfrac
,date
,pollster
,gopname
,demname
,indname
,incparname
= c
.next()
42 goppic
= getpic(gopname
)
43 dempic
= getpic(demname
)
44 indpic
= getpic(indname
)
46 fracs
= np
.array([gopfrac
,demfrac
,indfrac
])
47 partys
= ['Republican','Democrat','Independent']
49 favi
= np
.argmax(fracs
)
53 with
open(fn
,'w') as f
:
56 print>>f
,"<h1>State of "+state
+" polling</h1>"
59 print>>f
,"<h2>Republican candidate: ",gopname
,"</h2>"
60 print>>f
,"<img src='"+goppic
+"'></img>"
62 print>>f
,"Polling: ",gopfrac
67 print>>f
,"<h2>Democratic candidate: ",demname
,"</h2>"
68 print>>f
,"<img src='"+dempic
+"'></img>"
70 print>>f
,"Polling: ",demfrac
75 print>>f
,"<h2>Independent candidate: ",indname
,"</h2>"
76 print>>f
,"<img src='"+indpic
+"'></img>"
78 print>>f
,"Polling: ",indfrac
82 print>>f
,"The incumbent party is "+incparname
+"."
83 if favp
!= incparname
:
84 print>>f
,"We expect that to change to %s this election."%favp
86 print>>f
,"We don't expect that to change this election."
97 print "usage: python d.py <2 char state abbreviation>"