3 Build sqlite3 table of names and pictures
4 Author: Bryan Steinbach
15 conn
= sqlite3
.connect(tablefn
)
19 #c.execute('select * from polls where state = "CA"')
22 ''' Fetch image URL from candidate's name '''
23 if name
== '': return ''
25 c
.execute(''' select url from pics where name = '%s' '''%name
)
31 def retrieve_state(state
):
32 ''' Fetch information about state and render to web page '''
34 c
.execute('''select polls.state,polls.gop,polls.dem,polls.ind,polls.date1,polls.pollster,names.gop,names.dem,names.ind,names.incpar
35 from polls,names,pics where polls.state = '%s' and names.state = '%s' order by polls.date1 desc limit 1'''%(state
,state
))
37 state
,gopfrac
,demfrac
,indfrac
,date
,pollster
,gopname
,demname
,indname
,incparname
= c
.next()
41 goppic
= getpic(gopname
)
42 dempic
= getpic(demname
)
43 indpic
= getpic(indname
)
45 fracs
= np
.array([gopfrac
,demfrac
,indfrac
])
46 partys
= ['Republican','Democratic','Independent']
48 favi
= np
.argmax(fracs
)
52 with
open(fn
,'w') as f
:
55 print>>f
,"<h1>State of "+state
+" polling</h1>"
58 print>>f
,"<h2>Republican candidate: ",gopname
,"</h2>"
59 print>>f
,"<img src='"+goppic
+"'></img>"
61 print>>f
,"Polling: ",gopfrac
66 print>>f
,"<h2>Democratic candidate: ",demname
,"</h2>"
67 print>>f
,"<img src='"+dempic
+"'></img>"
69 print>>f
,"Polling: ",demfrac
74 print>>f
,"<h2>Independent candidate: ",indname
,"</h2>"
75 print>>f
,"<img src='"+indpic
+"'></img>"
77 print>>f
,"Polling: ",indfrac
81 print>>f
,"The incumbent party is "+incparname
+"."
82 if favp
!= incparname
:
83 print>>f
,"We expect that to change to %s this election."%favp
85 print>>f
,"We don't expect that to change this election."