Skip to content

Commit

Permalink
Add basic linting with flake8, fix lint
Browse files Browse the repository at this point in the history
Linting can be run with 'python setup.py flake8'
The worst offender was line length, so rather than fix it, I opted to
ignore that lint.
  • Loading branch information
maurizi committed Aug 3, 2018
1 parent 997c8b2 commit 21476db
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 35 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python:
- '3.6'

script:
- python setup.py flake8
- python setup.py test

deploy:
Expand Down
2 changes: 1 addition & 1 deletion omgeo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .geocoder import Geocoder
from .geocoder import Geocoder # NOQA
11 changes: 4 additions & 7 deletions omgeo/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
from omgeo.places import PlaceQuery
from omgeo.postprocessors import DupePicker, SnapPoints
import time

logger = logging.getLogger(__name__)
stats_logger = logging.getLogger('omgeo.stats')
Expand All @@ -13,9 +12,10 @@ class Geocoder():
Class for building a custom geocoder using external APIs.
"""

DEFAULT_SOURCES = [['omgeo.services.EsriWGS', {}],
['omgeo.services.Nominatim', {}]
]
DEFAULT_SOURCES = [
['omgeo.services.EsriWGS', {}],
['omgeo.services.Nominatim', {}]
]
DEFAULT_PREPROCESSORS = []
DEFAULT_POSTPROCESSORS = [
SnapPoints(),
Expand Down Expand Up @@ -96,16 +96,13 @@ def geocode(self, pq, waterfall=None, force_stats_logging=False):
* upstream_response_info - list of UpstreamResponseInfo objects
"""

start_time = time.time()
waterfall = self.waterfall if waterfall is None else waterfall
if type(pq) in (str, str):
pq = PlaceQuery(pq)
processed_pq = copy.copy(pq)

for p in self._preprocessors: # apply universal address preprocessing
processed_pq = p.process(processed_pq)
if not processed_pq:
return get_result() # universal preprocessor rejects PlaceQuery

upstream_response_info_list = []
processed_candidates = []
Expand Down
14 changes: 7 additions & 7 deletions omgeo/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ def lbl(str_, align='L'):
padding = num_spaces * ' '
return '%s%s' % (padding, str_)

return ' %s\n'\
' ------------\n'\
' | |\n'\
'%s| |%s\n'\
' | |\n'\
' ------------\n'\
' %s' % (lbl(top, 'C'), lbl(left, 'R'), lbl(right, 'L'), lbl(bottom, 'C'))
return ' %s\n'\
' ------------\n'\
' | |\n'\
'%s| |%s\n'\
' | |\n'\
' ------------\n'\
' %s' % (lbl(top, 'C'), lbl(left, 'R'), lbl(right, 'L'), lbl(bottom, 'C'))


class PlaceQuery():
Expand Down
11 changes: 5 additions & 6 deletions omgeo/postprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ def cleanup(str_):
attr_match = self.attr_dupes
attr_match_test_val = cleanup(getattr(hsc, attr_match))
# make a list of candidates that have essentially the same value for attr_match (like 123 Main & 123 MAIN)
#import IPython; IPython.embed()
matching_candidates = [mc for mc in candidates if cleanup(getattr(mc, attr_match)) == attr_match_test_val]
# sort them in the desired order so the first one has the best attribute value
matching_candidates = AttrSorter(self.ordered_list, self.attr_sort).process(matching_candidates)
Expand Down Expand Up @@ -522,11 +521,11 @@ def _get_distance(self, pnt1, pnt2):
lat1, lon1 = pnt1
lat2, lon2 = pnt2
radius = 6356752 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
dlat = math.radians(lat2 - lat1)
dlon = math.radians(lon2 - lon1)
a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon / 2) * math.sin(dlon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = radius * c
return d

Expand Down
2 changes: 1 addition & 1 deletion omgeo/preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __init__(self, acceptable_countries=None, country_map=None):
For example, suppose that the geocoding service recognizes
'GB', but not 'UK' -- and 'US', but not 'USA'::
country_map = {'UK':'GB', 'USA':'US'}
country_map = {'UK':'GB', 'USA':'US'}
"""
self.acceptable_countries = acceptable_countries if acceptable_countries is not None else []
Expand Down
1 change: 1 addition & 0 deletions omgeo/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa: F401
from .bing import Bing
from .esri import EsriWGS
from .us_census import USCensus
Expand Down
8 changes: 1 addition & 7 deletions omgeo/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

import requests

try:
# python 3
from urllib.parse import urlencode
except ImportError:
# python 2
from urllib import urlencode

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -226,7 +220,7 @@ def geocode(self, pq):
end = datetime.now()
response_time_sec = (end - start).total_seconds()
upstream_response_info.set_response_time(1000 * response_time_sec)
except:
except Exception:
upstream_response_info.set_success(False)
upstream_response_info.errors.append(format_exc())
return [], upstream_response_info
Expand Down
2 changes: 1 addition & 1 deletion omgeo/services/nominatim.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Nominatim(GeocodeService):
'leisure.sports_centre', 'lesiure.stadium', 'leisure.track',
'lesiure.water_park', 'man_made.lighthouse', 'man_made.works',
'military.barracks', 'military.bunker', 'office.', 'place.house',
'amenity.', 'power.generator', 'railway.station',
'amenity.', 'power.generator', 'railway.station',
'shop.', 'tourism.']

DEFAULT_REJECTED_ENTITIES = ['amenity.drinking_water',
Expand Down
13 changes: 8 additions & 5 deletions omgeo/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ def test_geocode_karori(self):
"""
candidates = self.g_bing.get_candidates(self.pq['karori'])
self.assertEqual(len(candidates) > 0, True, 'No candidates returned.')
self.assertEqual(any([('102' in c.match_addr and '6012' in c.match_addr) for c in candidates]),
self.assertEqual(
any([('102' in c.match_addr and '6012' in c.match_addr) for c in candidates]),
True, 'Could not find bldg. no. "102" and postcode "6012" in any address.')

def _test_address_components(self, candidate):
Expand Down Expand Up @@ -314,10 +315,12 @@ def _test_geocode_results_all_(self, verbosity=0, geocoder=Geocoder(),
else:
queries_with_results += 1
logger.info('Input: %s' % self.pq[place].query)
logger.info(['Output: %r (%s %s)\n' %
(c.match_addr,
c.geoservice,
[c.locator, c.score, c.confidence, c.entity]) for c in candidates])
logger.info([
'Output: %r (%s %s)\n' % (
c.match_addr,
c.geoservice,
[c.locator, c.score, c.confidence, c.entity])
for c in candidates])
self.assertEqual(expected_results, queries_with_results,
'Got results for %d of %d queries.' % (queries_with_results, len(self.pq)))

Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
ignore = E501
exclude = sphinx,build
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ def read(fname):
install_requires=[
'requests >= 2.18',
],
setup_requires=['flake8'],
test_suite='omgeo.tests.tests',
)

0 comments on commit 21476db

Please sign in to comment.