Skip to content

Commit 449e220

Browse files
authored
MAINT flake8 to ruff and re-run all linters on the full repo (#821)
1 parent fc2d5e3 commit 449e220

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+764
-494
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ doc/_build
3333
.idea
3434
*.code-workspace
3535
.vscode
36-

.jupyter/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
This directory is to setup jupyter on binder
2-

.jupyter/jupyter_notebook_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# To use jupytext in binder
2-
c.ContentsManager.preferred_jupytext_formats_read = 'py:percent' # noqa
2+
c.ContentsManager.preferred_jupytext_formats_read = "py:percent" # noqa

.pre-commit-config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ repos:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
exclude: notebooks
8+
exclude_types: [svg]
89
- id: trailing-whitespace
910
exclude: notebooks
11+
exclude_types: [svg]
1012
- repo: https://github.com/psf/black
1113
rev: 23.1.0
1214
hooks:
1315
- id: black
14-
- repo: https://github.com/pycqa/flake8
15-
rev: 4.0.1
16+
- repo: https://github.com/astral-sh/ruff-pre-commit
17+
rev: v0.11.2
1618
hooks:
17-
- id: flake8
18-
entry: pflake8
19-
additional_dependencies: [pyproject-flake8]
20-
types: [file, python]
19+
- id: ruff
20+
args: ["--fix", "--output-format=full"]

build_tools/generate-exercise-from-solution.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from jupytext.myst import myst_to_notebook
66
import jupytext
77

8+
89
def replace_simple_text(input_py_str):
910
result = input_py_str.replace("📃 Solution for", "📝")
1011
return result
@@ -19,28 +20,35 @@ def remove_solution(input_py_str):
1920
before this comment and add "# Write your code here." at the end of the
2021
cell.
2122
"""
22-
nb = jupytext.reads(input_py_str, fmt='py:percent')
23+
nb = jupytext.reads(input_py_str, fmt="py:percent")
2324

24-
cell_tags_list = [c['metadata'].get('tags') for c in nb.cells]
25-
is_solution_list = [tags is not None and 'solution' in tags
26-
for tags in cell_tags_list]
25+
cell_tags_list = [c["metadata"].get("tags") for c in nb.cells]
26+
is_solution_list = [
27+
tags is not None and "solution" in tags for tags in cell_tags_list
28+
]
2729
# Completely remove cells with "solution" tags
28-
nb.cells = [cell for cell, is_solution in zip(nb.cells, is_solution_list)
29-
if not is_solution]
30+
nb.cells = [
31+
cell
32+
for cell, is_solution in zip(nb.cells, is_solution_list)
33+
if not is_solution
34+
]
3035

3136
# Partial cell removal based on "# solution" comment
3237
marker = "# solution"
33-
pattern = re.compile(f"^{marker}.*", flags=re.MULTILINE|re.DOTALL)
38+
pattern = re.compile(f"^{marker}.*", flags=re.MULTILINE | re.DOTALL)
3439

35-
cells_to_modify = [c for c in nb.cells if c["cell_type"] == "code" and
36-
marker in c["source"]]
40+
cells_to_modify = [
41+
c
42+
for c in nb.cells
43+
if c["cell_type"] == "code" and marker in c["source"]
44+
]
3745

3846
for c in cells_to_modify:
3947
c["source"] = pattern.sub("# Write your code here.", c["source"])
4048

4149
# TODO: we could potentially try to avoid changing the input file jupytext
4250
# header since this info is rarely useful. Let's keep it simple for now.
43-
py_nb_str = jupytext.writes(nb, fmt='py:percent')
51+
py_nb_str = jupytext.writes(nb, fmt="py:percent")
4452
return py_nb_str
4553

4654

@@ -49,7 +57,7 @@ def write_exercise(solution_path, exercise_path):
4957

5058
output_str = input_str
5159
for replace_func in [replace_simple_text, remove_solution]:
52-
output_str= replace_func(output_str)
60+
output_str = replace_func(output_str)
5361
exercise_path.write_text(output_str)
5462

5563

@@ -70,12 +78,14 @@ def write_all_exercises(python_scripts_folder):
7078
if path.is_dir():
7179
write_all_exercises(path)
7280
else:
73-
if '_ex_' not in str(path):
81+
if "_ex_" not in str(path):
7482
raise ValueError(
75-
f'Path argument should be an exercise file. Path was {path}')
83+
f"Path argument should be an exercise file. Path was {path}"
84+
)
7685
solution_path = Path(str(path).replace("_ex_", "_sol_"))
7786
if not solution_path.exists():
7887
raise ValueError(
79-
f"{solution_path} does not exist, check argument path {path}")
88+
f"{solution_path} does not exist, check argument path {path}"
89+
)
8090

8191
write_exercise(solution_path, path)

build_tools/generate-index.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_first_title(path):
4141
elif path.suffix == ".md":
4242
md_str = path.read_text()
4343
else:
44-
raise ValueError(f"{filename} is not a .py or a .md file")
44+
raise ValueError(f"{path} is not a .py or a .md file")
4545

4646
return get_first_title_from_md_str(md_str)
4747

@@ -96,7 +96,9 @@ def get_single_file_markdown(docname):
9696
# This is simpler to point to inria.github.io generated HTML otherwise
9797
# there are quirks (MyST in quizzes not supported, slides not working,
9898
# etc ...)
99-
relative_url = str(target).replace("jupyter-book/", "").replace(".md", ".html")
99+
relative_url = (
100+
str(target).replace("jupyter-book/", "").replace(".md", ".html")
101+
)
100102
target = f"https://inria.github.io/scikit-learn-mooc/{relative_url}"
101103

102104
return f"[{title}]({target})"
@@ -140,7 +142,9 @@ def test_get_lesson_markdown():
140142
documents = json_info["documents"]
141143
print(
142144
get_lesson_markdown(
143-
documents["predictive_modeling_pipeline/01_tabular_data_exploration_index"]
145+
documents[
146+
"predictive_modeling_pipeline/01_tabular_data_exploration_index"
147+
]
144148
)
145149
)
146150

@@ -156,7 +160,8 @@ def get_module_markdown(module_dict, documents):
156160
module_title = module_dict["caption"]
157161
heading = f"# {module_title}"
158162
content = "\n\n".join(
159-
get_lesson_markdown(documents[docname]) for docname in module_dict["items"]
163+
get_lesson_markdown(documents[docname])
164+
for docname in module_dict["items"]
160165
)
161166
return f"{heading}\n\n{content}"
162167

@@ -219,7 +224,9 @@ def get_full_index_ipynb(toc_path):
219224
md_str = get_full_index_markdown(toc_path)
220225
nb = jupytext.reads(md_str, format=".md")
221226

222-
nb = nbformat.v4.new_notebook(cells=[nbformat.v4.new_markdown_cell(md_str)])
227+
nb = nbformat.v4.new_notebook(
228+
cells=[nbformat.v4.new_markdown_cell(md_str)]
229+
)
223230

224231
# nb_content = jupytext.writes(nb, fmt=".ipynb")
225232
# nb = json.loads(nb_content)

build_tools/generate-quizzes.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ def remove_solution(input_myst_str):
1313
"""
1414
nb = myst_to_notebook(input_myst_str)
1515

16-
cell_tags_list = [c['metadata'].get('tags') for c in nb.cells]
17-
is_solution_list = [tags is not None and 'solution' in tags
18-
for tags in cell_tags_list]
19-
nb.cells = [cell for cell, is_solution in zip(nb.cells, is_solution_list)
20-
if not is_solution]
21-
22-
myst_nb_str = jupytext.writes(nb, fmt='myst')
23-
24-
header_pattern = re.compile(r"---\njupytext.+---\s*",
25-
re.DOTALL | re.MULTILINE)
16+
cell_tags_list = [c["metadata"].get("tags") for c in nb.cells]
17+
is_solution_list = [
18+
tags is not None and "solution" in tags for tags in cell_tags_list
19+
]
20+
nb.cells = [
21+
cell
22+
for cell, is_solution in zip(nb.cells, is_solution_list)
23+
if not is_solution
24+
]
25+
26+
myst_nb_str = jupytext.writes(nb, fmt="myst")
27+
28+
header_pattern = re.compile(
29+
r"---\njupytext.+---\s*", re.DOTALL | re.MULTILINE
30+
)
2631
return re.sub(header_pattern, "", myst_nb_str)
2732

2833

@@ -39,12 +44,14 @@ def write_all_exercises(input_root_path, output_root_path):
3944

4045
for input_path in input_exercises:
4146
# FIXME there may be a better way with the pathlib API
42-
relative_path_str = re.sub(str(input_root_path) + "/?", "",
43-
str(input_path))
47+
relative_path_str = re.sub(
48+
str(input_root_path) + "/?", "", str(input_path)
49+
)
4450
output_path = Path(output_root_path).joinpath(relative_path_str)
4551
print(str(input_path), str(output_path))
4652
write_exercise_myst(input_path, output_path)
4753

54+
4855
if __name__ == "__main__":
4956
input_root_path = sys.argv[1]
5057
output_root_path = sys.argv[2]

build_tools/sanity-check.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
# TODO: we could get the list from .gitignore
66
IGNORE_LIST = [
7-
'.ipynb_checkpoints',
8-
'__pycache__',
7+
".ipynb_checkpoints",
8+
"__pycache__",
99
]
1010

1111
folder1, folder2 = sys.argv[1:3]
@@ -28,6 +28,7 @@ def get_basename(folder):
2828
only_in_folder2 = set(basenames2) - set(basenames1)
2929

3030
raise RuntimeError(
31-
f'Inconsistency between folder {folder1} and {folder2}\n'
32-
f'Only in folder {folder1}: {only_in_folder1}\n'
33-
f'Only in folder {folder2}: {only_in_folder2}')
31+
f"Inconsistency between folder {folder1} and {folder2}\n"
32+
f"Only in folder {folder1}: {only_in_folder1}\n"
33+
f"Only in folder {folder2}: {only_in_folder2}"
34+
)

