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()
# Will download data from Huffington Post and save it as data.csv # Run once before trying predict.py, but you shouldn't need to run it frequently. 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']:
def mock(): producer = ProducerThread(producers) producer.start() plugins = registry.setup_check_plugin() pollster = Pollster(plugins, 15) pollster.run()