From b914deade3ee8179efb0743713d9fb75d917079a Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Tue, 3 May 2022 17:40:41 -0400 Subject: [PATCH] Fix most flake8 errors --- elex/api/delegates.py | 11 ++++------- elex/api/models.py | 9 +++------ elex/api/trends.py | 5 +---- elex/api/utils.py | 2 +- elex/cli/app.py | 5 +---- elex/cli/decorators.py | 2 +- elex/cli/hooks.py | 3 ++- setup.cfg | 2 +- tests/test_ap_network.py | 1 - tests/test_candidate_reporting_unit.py | 5 +---- tests/test_new_england_reporting_units.py | 24 +++++++++++------------ tests/test_results.py | 5 +---- 12 files changed, 27 insertions(+), 47 deletions(-) diff --git a/elex/api/delegates.py b/elex/api/delegates.py index a59ba3d..bdd5617 100644 --- a/elex/api/delegates.py +++ b/elex/api/delegates.py @@ -203,13 +203,10 @@ def get_report_id(self, reports, key): """ for report in reports: - if ( - key == 'delSum' and - report.get('title') == 'Delegates / delsum' - ) or ( - key == 'delSuper' and - report.get('title') == 'Delegates / delsuper' - ): + delsum = key == 'delSum' and report.get('title') == 'Delegates / delsum' + delsuper = key == 'delSuper' and report.get('title') == 'Delegates / delsuper' + + if delsum or delsuper: id = report.get('id').rsplit('/', 1)[-1] return id diff --git a/elex/api/models.py b/elex/api/models.py index 706b039..bdaca81 100644 --- a/elex/api/models.py +++ b/elex/api/models.py @@ -725,10 +725,7 @@ def set_new_england_counties(self): d['precinctsreporting'] += cru.precinctsreporting try: - d['precinctsreportingpct'] = ( - float(d['precinctsreporting']) / - float(d['precinctstotal']) - ) + d['precinctsreportingpct'] = (float(d['precinctsreporting']) / float(d['precinctstotal'])) except ZeroDivisionError: d['precinctsreportingpct'] = 0.0 @@ -894,7 +891,7 @@ def set_id_field(self): self.id = self.electiondate def get(self, path, **params): - """ + r""" Farms out request to api_request. Could possibly handle choosing which parser backend to use -- API-only right now. @@ -947,7 +944,7 @@ def get_uniques(self, candidate_reporting_units): return candidates, ballot_measures def get_raw_races(self, **params): - """ + r""" Convenience method for fetching races by election date. Accepts an AP formatting date string, e.g., YYYY-MM-DD. Accepts any number of URL params as kwargs. diff --git a/elex/api/trends.py b/elex/api/trends.py index e63a14d..ab42807 100644 --- a/elex/api/trends.py +++ b/elex/api/trends.py @@ -105,10 +105,7 @@ def get_report_id(self, reports, key): organization-specific report ID. """ for report in reports: - if ( - key == self.office_code and - report.get('title') in [self.api_report_id, self.api_test_report_id] - ): + if (key == self.office_code and report.get('title') in [self.api_report_id, self.api_test_report_id]): id = report.get('id').rsplit('/', 1)[-1] return id return None diff --git a/elex/api/utils.py b/elex/api/utils.py index 275bc32..34bf5c0 100644 --- a/elex/api/utils.py +++ b/elex/api/utils.py @@ -69,7 +69,7 @@ def write_recording(payload): def api_request(path, **params): - """ + r""" Function wrapping Python-requests for making a request to the AP's elections API. diff --git a/elex/cli/app.py b/elex/cli/app.py index 404fa94..ff2c18a 100644 --- a/elex/cli/app.py +++ b/elex/cli/app.py @@ -360,10 +360,7 @@ def delegates(self): """ self.app.log.info('Getting delegate reports') - if ( - self.app.pargs.delegate_super_file and - self.app.pargs.delegate_sum_file - ): + if (self.app.pargs.delegate_super_file and self.app.pargs.delegate_sum_file): report = DelegateReport( delsuper_datafile=self.app.pargs.delegate_super_file, delsum_datafile=self.app.pargs.delegate_sum_file diff --git a/elex/cli/decorators.py b/elex/cli/decorators.py index d9ed2e9..657edf0 100644 --- a/elex/cli/decorators.py +++ b/elex/cli/decorators.py @@ -57,7 +57,7 @@ def decorated(self): self.app.log.error('HTTP Error {0} - {1}'.format(e.response.status_code, message)) self.app.log.debug('HTTP Error {0} ({1})'.format(e.response.status_code, e.response.url)) self.app.close(1) - except APAPIKeyException as e: + except APAPIKeyException: text = 'APAPIKeyError: AP_API_KEY environment variable is not set.' self.app.log.error(text) self.app.close(1) diff --git a/elex/cli/hooks.py b/elex/cli/hooks.py index ffc85a5..cbdd89a 100644 --- a/elex/cli/hooks.py +++ b/elex/cli/hooks.py @@ -38,7 +38,8 @@ def add_election_hook(app): app.close(1) else: app.election.officeids = app.pargs.officeids - # kept as a comma-delimited string so officeID as a param always appears once in request url (e.g. officeID=P%2CH%2CG) + # kept as a comma-delimited string so officeID as a param always appears once in request url + # (e.g. officeID=P%2CH%2CG) def cachecontrol_logging_hook(app): diff --git a/setup.cfg b/setup.cfg index 4a09d59..13d24a0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,7 +2,7 @@ universal = 1 [flake8] -max-line-length = 119 +max-line-length = 135 ignore = E731 [metadata] diff --git a/tests/test_ap_network.py b/tests/test_ap_network.py index aa8f63d..cac2f31 100644 --- a/tests/test_ap_network.py +++ b/tests/test_ap_network.py @@ -1,6 +1,5 @@ import os import unittest -import tests from elex.cli.app import ElexApp from requests.exceptions import HTTPError diff --git a/tests/test_candidate_reporting_unit.py b/tests/test_candidate_reporting_unit.py index 67e9063..64d5640 100644 --- a/tests/test_candidate_reporting_unit.py +++ b/tests/test_candidate_reporting_unit.py @@ -135,10 +135,7 @@ def test_candidate_reporting_unit_sums_reportingunit_pct(self): reporting_unit = self.reporting_units[0] candidate_reporting_units = self.candidate_reporting_units[0:2] self.assertEqual( - ( - candidate_reporting_units[0].votecount / - float(reporting_unit.votecount) - ), + (candidate_reporting_units[0].votecount / float(reporting_unit.votecount)), 0.694266390666554 ) diff --git a/tests/test_new_england_reporting_units.py b/tests/test_new_england_reporting_units.py index e217ffa..19a22b5 100644 --- a/tests/test_new_england_reporting_units.py +++ b/tests/test_new_england_reporting_units.py @@ -6,6 +6,8 @@ from elex.api import maps import tests +is_ct_county = lambda r: r.statepostal == "CT" and r.level == "county" + class TestConnecticutRollups(tests.ElectionResultsTestCase): """ @@ -20,9 +22,7 @@ def test_ct_has_candidates(self): def test_ct_has_all_counties(self): ct_counties = set() raw_counties = [ - r for r in self.reporting_units if - r.statepostal == "CT" and - r.level == "county" + r for r in self.reporting_units if is_ct_county(r) ] for c in raw_counties: ct_counties.add(c.reportingunitname) @@ -33,25 +33,23 @@ def test_ct_has_all_counties(self): def test_ct_counties_match_townships(self): ct_counties = [ - r for r in self.reporting_units if - r.statepostal == "CT" and - r.level == "county" + r for r in self.reporting_units if is_ct_county(r) ] for county in ct_counties: races = set([ r.raceid for r in self.reporting_units if - r.statepostal == "CT" and - r.level == "township" and - r.fipscode == county.fipscode + r.statepostal == "CT" and # noqa: W504 + r.level == "township" and # noqa: W504 + r.fipscode == county.fipscode # noqa: W504 ]) for race in races: townships = [ r.precinctstotal for r in self.reporting_units if - r.statepostal == "CT" and - r.level == "township" and - r.fipscode == county.fipscode and - r.raceid == race + r.statepostal == "CT" and # noqa: W504 + r.level == "township" and # noqa: W504 + r.fipscode == county.fipscode and # noqa: W504 + r.raceid == race # noqa: W504 ] self.assertEqual(county.precinctstotal, sum(townships)) diff --git a/tests/test_results.py b/tests/test_results.py index e0c06d7..baa377f 100644 --- a/tests/test_results.py +++ b/tests/test_results.py @@ -29,10 +29,7 @@ def test_number_of_counties(self): According to bug #236, we should be 1 county short. """ mass_results = [ - r for r in self.results if - r.raceid == '24547' and - r.level == 'county' and - r.last == 'Trump' + r for r in self.results if r.raceid == '24547' and r.level == 'county' and r.last == 'Trump' ] self.assertEqual(len(mass_results), len(maps.FIPS_TO_STATE['MA']))