Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Kirkman opened this issue Aug 5, 2020 · 4 comments
Open

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

Kirkman opened this issue Aug 5, 2020 · 4 comments

Comments

@Kirkman
Copy link

Kirkman commented Aug 5, 2020

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
Copy link
Author

Kirkman commented Aug 7, 2020

I was using clarify 0.4.0 installed via pip.

It looks like the most recent commit (f9bfb09) 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
Copy link
Contributor

dwillis commented Aug 8, 2020

Oh, interesting, thanks!

@Kirkman
Copy link
Author

Kirkman commented Sep 12, 2020

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

@jlkravitz
Copy link

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants