Skip to content

Commit 2ff0668

Browse files
authored
Merge pull request #64 from mfisher87/code-autoformatting
Set up `ruff` & `black` with `pre-commit` and document usage
2 parents baa1d6e + 2c63ce5 commit 2ff0668

File tree

13 files changed

+728
-470
lines changed

13 files changed

+728
-470
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Applied `ruff` and `black` (gh-64)
2+
1fec42d0baf90e00d510efd76cb6006fa0c70dc4

.github/workflows/check.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: "Check with pre-commit"
2+
on:
3+
- "push"
4+
- "pull_request"
5+
6+
jobs:
7+
8+
check-with-pre-commit:
9+
runs-on: "ubuntu-latest"
10+
strategy:
11+
matrix:
12+
python-version: ["3.11"]
13+
14+
steps:
15+
- uses: "actions/checkout@v3"
16+
17+
- name: "Set up Python ${{ matrix.python-version }}"
18+
uses: "actions/setup-python@v4"
19+
with:
20+
python-version: "${{ matrix.python-version }}"
21+
22+
- name: "Install pre-commit"
23+
run: "pip install pre-commit"
24+
25+
- name: "Run checks with pre-commit"
26+
run: "pre-commit run --all-files --show-diff-on-failure --color always"

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
repos:
2+
- repo: "https://github.com/charliermarsh/ruff-pre-commit"
3+
rev: "v0.0.269"
4+
hooks:
5+
- id: "ruff"
6+
# NOTE: "--exit-non-zero-on-fix" is important for CI to function
7+
# correctly!
8+
args: ["--fix", "--exit-non-zero-on-fix"]
9+
10+
- repo: "https://github.com/psf/black"
11+
rev: "23.3.0"
12+
hooks:
13+
- id: "black"

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Contact:
2424
Nathaniel J. Smith <njs@pobox.com> and Stéfan van der Walt <stefanv@berkeley.edu>
2525

2626
Dependencies:
27-
* Python 3.7+
27+
* Python 3.8+
2828
* `colorspacious <https://pypi.python.org/pypi/colorspacious>`_
2929
* Matplotlib
3030
* NumPy