datasets/penguins.csv

-1
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,3 @@ PAL0910,65,Chinstrap penguin (Pygoscelis antarctica),Anvers,Dream,"Adult, 1 Egg
343343
PAL0910,66,Chinstrap penguin (Pygoscelis antarctica),Anvers,Dream,"Adult, 1 Egg Stage",N99A2,No,2009-11-21,49.6,18.2,193,3775,MALE,9.4618,-24.70615,Nest never observed with full clutch.
344344
PAL0910,67,Chinstrap penguin (Pygoscelis antarctica),Anvers,Dream,"Adult, 1 Egg Stage",N100A1,Yes,2009-11-21,50.8,19,210,4100,MALE,9.98044,-24.68741,NA
345345
PAL0910,68,Chinstrap penguin (Pygoscelis antarctica),Anvers,Dream,"Adult, 1 Egg Stage",N100A2,Yes,2009-11-21,50.2,18.7,198,3775,FEMALE,9.39305,-24.25255,NA
346-

figures/plot_iris_visualization.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@
1818
plt.figure(figsize=(2.5, 2))
1919
patches = list()
2020
for this_y, target_name in enumerate(iris.target_names):
21-
patch = plt.hist(x[y == this_y],
22-
bins=np.linspace(x.min(), x.max(), 20),
23-
label=target_name)
21+
patch = plt.hist(
22+
x[y == this_y],
23+
bins=np.linspace(x.min(), x.max(), 20),
24+
label=target_name,
25+
)
2426
patches.append(patch[-1][0])
2527
style_figs.light_axis()
26-
feature_name = feature_name.replace(' ', '_')
27-
feature_name = feature_name.replace('(', '')
28-
feature_name = feature_name.replace(')', '')
29-
plt.savefig('iris_{}_hist.svg'.format(feature_name))
28+
feature_name = feature_name.replace(" ", "_")
29+
feature_name = feature_name.replace("(", "")
30+
feature_name = feature_name.replace(")", "")
31+
plt.savefig("iris_{}_hist.svg".format(feature_name))
3032

31-
plt.figure(figsize=(6, .25))
32-
plt.legend(patches, iris.target_names, ncol=3, loc=(0, -.37),
33-
borderaxespad=0)
33+
plt.figure(figsize=(6, 0.25))
34+
plt.legend(patches, iris.target_names, ncol=3, loc=(0, -0.37), borderaxespad=0)
3435
style_figs.no_axis()
35-
plt.savefig('legend_irises.svg')
36-
37-
36+
plt.savefig("legend_irises.svg")

0 commit comments

Comments
 (0)