Skip to content

Commit

Permalink
Merge pull request #136 from sr-murthy/flake8-linting
Browse files Browse the repository at this point in the history
Flake8 linting of core modules (`rtree/index.py` and `rtree/core.py`, ignoring E501) + drop Python 2 support
  • Loading branch information
sr-murthy authored Dec 21, 2019
2 parents 8b83e7f + a8ebb65 commit d202812
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ addons:
- libspatialindex-c3

install:
- pip install flake8
- pip install -e .

script:
- flake8 --ignore=E501 --exclude=rtree/__init__.py rtree/
- python -m pytest --doctest-modules rtree tests/test_*
5 changes: 3 additions & 2 deletions ci/azp/conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
- bash: |
source activate rtree
pip install pytest numpy
pip install flake8 pytest numpy
flake8 --ignore=E501 --exclude=rtree/__init__.py rtree/
python -m pytest --doctest-modules rtree tests/test_*
displayName: pytest
displayName: Lint with Flake8 and run unit tests
5 changes: 3 additions & 2 deletions ci/azp/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
sudo update-locale LANG=en_US.UTF-8
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
pip install pytest numpy
pip install flake8 pytest numpy
flake8 --ignore=E501 --exclude=rtree/__init__.py rtree/
python -m pytest --doctest-modules rtree tests/test_*
displayName: 'Run pytest'
displayName: Lint with Flake8 and run unit tests
5 changes: 3 additions & 2 deletions ci/azp/linux-1604-pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
displayName: pip install
- bash: |
pip3 install pytest numpy
pip3 install flake8 pytest numpy
flake8 --ignore=E501 --exclude=rtree/__init__.py rtree/
python3 -m pytest --doctest-modules rtree tests/test_*
displayName: pytest
displayName: Lint with Flake8 and run unit tests
5 changes: 3 additions & 2 deletions ci/azp/linux-1804-pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
displayName: pip install
- bash: |
pip3 install pytest numpy
pip3 install flake8 pytest numpy
flake8 --ignore=E501 --exclude=rtree/__init__.py rtree/
python3 -m pytest --doctest-modules rtree tests/test_*
displayName: pytest
displayName: Lint with Flake8 and run unit tests
5 changes: 3 additions & 2 deletions ci/azp/osx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
- bash: |
source activate rtree
pip install pytest numpy
pip install flake8 pytest numpy
flake8 --ignore=E501 --exclude=rtree/__init__.py rtree/
python -m pytest --doctest-modules rtree tests/test_*
displayName: pytest
displayName: Lint with Flake8 and run unit tests
5 changes: 3 additions & 2 deletions ci/azp/win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- script: |
call activate rtree
pip install pytest numpy
pip install flake8 pytest numpy
flake8 --ignore=E501 --exclude=rtree/__init__.py rtree/
python -m pytest --doctest-modules rtree tests
displayName: pytest
displayName: Lint with Flake8 and run unit tests
32 changes: 16 additions & 16 deletions rtree/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ def _load_library(dllname, loadfunction, dllpaths=('', )):
os.environ['PATH'] = oldenv
return None



base_name = 'spatialindex_c'
if '64' in platform.architecture()[0]:
arch = '64'
Expand Down Expand Up @@ -328,23 +326,25 @@ def _load_library(dllname, loadfunction, dllpaths=('', )):
rt.Index_Flush.restype = None
rt.Index_Flush.errcheck = check_void_done

rt.Index_Contains_obj.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.c_uint32,
ctypes.POINTER(
ctypes.POINTER(ctypes.c_void_p)),
ctypes.POINTER(ctypes.c_uint64)]
rt.Index_Contains_obj.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.c_uint32,
ctypes.POINTER(ctypes.POINTER(ctypes.c_void_p)),
ctypes.POINTER(ctypes.c_uint64)
]
rt.Index_Contains_obj.restype = ctypes.c_int
rt.Index_Contains_obj.errcheck = check_return

rt.Index_Contains_id.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.c_uint32,
ctypes.POINTER(
ctypes.POINTER(ctypes.c_int64)),
ctypes.POINTER(ctypes.c_uint64)]
rt.Index_Contains_id.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.c_uint32,
ctypes.POINTER(ctypes.POINTER(ctypes.c_int64)),
ctypes.POINTER(ctypes.c_uint64)
]
rt.Index_Contains_id.restype = ctypes.c_int
rt.Index_Contains_id.errcheck = check_return

Expand Down
85 changes: 33 additions & 52 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,8 @@

from . import core

try:
import cPickle as pickle
except ImportError:
import pickle
import pickle

import sys
if sys.version_info[0] == 2:
range = xrange
string_types = basestring
elif sys.version_info[0] == 3:
string_types = str


