Skip to content

Commit

Permalink
Use ruff as formatter and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Aug 23, 2024
1 parent 4d70d65 commit 9fdf324
Show file tree
Hide file tree
Showing 158 changed files with 980 additions and 1,150 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Install dependencies
run: |
sudo pip install cmake-format black
sudo pip install cmake-format
- name: Clang Format
run: ./script/clang-format --check
Expand All @@ -23,6 +23,13 @@ jobs:
find . -name 'CMakeLists.txt' -o -name '*.cmake' > cmake-src
xargs cmake-format --check < cmake-src
- name: Black
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: ["3.12"]

- name: Run ruff
run: |
black --check .
pip install ruff
ruff check
ruff format --check
1 change: 0 additions & 1 deletion ci/github/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from setuptools import setup
from setuptools_scm import get_version


version = get_version()


Expand Down
6 changes: 3 additions & 3 deletions cmake/create_cmakelists.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python

from os import listdir
from os.path import isfile, join, isdir, islink
import sys
from os import listdir
from os.path import isdir, isfile, islink, join


def findFilesAndDirectories(directory):
Expand All @@ -11,7 +11,7 @@ def findFilesAndDirectories(directory):
directories = []
for f in all_files:
path = join(directory, f)
if isfile(path) and not f == "CMakeLists.txt" and not islink(path):
if isfile(path) and f != "CMakeLists.txt" and not islink(path):
files.append(f)
if isdir(path):
directories.append(f)
Expand Down
37 changes: 37 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
[build-system]
requires = ["setuptools", "setuptools_scm", "wheel", "scikit-build", "cmake", "conan<2", "ninja"]

[tool.ruff]
src = ["python/resdata"]
line-length = 88

[tool.ruff.lint]
select = [
"W", # pycodestyle
"I", # isort
"B", # flake-8-bugbear
"SIM", # flake-8-simplify
"F", # pyflakes
"PL", # pylint
"NPY", # numpy specific rules
"C4", # flake8-comprehensions
"PD", # pandas-vet
]
ignore = ["PLW2901", # redefined-loop-name
"PLR2004", # magic-value-comparison
"PLR0915", # too-many-statements
"PLR0912", # too-many-branches
"PLR0911", # too-many-return-statements
"PLC2701", # import-private-name
"PLR6201", # literal-membership
"PLR0914", # too-many-locals
"PLR6301", # no-self-use
"PLW1641", # eq-without-hash
"PLR0904", # too-many-public-methods
"PLR1702", # too-many-nested-blocks
"PLW3201", # bad-dunder-method-name
"PD901",
"C409",
"PLC0414",
]

[tool.ruff.lint.pylint]
max-args = 15
14 changes: 4 additions & 10 deletions python/docs/examples/avg_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ def avg_pressure(p, sw, pv, region, region_id, result):

p1 = p.sum(mask=region) / region.active_size()

if total_pv > 0:
p2 = p_pv.sum(mask=region) / total_pv
else:
p2 = None

if total_hc_pv > 0:
p3 = p_hc_pv.sum(mask=region) / total_hc_pv
else:
p3 = None
p2 = p_pv.sum(mask=region) / total_pv if total_pv > 0 else None

p3 = p_hc_pv.sum(mask=region) / total_hc_pv if total_hc_pv > 0 else None
else:
p1 = None
p2 = None
Expand Down Expand Up @@ -71,7 +65,7 @@ def avg_pressure(p, sw, pv, region, region_id, result):
avg_pressure(p, sw, pv, ResdataRegion(grid, True), "field", result)
sim_days.append(header.get_sim_days())

for key in result.keys():
for key in result:
plt.figure(1)
for index, p in enumerate(result[key]):
plt.plot(sim_days, p, label="Region:%s P%d" % (key, index + 1))
Expand Down
4 changes: 2 additions & 2 deletions python/docs/examples/cmp_nnc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python
import sys
from operator import itemgetter
from resdata.resfile import ResdataFile
from resdata.grid import Grid

from resdata.grid import Grid
from resdata.resfile import ResdataFile

if __name__ == "__main__":
case = sys.argv[1]
Expand Down
5 changes: 3 additions & 2 deletions python/docs/examples/grid_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import sys
from resdata.grid import ResdataRegion, Grid

from resdata.grid import Grid, ResdataRegion


def volume_min_max(grid):
Expand All @@ -26,7 +27,7 @@ def main(grid):

if __name__ == "__main__":
if len(sys.argv) < 2:
exit("usage: grid_info.py path/to/file.EGRID")
sys.exit("usage: grid_info.py path/to/file.EGRID")
case = sys.argv[1]
grid = Grid(case)
main(grid)
30 changes: 20 additions & 10 deletions python/resdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from cwrap import Prototype

from .version import version as __version__
from .version import version as __version__ # noqa: F401


def _dlopen_resdata():
Expand All @@ -69,7 +69,7 @@ def _dlopen_resdata():

