Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add Ruff Formatter and Linter #139

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This workflow will compare tapenade file.
name: Tapenade Comparison
# This workflow will check code (tapenade compare and ruff check)
name: Code Checks

on:

Expand All @@ -25,11 +25,26 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Install dependencies
run: |
sudo apt-get update -y
sudo apt-get install build-essential make openjdk-11-jre-headless
python -m venv test
. test/bin/activate
python -m pip install --upgrade pip
pip install ruff

- name: Compare
- name: Tapenade Compare
run: |
make tap-cmp

- name: Ruff Check
run: |
. test/bin/activate
make check
28 changes: 15 additions & 13 deletions dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ def find_use_statement(f: pathlib.PosixPath) -> set[str]:

Notes
-----
An explanation of the pattern: r"^(?!\s*[!]).*?use\s+(\w+)"
An explanation of the pattern: r"^(?!\\s*[!]).*?use\\s+(\\w+)"

- ^ : Matches the start of a line
- (?!\s*[!]) : Negative lookahead that excludes matches with ! after zero or more whitespace characters (\s)
- .*? : Lazy match of any character (.) zero or more times (*) until it encounters the next part of the pattern
- use\s+ : Matches the string "use" followed by one or more whitespace characters (\s)
- (\w+) : Captures one or more word characters (\w+) after "use" as a group
- (?!\\s*[!]) : Negative lookahead that excludes matches with ! after zero or more whitespace characters
(\\s)
- .*? : Lazy match of any character (.) zero or more times (*) until it encounters the next part of the
pattern
- use\\s+ : Matches the string "use" followed by one or more whitespace characters (\\s)
- (\\w+) : Captures one or more word characters (\\w+) after "use" as a group

So this regex pattern looks for lines that start with "use" followed by one or more whitespace characters,
but excludes lines that have a comment symbol (!) before the "use".
Expand Down Expand Up @@ -109,7 +111,7 @@ def get_dependencies(files: list[pathlib.PosixPath]) -> dict[list]:
"""
ret = {}

for i, f in enumerate(files):
for f in files:
deps = find_use_statement(f)

ret.update({f.stem: deps})
Expand All @@ -131,7 +133,8 @@ def sort_by_dependencies(files: list[pathlib.PosixPath]):
-----
The sorting algorithm is applied sequentially for each module and there is the explanation for one:
- We look first for it's dependencies and we move the module behind each dependency (Forward loop)
- We look for dependencies for the current module in other module and we move the current module above each dependency (Backward loop)
- We look for dependencies for the current module in other module and we move the current module above
each dependency (Backward loop)
"""

# % Get dependencies dictionary
Expand Down Expand Up @@ -178,14 +181,13 @@ def sort_by_dependencies(files: list[pathlib.PosixPath]):
if __name__ == "__main__":
fcore_path = pathlib.Path("smash/fcore/")

# % The files are presorted to avoid the randomness that makes the order of dependencies change even if there are no new files.
# % The files are presorted to avoid the randomness that makes the order of dependencies change even if
# % there are no new files.
# % There can be several ways to sort files by dependencies
c_files = sorted(list(fcore_path.glob("*/*.c")))
f77_files = sorted(list(fcore_path.glob("*/*.f")))
c_files = sorted(fcore_path.glob("*/*.c"))
f77_files = sorted(fcore_path.glob("*/*.f"))
# % In case we have generated f90wrap files we must remove them
f90_files = sorted(
list(set(fcore_path.glob("*/*.f90")) - set(fcore_path.glob("f90wrap/*.f90")))
)
f90_files = sorted(set(fcore_path.glob("*/*.f90")) - set(fcore_path.glob("f90wrap/*.f90")))

sort_by_dependencies(f90_files)

Expand Down
10 changes: 4 additions & 6 deletions doc/gen_dataset.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import argparse
import os

DATASET = ["Cance", "France", "Lez"]
DATASET_PATH = f"{os.path.dirname(os.path.realpath(__file__))}/../smash/factory/dataset"

# TODO: Refactorize this when it gets more complicated. Pass to each dataset the files or directories
# that we want to be uploaded and used in the documentation.
if __name__ == "__main__":

parser = argparse.ArgumentParser()

