openelections / clarify

Discover and parse results for jurisdictions that use Clarity-based election systems.
MIT License
38 stars 22 forks source link

KeyError when precinct isn't listed under <precincts> #37

Open Kirkman opened 4 years ago

Kirkman commented 4 years ago

Last night ran into an issue with a LiveVoterTurnout XML file that Clarify wouldn't parse.

Basically, each Contest had a Intrastate New Resident precinct listed. But the master Precincts list didn't include it.

This led to a KeyError:

Traceback (most recent call last):
  File "parserClarity.py", line 294, in <module>
    p.parse( results_file )
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 51, in parse
    self._contests = self._parse_contests(tree)
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 284, in _parse_contests
    return [self._parse_contest(el) for el in contest_els]
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 284, in <listcomp>
    return [self._parse_contest(el) for el in contest_els]
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 312, in _parse_contest
    for c in self._parse_choices(contest_el, contest):
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 370, in _parse_choices
    for c_el in contest_el.xpath('Choice')]
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 370, in <listcomp>
    for c_el in contest_el.xpath('Choice')]
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 409, in _parse_choice
    subjurisdiction = self.get_result_jurisdiction(subjurisdiction_el.attrib['name'])
  File "/Users/kirkman/.virtualenvs/elections3/lib/python3.7/site-packages/clarify/parser.py", line 219, in get_result_jurisdiction
    return self._result_jurisdiction_lookup[name]
KeyError: 'Intrastate New Resident'

I'm attaching the ZIP file for reference. If I get some time next week I may try to see if I can find a solution and submit a pull request, but wanted to let you know about it.

lincoln-county.zip

Kirkman commented 4 years ago

I was using clarify 0.4.0 installed via pip.

It looks like the most recent commit (f9bfb09876a1c2f0b68034aa0494a31aaf009250) in this repo solves my issue.

However, I also found that, in 0.4.0, simply deleting the second line here ...

            for subjurisdiction_el in vt_el.xpath('./Precinct') + vt_el.xpath('./County'):
                subjurisdiction = self.get_result_jurisdiction(subjurisdiction_el.attrib['name'])
                subjurisdiction = self._get_or_create_result_jurisdiction(subjurisdiction_el)

...resolved the issue as well.

dwillis commented 4 years ago

Oh, interesting, thanks!

Kirkman commented 4 years ago

Do you anticipate a version bump for Clarify soon on PyPi incorporating that change?

jlkravitz commented 3 years ago

I seem to be having a similar issue. I'm trying to parse Fort Bend County, TX results , and there are a few odd precinct names. Here is the output I get from the parser:

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 51, in parse self._contests = self._parse_contests(tree) File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 284, in _parse_contests return [self._parse_contest(el) for el in contest_els] File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 284, in return [self._parse_contest(el) for el in contest_els] File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 312, in _parse_contest for c in self._parse_choices(contest_el, contest): File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 369, in _parse_choices return [self._parse_choice(c_el, contest) File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 369, in return [self._parse_choice(c_el, contest) File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 409, in _parse_choice subjurisdiction = self.get_result_jurisdiction(subjurisdiction_el.attrib['name']) File "/usr/local/lib/python3.9/site-packages/clarify/parser.py", line 219, in get_result_jurisdiction return self._result_jurisdiction_lookup[name] KeyError: '1134 M68'