datadesk / python-elections

A Python wrapper for the Associated Press' U.S. election data service.
python-elections.rtfd.org
176 stars 49 forks source link

get_topofticket() bailing on vote_total calculation #71

Closed eads closed 11 years ago

eads commented 11 years ago

Here's the trace:

In [26]: client.get_topofticket()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-d18d0a277baf> in <module>()
----> 1 client.get_topofticket()

/Users/deads/.virtualenvs/electioncenter/src/python-elections/elections/ap.pyc in get_topofticket(self, election_date, **kwargs)
     98             result = TopOfTicket(self, dt.strftime("%Y%m%d"), **kwargs)
     99         else:
--> 100             result = TopOfTicket(self, **kwargs)
    101         self.ftp.quit()
    102         return result

/Users/deads/.virtualenvs/electioncenter/src/python-elections/elections/ap.pyc in __init__(self, client, name, results, delegates)
    742             self.reporting_unit_file_path = "/inits/US/US_%(name)s_ru.txt" % {'name': name}
    743             self.candidate_file_path = "/inits/US/US_%(name)s_pol.txt" % {'name': name}
--> 744         super(TopOfTicket, self).__init__(client, name, results, delegates)
    745 
    746     @property

/Users/deads/.virtualenvs/electioncenter/src/python-elections/elections/ap.pyc in __init__(self, client, name, results, delegates)
    297         self._init_candidates()
    298         if results:
--> 299             self.fetch_results()
    300         # Fetches delegates for any Primary or Caucus races
    301         if delegates and self.filter_races(is_general=False):

/Users/deads/.virtualenvs/electioncenter/src/python-elections/elections/ap.pyc in fetch_results(self)
    402         the most fresh data from the AP.
    403         """
--> 404         self._get_flat_results()
    405 
    406     def fetch_delegates(self):

/Users/deads/.virtualenvs/electioncenter/src/python-elections/elections/ap.pyc in _get_flat_results(self, ftp)
    657                 # Update the candidate's global vote total if data are statewide
    658                 if is_state:
--> 659                     candidate.vote_total = vote_count
    660 
    661                 # Set is_winner and is_runoff

AttributeError: 'NoneType' object has no attribute 'vote_total'
palewire commented 11 years ago

Yes. I hit this error today too. There are IDs in the AP results file that do not appear in the "init" files with all the metadata about candidates and races. I've contacted AP but yet to hear back.

In the mean time, I pushed a hack that will "continue" and skip results rows that are not in the inits file for now. Once we hear back from AP on the source of the issue, we'll make a better fix.

palewire commented 11 years ago

And, FTR, this error didn't occur until they updated the inits files today, so I suspect something is off there. Though it's possible we're reaching in and grabbing the wrong inits.

palewire commented 11 years ago

here's that patch, which is now in the master branch https://github.com/datadesk/python-elections/commit/6a91788a9dc7c2cc1966c608ddeb6ab0a12cd15a

eads commented 11 years ago

Thanks, Ben. we've been building with mocked data so I could work around the designer's schedule, so today is the first I've been working in earnest with the elections lib. Apparently I picked a good day (: On Oct 9, 2012 4:38 PM, "Ben Welsh" notifications@github.com wrote:

here's that patch, which is now in the master branch datadesk/python-elections@6a91788https://github.com/datadesk/python-elections/commit/6a91788a9dc7c2cc1966c608ddeb6ab0a12cd15a

— Reply to this email directly or view it on GitHubhttps://github.com/datadesk/python-elections/issues/71#issuecomment-9280979.

palewire commented 11 years ago

Yeah, it's all coming together on the runway here, as you can tell. Also, don't miss the new undocumented get_presidential_summary method if you're looking to do a 50 state map.

eads commented 11 years ago

Yup, already found it. Looks incredibly handy.

palewire commented 11 years ago

AP says

That race, 6433 in AR, was updated today, to candidates For and Against, and will be reflected in the US.txt and in the US pol file in tomorrow’s test. It will be candidates 12040 and 12041. t;2012-11-06;AR;1;0;Arkansas;6433;I;G;1;Issue;5 - Medical Marijuana;;Ballot Issue;Allow Medical Marijuana;1;0;0;2393;12040;1;;;;For;;0;0;0;;2;12041;2;;;;Against;;0;0;0;;3; pol init: | 12040|||For |For | | | 6433| 2| 1| 8190|AR | | 12041|||Against |Against | | | 6433| 3| 2| 8191|AR