def main(): args = get_args() pollster = Pollster() chart = pollster.charts(topic=args.topic) if args.run_all: for state in chart: polls = state.polls() for poll in polls: process_poll(poll) else: if args.state: state = get_state_from_chart(chart, args.state) else: state = chart[int(args.index)] polls = state.polls() for poll in polls: process_poll(poll)
def update_polls(): pollster = Pollster() for state in State.select().where(State.electoral_votes > 1): charts = pollster.charts(topic='2012-president', state=state.id) if charts: chart = charts[0] else: print 'NO DATA FOR %s' % state.id.upper() continue obama = 0 romney = 0 if chart.estimates: for estimate in chart.estimates: if estimate['choice'] == "Obama": obama = estimate['value'] elif estimate['choice'] == "Romney": romney = estimate['value'] else: print 'NO ESTIMATES FOR %s' % state.id.upper() continue prediction = "t" if abs(obama - romney) > 15: if obama > romney: prediction = "sd" else: prediction = "sr" elif abs(obama - romney) > 7.5: if obama > romney: prediction = "ld" else: prediction = "lr" uq = State.update(prediction=prediction).where(State.id == state) uq.execute()
from csv import DictWriter import re from pollster import Pollster pollster = Pollster() kDATE = re.compile("[0-9]*-[0-9][0-9]-[0-9][0-9]") kFIELDS = ['YEAR', 'DATE', 'TOPIC', 'NAME', 'MOE', 'SUBPOP', 'SUBPOPID', 'CHOICE', 'PARTY', 'VALUE', 'OBS', 'STATE'] if __name__ == "__main__": o = DictWriter(open("data.csv", 'w'), kFIELDS) o.writeheader() for year in [2012, 2016]: line = {} line['YEAR'] = year entry = pollster.charts(topic='%i-president' % year) for chart in entry: for poll in chart.polls(): line['DATE'] = kDATE.findall(str(poll))[-1] for question in poll.questions: line['TOPIC'] = question['topic'] line['NAME'] = question['name'] line['STATE'] = question['state'] subpop_id = 0 for subpop in question['subpopulations']: subpop_id += 1 line['SUBPOPID'] = subpop_id line['SUBPOP'] = subpop['name'] line['MOE'] = subpop['margin_of_error'] line['OBS'] = subpop['observations'] for res in subpop['responses']:
from pollster import Pollster pollster = Pollster() kDATE = re.compile("[0-9]*-[0-9][0-9]-[0-9][0-9]") kFIELDS = [ 'YEAR', 'DATE', 'TOPIC', 'NAME', 'MOE', 'SUBPOP', 'SUBPOPID', 'CHOICE', 'PARTY', 'VALUE', 'OBS', 'STATE' ] if __name__ == "__main__": o = DictWriter(open("data.csv", 'w'), kFIELDS) o.writeheader() for year in [2012, 2016]: line = {} line['YEAR'] = year entry = pollster.charts(topic='%i-president' % year) for chart in entry: for poll in chart.polls(): line['DATE'] = kDATE.findall(str(poll))[-1] for question in poll.questions: line['TOPIC'] = question['topic'] line['NAME'] = question['name'] line['STATE'] = question['state'] subpop_id = 0 for subpop in question['subpopulations']: subpop_id += 1 line['SUBPOPID'] = subpop_id line['SUBPOP'] = subpop['name'] line['MOE'] = subpop['margin_of_error'] line['OBS'] = subpop['observations'] for res in subpop['responses']:
# pip install pollster from pollster import Pollster pollster = Pollster() # current estimate of president's job approval chart = pollster.charts(topic='obama-job-approval')[20] chart.estimates example_topics = ['2012-gop-primary', '2012-senate', '2012-governor', '2012-president', '2012-house'] # list charts about Texas pollster.charts(state='TX') # calculate margin between Obama and Romney from poll poll = pollster.polls(chart='2012-general-election-romney-vs-obama')[0] question = [ x['subpopulations'][0] for x in poll.questions if x['chart'] == '2012-general-election-romney-vs-obama'][0] obama = [x for x in question['responses'] if x['choice'] == 'Obama'][0] romney = [x for x in question['responses'] if x['choice'] == 'Romney'][0] print obama['value'] - romney['value'] # check methodology in recent polls about the house chart = pollster.chart(slug='us-health-bill') print [[x.pollster, x.method] for x in chart.polls()] # TODO: compare favorability of Trump and Clinton from a recent poll # use info at http://elections.huffingtonpost.com/pollster/api/ new_poll = pollster.polls(chart = '2016-general-election-trump-vs-clinton')[0] new_question = [x['subpopulations'][0] for x in new_poll.questions if x['chart'] == '2016-general-election-trump-vs-clinton'][0] trump = [x for x in new_question['responses'] if x['choice'] == 'Donald Trump'][0] clinton = [x for x in new_question['responses'] if x['choice'] == 'Hillary Clinton'][0]
# pip install pollster from pollster import Pollster pollster = Pollster() # current estimate of president's job approval chart = pollster.charts(topic='obama-job-approval')[20] chart.estimates example_topics = [ '2012-gop-primary', '2012-senate', '2012-governor', '2012-president', '2012-house' ] # list charts about Texas pollster.charts(state='TX') # calculate margin between Obama and Romney from poll poll = pollster.polls(chart='2012-general-election-romney-vs-obama')[0] question = [ x['subpopulations'][0] for x in poll.questions if x['chart'] == '2012-general-election-romney-vs-obama' ][0] obama = [x for x in question['responses'] if x['choice'] == 'Obama'][0] romney = [x for x in question['responses'] if x['choice'] == 'Romney'][0] print obama['value'] - romney['value'] # check methodology in recent polls about the house chart = pollster.chart(slug='us-health-bill') print[[x.pollster, x.method] for x in chart.polls()]
from pollster import Pollster import sys pollsterList = [] pollster = Pollster() pollCharts = pollster.charts() #iterate through every category of polls for x in pollCharts: #iterate through all the polls in the category for y in x.polls(): if(y.pollster not in pollsterList): pollsterList.append(y.pollster) print pollsterList
from pollster import Pollster pollster = Pollster() print('\npollster.charts(): ----------------------------------------------') print(pollster.charts()) print('\npollster.charts(topics = \'obama-job-approval\'))--------------------') print(pollster.charts(topic = 'obama-job-approval')) print('\npollster.charts()[0]: -------------------------------------------') print(pollster.charts()[0]) print('\npollster.charts()[0].estimates: ---------------------------------') print(pollster.charts()[0].estimates) print('\npollster.charts()[0].estimates_by_date()[1]: --------------------') print(pollster.charts()[0].estimates_by_date()[1]) #def stateVsTopic(): # stateCharts = pollster.charts(state='CO') # presCharts = pollster.charts(topic='2016-president') # # for stateChart in stateCharts: # for presChart in presCharts: # if presChart == stateChart: # print presChart #def stateAndTopic(): # print pollster.charts(topic='2016-president', state='WI')
from pollster import Pollster pollster = Pollster() print('\npollster.charts(): ----------------------------------------------') print(pollster.charts()) print( '\npollster.charts(topics = \'obama-job-approval\'))--------------------') print(pollster.charts(topic='obama-job-approval')) print('\npollster.charts()[0]: -------------------------------------------') print(pollster.charts()[0]) print('\npollster.charts()[0].estimates: ---------------------------------') print(pollster.charts()[0].estimates) print('\npollster.charts()[0].estimates_by_date()[1]: --------------------') print(pollster.charts()[0].estimates_by_date()[1])
# pip install pollster from pollster import Pollster pollster = Pollster() # current estimate of president's job approval chart = pollster.charts(topic='obama-job-approval')[20] chart.estimates example_topics = ['2012-gop-primary', '2012-senate', '2012-governor', '2012-president', '2012-house'] # list charts about Texas pollster.charts(state='TX') # calculate margin between Obama and Romney from poll poll = pollster.polls(chart='2012-general-election-romney-vs-obama')[0] question = [ x['subpopulations'][0] for x in poll.questions if x['chart'] == '2012-general-election-romney-vs-obama'][0] obama = [x for x in question['responses'] if x['choice'] == 'Obama'][0] romney = [x for x in question['responses'] if x['choice'] == 'Romney'][0] print obama['value'] - romney['value'] # check methodology in recent polls about the house chart = pollster.chart(slug='us-health-bill') print [[x.pollster, x.method] for x in chart.polls()] # TODO: compare favorability of Trump and Clinton from a recent poll # use info at http://elections.huffingtonpost.com/pollster/api/ chart = pollster.charts(topic='favorable-ratings') print chart ### See list of favorable ratings which exist chart[] ### Select index of Clinton and Trump