@ct.CFUNCTYPE(None, ct.c_char_p, ct.c_int, ct.c_char_p, ct.c_char_p, ct.c_char_p)
def _c_abort_handler(filename, lineno, function, message, backtrace):
global _abort_handler
global _abort_handler # noqa: PLW0602
if not _abort_handler:
return
_abort_handler(
Expand All @@ -85,7 +85,7 @@ def set_abort_handler(function):
"""
Set callback function for util_abort, which is called prior to std::abort()
"""
global _abort_handler
global _abort_handler # noqa: PLW0603
_abort_handler = function

ResdataPrototype.lib.util_set_abort_handler(_c_abort_handler)
Expand All @@ -100,15 +100,25 @@ def __init__(self, prototype, bind=True):
)


from .rd_type import ResDataType, ResdataTypeEnum
from .rd_type import ResDataType as ResDataType
from .rd_type import ResdataTypeEnum as ResdataTypeEnum
from .rd_util import (
FileType,
FileMode,
Phase,
UnitSystem,
ResdataUtil,
FileMode as FileMode,
)
from .util.util import ResdataVersion, updateAbortSignals
from .rd_util import (
FileType as FileType,
)
from .rd_util import (
Phase as Phase,
)
from .rd_util import (
ResdataUtil as ResdataUtil,
)
from .rd_util import (
UnitSystem as UnitSystem,
)
from .util.util import ResdataVersion as ResdataVersion
from .util.util import updateAbortSignals

updateAbortSignals()

Expand Down
19 changes: 9 additions & 10 deletions python/resdata/geometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
"""

import resdata
from cwrap import Prototype
import resdata as resdata

from .geo_pointset import GeoPointset
from .geo_region import GeoRegion
from .cpolyline import CPolyline
from .cpolyline_collection import CPolylineCollection
from .polyline import Polyline
from .xyz_io import XYZIo
from .geometry_tools import GeometryTools
from .surface import Surface
from .cpolyline import CPolyline as CPolyline
from .cpolyline_collection import CPolylineCollection as CPolylineCollection
from .geo_pointset import GeoPointset as GeoPointset
from .geo_region import GeoRegion as GeoRegion
from .geometry_tools import GeometryTools as GeometryTools
from .polyline import Polyline as Polyline
from .surface import Surface as Surface
from .xyz_io import XYZIo as XYZIo
12 changes: 4 additions & 8 deletions python/resdata/geometry/cpolyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import os.path

from cwrap import BaseCClass

from resdata import ResdataPrototype

from .geometry_tools import GeometryTools


Expand Down Expand Up @@ -54,10 +56,7 @@ def createFromXYZFile(cls, filename, name=None):

def __str__(self):
name = self.getName()
if name:
str = "%s [" % name
else:
str = "["
str = "%s [" % name if name else "["

for index, p in enumerate(self):
str += "(%g,%g)" % p
Expand Down Expand Up @@ -143,10 +142,7 @@ def extendToBBox(self, bbox, start=True):
intersections = GeometryTools.rayPolygonIntersections(p1, ray_dir, bbox)
if intersections:
p2 = intersections[0][1]
if self.getName():
name = "Extend:%s" % self.getName()
else:
name = None
name = "Extend:%s" % self.getName() if self.getName() else None

return CPolyline(name=name, init_points=[(p1[0], p1[1]), p2])
else:
Expand Down
12 changes: 5 additions & 7 deletions python/resdata/geometry/cpolyline_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
Create a polygon
"""

import ctypes

from cwrap import BaseCClass

from resdata import ResdataPrototype
from resdata.geometry import CPolyline

Expand Down Expand Up @@ -85,11 +84,10 @@ def shallowCopy(self):
def addPolyline(self, polyline, name=None):
if not isinstance(polyline, CPolyline):
polyline = CPolyline(init_points=polyline, name=name)
else:
if not name is None:
raise ValueError(
"The name keyword argument can only be supplied when add not CPOlyline object"
)
elif not name is None:
raise ValueError(
"The name keyword argument can only be supplied when add not CPOlyline object"
)

name = polyline.getName()
if name and name in self:
Expand Down
1 change: 1 addition & 0 deletions python/resdata/geometry/geo_pointset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cwrap import BaseCClass

from resdata import ResdataPrototype


Expand Down
10 changes: 6 additions & 4 deletions python/resdata/geometry/geo_region.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from ctypes import c_double

from cwrap import BaseCClass
from resdata.util.util import IntVector

from resdata import ResdataPrototype

from .cpolyline import CPolyline
from ctypes import c_double

cpair = c_double * 2 # this is a function that maps two doubles to a double*

Expand Down Expand Up @@ -42,7 +44,7 @@ class GeoRegion(BaseCClass):
)

def __init__(self, pointset, preselect=False):
self._preselect = True if preselect else False
self._preselect = bool(preselect)
c_ptr = self._alloc(pointset, self._preselect)
if c_ptr:
super(GeoRegion, self).__init__(c_ptr)
Expand All @@ -68,7 +70,7 @@ def _construct_cline(self, line):
x2, y2 = map(float, p2)
except Exception as err:
err_msg = "Select with pair ((x1,y1), (x2,y2)), not %s (%s)."
raise ValueError(err_msg % (line, err))
raise ValueError(err_msg % (line, err)) from err
x1x2_ptr = cpair(x1, x2)
y1y2_ptr = cpair(y1, y2)
return x1x2_ptr, y1y2_ptr
Expand Down
Loading

0 comments on commit 9fdf324

Please sign in to comment.