doc/contributing.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,19 @@ rm -rf dist
2121
python -m build
2222
pip install dist/*.whl # or `dist/*.tar.gz`
2323
```
24+
25+
26+
## Code formatting and linting
27+
28+
This codebase uses [black](https://black.readthedocs.io/en/stable/) and
29+
[ruff](https://github.com/charliermarsh/ruff) to automatically format and lint the code.
30+
31+
[`pre-commit`](https://pre-commit.com/) is configured to run them automatically. You can
32+
trigger this manually with `pre-commit run --all-files`.
33+
34+
Thanks to pre-commit, all commits should be formatted. In cases where formatting needs
35+
to be fixed (e.g. changing config of a linter), a format-only commit should be created,
36+
and then another commit should immediately follow which updates
37+
`.git-blame-ignore-revs`. For example:
38+
[1fec42d](https://github.com/matplotlib/viscm/pull/64/commits/1fec42d0baf90e00d510efd76cb6006fa0c70dc4),
39+
[8aa7bb0](https://github.com/matplotlib/viscm/pull/64/commits/8aa7bb01440aeca6f8bbcefe0671c28f2ce284c6).

environment.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ channels:
33
- "conda-forge"
44
- "nodefaults"
55
dependencies:
6+
# Runtime
67
- "python ~=3.11"
78
- "numpy ~=1.24"
89
- "matplotlib ~=3.7"
910
- "colorspacious ~=1.1"
1011
- "scipy ~=1.10"
12+
13+
# Development
14+
- "pre-commit"
15+
1116
- pip:
1217
- "build ~=0.10"

pyproject.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ classifiers = [
1515
"Programming Language :: Python :: 3",
1616
]
1717

18-
requires-python = "~=3.7"
18+
requires-python = "~=3.8"
1919
dependencies = [
2020
"numpy",
2121
"matplotlib",
@@ -45,4 +45,12 @@ packages = {find = {}}
4545
package-data = {viscm = ["examples/*"]}
4646

4747

48-
# [tool.black]
48+
[tool.black]
49+
target-version = ["py38", "py39", "py310", "py311"]
50+
51+
[tool.ruff]
52+
target-version = "py38"
53+
select = ["F", "E", "W", "C90", "I", "UP", "YTT", "B", "A", "C4", "T10", "RUF"]
54+
55+
[tool.ruff.mccabe]
56+
max-complexity = 11

tests.py

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from viscm.gui import *
2-
from viscm.bezierbuilder import *
31
import numpy as np
4-
import matplotlib as mpl
5-
from matplotlib.backends.qt_compat import QtGui, QtCore
6-
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
72

8-
cms = {"viscm/examples/sample_linear.jscm",
9-
"viscm/examples/sample_diverging.jscm",
10-
"viscm/examples/sample_diverging_continuous.jscm"}
3+
from viscm.bezierbuilder import json
4+
from viscm.gui import Colormap, viscm_editor
5+
6+
cms = {
7+
"viscm/examples/sample_linear.jscm",
8+
"viscm/examples/sample_diverging.jscm",
9+
"viscm/examples/sample_diverging_continuous.jscm",
10+
}
1111

1212

1313
def test_editor_loads_native():
@@ -16,8 +16,13 @@ def test_editor_loads_native():
1616
data = json.loads(f.read())
1717
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
1818
cm.load(k)
19-
viscm = viscm_editor(uniform_space=cm.uniform_space, cmtype=cm.cmtype, method=cm.method, **cm.params)
20-
assert viscm.name == data["name"]
19+
viscm = viscm_editor(
20+
uniform_space=cm.uniform_space,
21+
cmtype=cm.cmtype,
22+
method=cm.method,
23+
**cm.params,
24+
)
25+
assert viscm.name == data["name"]
2126

2227
extensions = data["extensions"]["https://matplotlib.org/viscm"]
2328
xp, yp, fixed = viscm.control_point_model.get_control_points()
@@ -26,7 +31,7 @@ def test_editor_loads_native():
2631
assert len(extensions["xp"]) == len(xp)
2732
assert len(extensions["yp"]) == len(yp)
2833
assert len(xp) == len(yp)
29-
for i in range(len(xp)):
34+
for i in range(len(xp)):
3035
assert extensions["xp"][i] == xp[i]
3136
assert extensions["yp"][i] == yp[i]
3237
assert extensions["min_Jp"] == viscm.min_Jp
@@ -35,19 +40,34 @@ def test_editor_loads_native():
3540
assert extensions["cmtype"] == viscm.cmtype
3641

3742
colors = data["colors"]
38-
colors = [[int(c[i:i + 2], 16) / 256 for i in range(0, 6, 2)] for c in [colors[i:i + 6] for i in range(0, len(colors), 6)]]
43+
colors = [
44+
[int(c[i : i + 2], 16) / 256 for i in range(0, 6, 2)]
45+
for c in [colors[i : i + 6] for i in range(0, len(colors), 6)]
46+
]
3947
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
4048
for i in range(len(colors)):
4149
for z in range(3):
4250
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)
4351

52+
53+
# import matplotlib as mpl
54+
# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
55+
# from matplotlib.backends.qt_compat import QtCore, QtGui
56+
#
4457
# def test_editor_add_point():
4558
# # Testing linear
46-
59+
#
4760
# fig = plt.figure()
4861
# figure_canvas = FigureCanvas(fig)
49-
# linear = viscm_editor(min_Jp=40, max_Jp=60, xp=[-10, 10], yp=[0,0], figure=fig, cmtype="linear")
50-
62+
# linear = viscm_editor(
63+
# min_Jp=40,
64+
# max_Jp=60,
65+
# xp=[-10, 10],
66+
# yp=[0,0],
67+
# figure=fig,
68+
# cmtype="linear",
69+
# )
70+
#
5171
# Jp, ap, bp = linear.cmap_model.get_Jpapbp(3)
5272
# eJp, eap, ebp = [40, 50, 60], [-10, 0, 10], [0, 0, 0]
5373
# for i in range(3):
@@ -61,12 +81,24 @@ def test_editor_loads_native():
6181
# for i in range(3):
6282
# for z in range(3):
6383
# assert approxeq(rgb[i][z], ergb[i][z])
64-
84+
6585

6686
# # Testing adding a point to linear
6787
# linear.bezier_builder.mode = "add"
68-
# qtEvent = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, QtCore.QPoint(), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.ShiftModifier)
69-
# event = mpl.backend_bases.MouseEvent("button_press_event", figure_canvas, 0, 10, guiEvent=qtEvent)
88+
# qtEvent = QtGui.QMouseEvent(
89+
# QtCore.QEvent.MouseButtonPress,
90+
# QtCore.QPoint(),
91+
# QtCore.Qt.LeftButton,
92+
# QtCore.Qt.LeftButton,
93+
# QtCore.Qt.ShiftModifier,
94+
# )
95+
# event = mpl.backend_bases.MouseEvent(
96+
# "button_press_event",
97+
# figure_canvas,
98+
# 0,
99+
# 10,
100+
# guiEvent=qtEvent,
101+
# )
70102
# event.xdata = 0
71103
# event.ydata = 10
72104
# event.inaxes = linear.bezier_builder.ax
@@ -87,8 +119,20 @@ def test_editor_loads_native():
87119

88120
# # Removing a point from linear
89121
# linear.bezier_builder.mode = "remove"
90-
# qtEvent = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, QtCore.QPoint(), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.ControlModifier)
91-
# event = mpl.backend_bases.MouseEvent("button_press_event", figure_canvas, 0, 10, guiEvent=qtEvent)
122+
# qtEvent = QtGui.QMouseEvent(
123+
# QtCore.QEvent.MouseButtonPress,
124+
# QtCore.QPoint(),
125+
# QtCore.Qt.LeftButton,
126+
# QtCore.Qt.LeftButton,
127+
# QtCore.Qt.ControlModifier,
128+
# )
129+
# event = mpl.backend_bases.MouseEvent(
130+
# "button_press_event",
131+
# figure_canvas,
132+
# 0,
133+
# 10,
134+
# guiEvent=qtEvent,
135+
# )
92136
# event.xdata = 0
93137
# event.ydata = 10
94138
# event.inaxes = linear.bezier_builder.ax
@@ -102,7 +146,5 @@ def test_editor_loads_native():
102146
# # print(linear.cmap_model.get_Jpapbp(3))
103147

104148

105-
106149
def approxeq(x, y, err=0.0001):
107150
return abs(y - x) < err
108-

viscm/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
# See file LICENSE.txt for license information.
55

66
from .cli import cli
7+
78
cli()

0 commit comments

Comments
 (0)