Ejemplo n.º 1
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()]

# 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]
states = {state.abbr: state.name for state in us.STATES}

# Read electoral college votes from csv file
# https://raw.githubusercontent.com/chris-taylor/USElection/master/data/electoral-college-votes.csv
with open('electoral-college-votes.csv') as csvfile:
    reader = csv.reader(csvfile)
    college = {row[0]: int(row[1]) for row in reader}

# Initialize Pollster API
pollster = Pollster()

date = '2016-11-01'  # 1 weeks of polls between 2016-11-01 and 2016-11-07
polls = []
page = 1

query = pollster.polls(topic='2016-president', after=date, page=page)
while query:
    polls.extend(query)
    query = pollster.polls(topic='2016-president', after=date, page=page)
    page += 1

# Save polls by pickle
pickle.dump(polls, open('polls.p', 'wb'))

# Keep only state polls, igore national polls
state_questions = [
    question for poll in polls for question in poll.questions
    if question['topic'] == '2016-president'
    and question['code'].split('-')[1] in states
]
Ejemplo n.º 3
0
# pip install pollster
from pollster import Pollster 

pollster = Pollster()

# current estimate of president's job approval
chart = pollster.charts(topic='obama-job-approval')[0]
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 Democratic and Republican parties from a recent poll
# # use info at http://elections.huffingtonpost.com/pollster/api/charts
Ejemplo n.º 4
0
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/
def fetchPolls():
    #This function will fetch all new polls from Pollster and injects them into the SQLite database
    #First, we call updateTableStructure() to make sure that the SQLite table has the right structure and so we can get the list of candidates that we are concerned with
    dems, repubs = updateTableStructure()
    demString = str(dems).replace('[', '').replace(']', '').replace('\'', '`').replace('"', '`').replace('O`M', 'O\'M')
    repubString = str(repubs).replace('[', '').replace(']', '').replace('\'', '`').replace('"', '`')
    #Now we initialize everything for the SQLite database
    cursor = conn.cursor()
    #Now we initialize the pollster object
    pollster = Pollster()
    #Now we query the DemPrimary table to find the most recent poll that was inserted
    cursor.execute("SELECT EndDate FROM DemPrimary ORDER BY EndDate DESC;")
    testDemDates = cursor.fetchall()
    if testDemDates:
        #We need to make sure that there testDemDates has some entry, otherwise that means absolutely no polls have been injected, so we set the date to 2012-11-06, the day of the 2012 presidential election
        latestDemDate = testDemDates[0][0] + datetime.timedelta(1)
    else:
        latestDemDate = datetime.date(2012, 11, 06)
    #The next step is to get all the new polls. In order to do this, we need to first get the first page of polls and then every subsequent page of polls after.
    demPolls = []
    newDemPolls = pollster.polls(chart = '2016-national-democratic-primary', page=1, after=str(latestDemDate))
    i = 2
    while newDemPolls:
        demPolls.extend(newDemPolls)
        newDemPolls = pollster.polls(chart = '2016-national-democratic-primary', page=i, after=str(latestDemDate))
        i += 1
    #Now we loop through every poll and inject it into the database.
    for p in demPolls:
        #pollProps are true of all subpopulations
        pollProps = [str(p.pollster), str(p.start_date), str(p.end_date), str(p.partisan), str(p.affiliation), str(p.method)]
        relevantQuestion = [x for x in p.questions if x['topic'] == '2016-president-dem-primary'][0]
        #Loop through subpopulations and inject each separately (treat each as its own poll)
        for s in relevantQuestion['subpopulations']:
            values = pollProps + [s['observations'] if s['observations'] else 0, str(s['name'])]
            resp = s['responses']
            for c in dems:
                relevantResp = [x for x in resp if x['last_name']==c]
                val = relevantResp[0]['value'] if relevantResp else 0
                values.append(val)
            cursor.execute("INSERT INTO DemPrimary (Pollster, StartDate, EndDate, Partisan, Affiliation, Method, Sample, SampleType, %(cands)s) VALUES (%(val)s);" %{"val": str(values).replace('[', '').replace(']', ''), "cands":demString})
    #We now follow the same process for the Republicans
    cursor.execute("SELECT EndDate FROM RepubPrimary ORDER BY EndDate DESC;")
    testRepubDates = cursor.fetchall()
    if testRepubDates:
        latestRepubDate = testRepubDates[0][0] + datetime.timedelta(1)
    else:
        latestRepubDate = datetime.date(2012, 11, 06)
    repubPolls = []
    newRepubPolls = pollster.polls(chart = '2016-national-gop-primary', page=1, after=str(latestRepubDate))
    i = 2
    while newRepubPolls:
        repubPolls.extend(newRepubPolls)
        newRepubPolls = pollster.polls(chart = '2016-national-gop-primary', page=i, after=str(latestRepubDate))
        i += 1
    for p in repubPolls:
        pollProps = [str(p.pollster), str(p.start_date), str(p.end_date), str(p.partisan), str(p.affiliation), str(p.method)]
        relevantQuestion = [x for x in p.questions if x['topic'] == '2016-president-gop-primary'][0]
        for s in relevantQuestion['subpopulations']:
            values = pollProps + [s['observations'] if s['observations'] else 0, str(s['name'])]
            resp = s['responses']
            for c in repubs:
                relevantResp = [x for x in resp if x['last_name']==c]
                val = relevantResp[0]['value'] if relevantResp else 0
                values.append(val)
            cursor.execute("INSERT INTO RepubPrimary (Pollster, StartDate, EndDate, Partisan, Affiliation, Method, Sample, SampleType, %(cands)s) VALUES (%(val)s);" %{"val": str(values).replace('[', '').replace(']', ''), "cands":repubString})
    conn.commit()
    return dems, repubs