parser.add_argument(
Expand Down Expand Up @@ -40,13 +39,12 @@
os.system(f"cp -r {DATASET_PATH}/{ds}/pet {DATASET_PATH}/{ds}/prcp {ds_dir}/.")

if ds == "Cance":
os.system(
f"cp -r {DATASET_PATH}/{ds}/qobs {DATASET_PATH}/{ds}/gauge_attributes.csv {ds_dir}/."
)
os.system(f"cp -r {DATASET_PATH}/{ds}/qobs {DATASET_PATH}/{ds}/gauge_attributes.csv {ds_dir}/.")

elif ds == "Lez":
os.system(
f"cp -r {DATASET_PATH}/{ds}/qobs {DATASET_PATH}/{ds}/gauge_attributes.csv {DATASET_PATH}/{ds}/descriptor {ds_dir}/."
f"cp -r {DATASET_PATH}/{ds}/qobs {DATASET_PATH}/{ds}/gauge_attributes.csv "
f"{DATASET_PATH}/{ds}/descriptor {ds_dir}/."
)

if args.tar:
Expand Down
27 changes: 8 additions & 19 deletions doc/source/_ext/custom_options_directive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ruff: noqa

import pydoc
import sphinx
import inspect
Expand Down Expand Up @@ -52,9 +54,7 @@ class SmashModelOptimize(PythonDomain):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.directives = dict(self.directives)
self.directives["function"] = wrap_mangling_directive(
self.directives["function"]
)
self.directives["function"] = wrap_mangling_directive(self.directives["function"])


class SmashNetAdd(PythonDomain):
Expand All @@ -63,9 +63,7 @@ class SmashNetAdd(PythonDomain):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.directives = dict(self.directives)
self.directives["function"] = wrap_mangling_directive(
self.directives["function"]
)
self.directives["function"] = wrap_mangling_directive(self.directives["function"])


class SmashNetCompile(PythonDomain):
Expand All @@ -74,9 +72,7 @@ class SmashNetCompile(PythonDomain):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.directives = dict(self.directives)
self.directives["function"] = wrap_mangling_directive(
self.directives["function"]
)
self.directives["function"] = wrap_mangling_directive(self.directives["function"])


BLURB = """
Expand Down Expand Up @@ -107,9 +103,7 @@ def run(self):
# Format signature taking implementation into account
args = list(args)

if (
defaults is not None
): # only remove args and set default if defaults is not None
if defaults is not None: # only remove args and set default if defaults is not None
defaults = list(defaults)

def set_default(arg, value):
Expand All @@ -134,9 +128,7 @@ def remove_arg(arg):
options.append(
(
opt_name,
impl_defaults[
len(impl_defaults) - (len(impl_args) - j)
],
impl_defaults[len(impl_defaults) - (len(impl_args) - j)],
)
)
else:
Expand Down Expand Up @@ -167,10 +159,7 @@ def remove_arg(arg):
for parameters in signature.parameters.values():
if parameters.name == "self":
continue
elif (
parameters.name == "mapping"
and self.options["alg"] == "l-bfgs-b"
):
elif parameters.name == "mapping" and self.options["alg"] == "l-bfgs-b":
default = "'distributed'"
elif parameters.name == "optimizer":
default = f"'{self.options['opt']}'"
Expand Down
4 changes: 2 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import inspect
import os
import pathlib
import sys
import warnings
import inspect
import pathlib

import smash

Expand Down
10 changes: 10 additions & 0 deletions doc/source/contributor_guide/development_process_details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ of each command, please look directly at the makefile.
and an hdf5 file storing the new baseline (``new_baseline.hdf5``). Once you have ensured that all tests are successfully passed,
rename the ``new_baseline.hdf5`` file to ``baseline.hdf5`` and remove the previous version.

- ``format``
Format Python and Fortran files. Python files are formatted with `Ruff <https://docs.astral.sh/ruff/formatter/>`__
and Fortran files are formatted with `Fprettify <https://github.com/pseewald/fprettify/>`__.
Ruff formatter and fprettify options can be found, respectively, in the ``pyproject.toml`` file and ``makefile``

- ``check``
Check Python files. Pythons files are checked with `Ruff <https://docs.astral.sh/ruff/linter/>`__.
Ruff linters rules can be found in the ``pyproject.toml`` file. This checks are also run in the continuous
integration service (``CI``).

Fortran guideline
-----------------

Expand Down
12 changes: 3 additions & 9 deletions doc/source/gen_rst.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import pathlib
import sys

if __name__ == "__main__":
if len(sys.argv) < 2:
Expand All @@ -13,20 +13,14 @@
exists_input = input("Overwrite existing file ([y]/n) ? ")

if exists_input.lower() not in ["", "y", "yes"]:
exit(0)
sys.exit(0)

file_path.parent.mkdir(exist_ok=True)

if file_path.stem == "index":
rst_label = ".. _" + str(file_path.parent).replace("/", ".") + ":"
else:
rst_label = (
".. _"
+ str(file_path.parent).replace("/", ".")
+ "."
+ file_path.stem
+ ":"
)
rst_label = ".. _" + str(file_path.parent).replace("/", ".") + "." + file_path.stem + ":"

rst_main_title = " ".join(word.capitalize() for word in file_path.stem.split("_"))
len_rst_main_title = len(rst_main_title)
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ dependencies:
- pytest-cov

# format
- black
- ruff
- fprettify
Loading
Loading