def string_output(s):
if sys.version_info[0] == 2:
return s
elif sys.version_info[0] == 3:
return s.decode('UTF-8')

RT_Memory = 0
RT_Disk = 1
Expand Down Expand Up @@ -61,10 +45,12 @@ def _get_bounds(handle, bounds_fn, interleaved):
if (dimension.value == 0):
return None

mins = ctypes.cast(pp_mins, ctypes.POINTER(ctypes.c_double
* dimension.value))
maxs = ctypes.cast(pp_maxs, ctypes.POINTER(ctypes.c_double
* dimension.value))
mins = ctypes.cast(
pp_mins, ctypes.POINTER(ctypes.c_double * dimension.value)
)
maxs = ctypes.cast(
pp_maxs, ctypes.POINTER(ctypes.c_double * dimension.value)
)

results = [mins.contents[i] for i in range(dimension.value)]
results += [maxs.contents[i] for i in range(dimension.value)]
Expand Down Expand Up @@ -94,7 +80,7 @@ def _get_data(handle):
class Index(object):
"""An R-Tree, MVR-Tree, or TPR-Tree indexing object"""

def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs):
"""Creates a new index
:param filename:
Expand Down Expand Up @@ -220,7 +206,7 @@ def __init__(self, *args, **kwargs):
basename = None
storage = None
if args:
if isinstance(args[0], string_types) or isinstance(args[0], bytes):
if isinstance(args[0], str) or isinstance(args[0], bytes):
# they sent in a filename
basename = args[0]
# they sent in a filename, stream
Expand Down Expand Up @@ -283,7 +269,6 @@ def __init__(self, *args, **kwargs):
else:
self.properties.storage = RT_Memory


ps = kwargs.get('pagesize', None)
if ps:
self.properties.pagesize = int(ps)
Expand Down Expand Up @@ -386,7 +371,6 @@ def _serialize(self, obj):
# return serialized to keep it alive for the pointer.
return size, ctypes.cast(p, ctypes.POINTER(ctypes.c_uint8)), serialized


def set_result_limit(self, value):
return core.rt.Index_SetResultSetOffset(self.handle, value)

Expand Down Expand Up @@ -463,7 +447,7 @@ def _insertTP(self, id, coordinates, velocities, time, obj=None):
p_mins, p_maxs = self.get_coordinate_pointers(coordinates)
pv_mins, pv_maxs = self.get_coordinate_pointers(velocities)
# End time isn't used
t_start, t_end = self._get_time_doubles((time, time+1))
t_start, t_end = self._get_time_doubles((time, time + 1))
data = ctypes.c_ubyte(0)
size = 0
if obj is not None:
Expand Down Expand Up @@ -529,7 +513,6 @@ def count(self, coordinates):

return p_num_results.value


def _countTP(self, coordinates, velocities, times):
p_mins, p_maxs = self.get_coordinate_pointers(coordinates)
pv_mins, pv_maxs = self.get_coordinate_pointers(velocities)
Expand Down Expand Up @@ -777,7 +760,7 @@ def _get_objects(self, it, num_results, objects):
yield self.loads(data)

core.rt.Index_DestroyObjResults(its, num_results)
except: # need to catch all exceptions, not just rtree.
except Exception: # need to catch all exceptions, not just rtree.
core.rt.Index_DestroyObjResults(its, num_results)
raise

Expand All @@ -790,7 +773,7 @@ def _get_ids(self, it, num_results):
for i in range(num_results):
yield items.contents[i]
core.rt.Index_Free(its)
except:
except Exception:
core.rt.Index_Free(its)
raise

Expand Down Expand Up @@ -865,10 +848,9 @@ def nearest(self, coordinates, num_results=1, objects=False):
ctypes.byref(it),
p_num_results)

return self._get_ids(it, min(num_results,p_num_results.contents.value))
return self._get_ids(it, min(num_results, p_num_results.contents.value))

def _nearestTP(self, coordinates, velocities, times, num_results=1,
objects=False):
def _nearestTP(self, coordinates, velocities, times, num_results=1, objects=False):
p_mins, p_maxs = self.get_coordinate_pointers(coordinates)
pv_mins, pv_maxs = self.get_coordinate_pointers(velocities)
t_start, t_end = self._get_time_doubles(times)
Expand Down Expand Up @@ -1049,8 +1031,8 @@ def py_next_item(p_id, p_mins, p_maxs, p_dimension, p_data, p_length):
# this code assumes the coords are not interleaved.
# xmin, xmax, ymin, ymax, zmin, zmax
for i in range(dimension):
mins[i] = coordinates[i*2]
maxs[i] = coordinates[(i*2)+1]
mins[i] = coordinates[i * 2]
maxs[i] = coordinates[(i * 2) + 1]

p_mins[0] = ctypes.cast(mins, ctypes.POINTER(ctypes.c_double))
p_maxs[0] = ctypes.cast(maxs, ctypes.POINTER(ctypes.c_double))
Expand Down Expand Up @@ -1144,6 +1126,7 @@ def leaves(self):

return output


# An alias to preserve backward compatibility
Rtree = Index

Expand Down Expand Up @@ -1208,8 +1191,8 @@ def destroy(self):
try:

if self._ptr is not None:
self._destroy(self._ptr)
self._ptr = None
self._destroy(self._ptr)
self._ptr = None
except AttributeError:
pass

Expand Down Expand Up @@ -1239,10 +1222,11 @@ def flush(self):
try:
core.rt.Index_Flush
if self._ptr is not None:
core.rt.Index_Flush(self._ptr)
core.rt.Index_Flush(self._ptr)
except AttributeError:
pass


class IndexStreamHandle(IndexHandle):

_create = core.rt.Index_CreateWithStream
Expand Down Expand Up @@ -1519,23 +1503,21 @@ def set_reinsert_factor(self, value):
"""Reinsert factor"""

def get_filename(self):
s = core.rt.IndexProperty_GetFileName(self.handle)
return string_output(s)
return core.rt.IndexProperty_GetFileName(self.handle).decode()

def set_filename(self, value):
if isinstance(value, string_types):
if isinstance(value, str):
value = value.encode('utf-8')
return core.rt.IndexProperty_SetFileName(self.handle, value)

filename = property(get_filename, set_filename)
"""Index filename for disk storage"""

def get_dat_extension(self):
s = core.rt.IndexProperty_GetFileNameExtensionDat(self.handle)
return string_output(s)
return core.rt.IndexProperty_GetFileNameExtensionDat(self.handle).decode()

def set_dat_extension(self, value):
if isinstance(value, string_types):
if isinstance(value, str):
value = value.encode('utf-8')
return core.rt.IndexProperty_SetFileNameExtensionDat(
self.handle, value)
Expand All @@ -1544,11 +1526,10 @@ def set_dat_extension(self, value):
"""Extension for .dat file"""

def get_idx_extension(self):
s = core.rt.IndexProperty_GetFileNameExtensionIdx(self.handle)
return string_output(s)
return core.rt.IndexProperty_GetFileNameExtensionIdx(self.handle).decode()

def set_idx_extension(self, value):
if isinstance(value, string_types):
if isinstance(value, str):
value = value.encode('utf-8')
return core.rt.IndexProperty_SetFileNameExtensionIdx(
self.handle, value)
Expand Down Expand Up @@ -1858,7 +1839,7 @@ def __init__(self, *args, **kwargs):
[34.37768294..., 26.73758537..., 49.37768294..., 41.73758537...]
"""
if args:
if isinstance(args[0], string_types) \
if isinstance(args[0], str) \
or isinstance(args[0], bytes) \
or isinstance(args[0], ICustomStorage):
raise ValueError('%s supports only in-memory indexes'
Expand Down Expand Up @@ -1978,11 +1959,11 @@ def intersection(self, coordinates, bbox=False):
49.3776829412, 41.7375853734])]
"""
if bbox == False:
if bbox is False:
for id in super(RtreeContainer,
self).intersection(coordinates, bbox):
yield self._objects[id][1]
elif bbox == True:
elif bbox is True:
for value in super(RtreeContainer,
self).intersection(coordinates, bbox):
value.object = self._objects[value.id][1]
Expand All @@ -1992,7 +1973,7 @@ def intersection(self, coordinates, bbox=False):
raise ValueError(
"valid values for the bbox argument are True and False")

def nearest(self, coordinates, num_results = 1, bbox=False):
def nearest(self, coordinates, num_results=1, bbox=False):
"""Returns the ``k``-nearest objects to the given coordinates
in increasing distance order.
Expand Down Expand Up @@ -2026,11 +2007,11 @@ def nearest(self, coordinates, num_results = 1, bbox=False):
>>> idx.insert(object(), (34.37, 26.73, 49.37, 41.73))
>>> hits = idx.nearest((0, 0, 10, 10), 3, bbox=True)
"""
if bbox == False:
if bbox is False:
for id in super(RtreeContainer,
self).nearest(coordinates, num_results, bbox):
yield self._objects[id][1]
elif bbox == True:
elif bbox is True:
for value in super(RtreeContainer,
self).nearest(coordinates, num_results, bbox):
value.object = self._objects[value.id][1]
Expand Down

0 comments on commit d202812

Please sign in to comment.