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
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:
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