votesmart / python-votesmart

python client library for interacting with Project Vote Smart API
Other
84 stars 40 forks source link

Bug fix for Elections with a single stage #3

Closed slinkp closed 15 years ago

slinkp commented 15 years ago

Apparently the votesmart service only wraps ElectionStages in a list when there's more than one. So if you try to call getElectionByYearState for a year that only has one election, you get a TypeError:

>>> votesmart.election.getElectionByYearState(2009, stateId='NY')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pw/builds/survey/builds/20090625/lib/python2.5/site-packages/votesmart.py", line 338, in getElectionByYearState
    return _result_to_obj(Election, result['elections']['election'])
  File "/home/pw/builds/survey/builds/20090625/lib/python2.5/site-packages/votesmart.py", line 184, in _result_to_obj
    return [cls(result)]
  File "/home/pw/builds/survey/builds/20090625/lib/python2.5/site-packages/votesmart.py", line 72, in __init__
   stages = [ElectionStage(s) for s in d.pop('stage')]
  File "/home/pw/builds/survey/builds/20090625/lib/python2.5/site-packages/votesmart.py", line 24, in __init__
    self.__dict__ = d
TypeError: __dict__ must be set to a dictionary, not a 'str'

To fix this, Election.init can be rewritten like so:

class Election(VotesmartApiObject): def init(self, d): stages = d.pop('stage') self.dict = d if isinstance(stages, dict): stages = [ElectionStage(stages)] else: stages = [ElectionStage(s) for s in stages] self.stages = stages

jamesturk commented 15 years ago

fix elections with a single stage, closed by 299a55fe4c4a6c019fe1b29c55cdafcdf06727de