Skip to content

Commit

Permalink
Use ruff to lint and format code
Browse files Browse the repository at this point in the history
  • Loading branch information
jongbinjung committed Oct 11, 2024
1 parent 6d9a5fc commit 5bd889f
Show file tree
Hide file tree
Showing 19 changed files with 313 additions and 277 deletions.
20 changes: 0 additions & 20 deletions .flake8

This file was deleted.

46 changes: 40 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ Changelog = 'https://package.readthedocs.io/en/latest/changelog.html'

[project.optional-dependencies]
numpy = ['numpy']
test = ['pytest', 'pytest-cov', 'flake8', 'pylint', 'numpy']
test = ['pytest', 'pytest-cov', 'ruff', 'numpy']
all = [
'h3[test]',
'jupyter-book',
'flake8',
'sphinx>=7.3.3', # https://github.com/sphinx-doc/sphinx/issues/12290
'jupyterlab',
'jupyterlab-geojson',
Expand All @@ -59,15 +59,49 @@ all = [
'contextily',
'cartopy',
'geoviews',
'numpy',
'pytest',
'pytest-cov',
'pylint',
]

[tool.pytest.ini_options]
addopts = "--cov=h3 --cov=tests --cov-report=term-missing --durations=10"

[tool.ruff]
src = [
"src",
"tests",
]

lint.select = [
"E",
"F",
"W",
]

lint.extend-select = [
"I",
]

lint.ignore = [
"E251", # unexpected spaces around keyword / parameter equals
"E272", # multiple spaces before keyword
# sometimes I just want to line up decimal points...
"E201", # whitespace after '['
"E241", # multiple spaces after ','
"E731", # do not assign a lambda expression, use a def
]

respect-gitignore = true

[tool.ruff.format]
indent-style = "space"
quote-style = "single"

[tool.ruff.per-file-ignores]
# Do not enforce usage in init files
"__init__.py" = [
"E402",
"F401",
]

[tool.coverage.run]
omit = [
'*/h3/api/basic_int/__init__.py',
Expand Down
2 changes: 0 additions & 2 deletions src/h3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
from ._cy import (
UnknownH3ErrorCode,
H3BaseException,

H3GridNavigationError,
H3MemoryError,
H3ValueError,

H3FailedError,
H3DomainError,
H3LatLngDomainError,
Expand Down
6 changes: 1 addition & 5 deletions src/h3/_cy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@
is_valid_vertex,
)

from .to_multipoly import (
cells_to_multi_polygon
)
from .to_multipoly import cells_to_multi_polygon

from .util import (
c_version,
Expand All @@ -87,11 +85,9 @@
from .error_system import (
UnknownH3ErrorCode,
H3BaseException,

H3GridNavigationError,
H3MemoryError,
H3ValueError,

H3FailedError,
H3DomainError,
H3LatLngDomainError,
Expand Down
48 changes: 15 additions & 33 deletions src/h3/_h3shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ class H3Shape(metaclass=ABCMeta):
"""
Abstract parent class of ``LatLngPoly`` and ``LatLngMultiPoly``.
"""

@property
@abstractmethod
def __geo_interface__(self):
""" https://github.com/pytest-dev/pytest-cov/issues/428 """
"""https://github.com/pytest-dev/pytest-cov/issues/428"""


class LatLngPoly(H3Shape):
Expand Down Expand Up @@ -51,6 +52,7 @@ class LatLngPoly(H3Shape):
... )
<LatLngPoly: [4/(3, 5)]>
"""

def __init__(self, outer, *holes):
loops = [outer] + list(holes)
for loop in loops:
Expand All @@ -63,10 +65,7 @@ def __init__(self, outer, *holes):
raise ValueError('LatLngPoly only accepts 2D points: lat/lng.')

self.outer = tuple(_open_ring(outer))
self.holes = tuple(
_open_ring(hole)
for hole in holes
)
self.holes = tuple(_open_ring(hole) for hole in holes)

def __repr__(self):
return '<LatLngPoly: {}>'.format(self.loopcode)
Expand All @@ -80,7 +79,7 @@ def __len__(self):

@property
def loopcode(self):
""" Short code for describing the length of the outer loop and each hole
"""Short code for describing the length of the outer loop and each hole
Example: ``[382/(18, 6, 6)]`` indicates an outer loop of 382 points,
along with 3 holes with 18, 6, and 6 points, respectively.
Expand Down Expand Up @@ -116,12 +115,13 @@ class LatLngMultiPoly(H3Shape):
polys : list[LatLngPoly]
List of lat/lng points describing the outer loop of the polygon
"""

def __init__(self, *polys):
self.polys = tuple(polys)

for p in self.polys:
if not isinstance(p, LatLngPoly):
raise ValueError('LatLngMultiPoly requires each input to be an LatLngPoly object, instead got: ' + str(p)) # noqa
raise ValueError('LatLngMultiPoly requires each input to be an LatLngPoly object, instead got: ' + str(p)) # fmt: skip

def __repr__(self):
out = [p.loopcode for p in self.polys]
Expand All @@ -133,8 +133,7 @@ def __iter__(self):
return iter(self.polys)

def __len__(self):
""" Give the number of polygons in this multi-polygon.
"""
"""Give the number of polygons in this multi-polygon."""

"""
TODO: Pandas series or dataframe representation changes depending
Expand Down Expand Up @@ -195,19 +194,13 @@ def __geo_interface__(self):


def _mpoly_to_LL3(mpoly):
ll3 = tuple(
_polygon_to_LL2(poly)
for poly in mpoly
)
ll3 = tuple(_polygon_to_LL2(poly) for poly in mpoly)

return ll3


def _LL3_to_mpoly(ll3):
polys = [
_LL2_to_polygon(ll2)
for ll2 in ll3
]
polys = [_LL2_to_polygon(ll2) for ll2 in ll3]

mpoly = LatLngMultiPoly(*polys)

Expand All @@ -216,10 +209,7 @@ def _LL3_to_mpoly(ll3):

def _polygon_to_LL2(poly):
ll2 = [poly.outer] + list(poly.holes)
ll2 = tuple(
_close_ring(_swap_latlng(ll1))
for ll1 in ll2
)
ll2 = tuple(_close_ring(_swap_latlng(ll1)) for ll1 in ll2)

return ll2

Expand All @@ -230,15 +220,9 @@ def _remove_z(ll1):


def _LL2_to_polygon(ll2):
ll2 = [
_remove_z(ll1)
for ll1 in ll2
]

ll2 = [
_swap_latlng(ll1)
for ll1 in ll2
]
ll2 = [_remove_z(ll1) for ll1 in ll2]

ll2 = [_swap_latlng(ll1) for ll1 in ll2]
h3poly = LatLngPoly(*ll2)

return h3poly
Expand All @@ -263,9 +247,7 @@ def _LL3_to_geojson_dict(ll3):


def _swap_latlng(ll1):
ll1 = tuple(
(b, a) for a, b in ll1
)
ll1 = tuple((b, a) for a, b in ll1)
return ll1


Expand Down
15 changes: 8 additions & 7 deletions src/h3/api/basic_int/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
from ... import _cy
from ..._h3shape import (
H3Shape,
LatLngPoly,
LatLngMultiPoly,
LatLngPoly,
geo_to_h3shape,
h3shape_to_geo,
)

from ._convert import (
_in_scalar,
_out_scalar,
_in_collection,
_in_scalar,
_out_collection,
_out_scalar,
)


Expand Down Expand Up @@ -1071,9 +1070,11 @@ def great_circle_distance(latlng1, latlng2, unit='km'):
lat1, lng1 = latlng1
lat2, lng2 = latlng2
return _cy.great_circle_distance(
lat1, lng1,
lat2, lng2,
unit = unit
lat1,
lng1,
lat2,
lng2,
unit=unit,
)


Expand Down
1 change: 1 addition & 0 deletions src/h3/api/numpy_int/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def _in_scalar(x):

def _in_collection(x):
import numpy as np

# array is copied only if dtype does not match
# `list`s should work, but not `set`s of integers
return np.asarray(x, dtype='uint64')
Expand Down
Loading

0 comments on commit 5bd889f

Please sign in to comment.