Skip to content

Commit 7885e17

Browse files
authored
adding flake8 to precommit, precommit to github workflow (#87)
* adding flake8 to precommit * adding precommit to github workflow * updating build instructions * adding CODE_OF_CONDUCT.md
1 parent 1c5690c commit 7885e17

25 files changed

+262
-90
lines changed

Diff for: .flake8

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[flake8]
2+
filename = *.py
3+
max_line_length = 80
4+
max-doc-length = 80
5+
show-source = True
6+
extend-ignore = E203
7+
8+
exclude =
9+
build
10+
.git
11+
12+
13+
per-file-ignores =
14+
examples/stencil-2d.py: E402
15+
scripts/code_gen.py: F821
16+
sharpy/__init__.py: F401, E501
17+
sharpy/ndarray.py: F401, E501

Diff for: .github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ jobs:
118118
run: |
119119
. $GITHUB_WORKSPACE/third_party/install/miniconda/etc/profile.d/conda.sh
120120
. $GITHUB_WORKSPACE/third_party/install/miniconda/bin/activate sharpy
121-
MLIRROOT=$GITHUB_WORKSPACE/third_party/install/llvm-mlir IMEXROOT=$GITHUB_WORKSPACE/third_party/install/imex python setup.py install
121+
MLIRROOT=$GITHUB_WORKSPACE/third_party/install/llvm-mlir IMEXROOT=$GITHUB_WORKSPACE/third_party/install/imex python -m pip install . -v
122122
# SHARPY_ROOT=`pip show -f sharpy | grep Location | awk '{print $2}'`
123123
# for some reason we cannot use the idtr from the install, not even a copy works
124124
libidtr=`ls ${PWD}/build/lib.linux-x86_64-cpython-*/sharpy/libidtr.so`

Diff for: .github/workflows/pre-commit.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: pre-commit
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [master]
7+
8+
permissions: read-all
9+
10+
jobs:
11+
pre-commit:
12+
runs-on: ubuntu-20.04
13+
steps:
14+
- uses: actions/[email protected]
15+
- uses: actions/setup-python@v5
16+
with:
17+
python-version: '3.10'
18+
- name: Version of clang-format
19+
run: |
20+
clang-format --version
21+
- uses: pre-commit/[email protected]

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
src/include/sharpy/p2c_ids.hpp
2+
.vscode
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

Diff for: .pre-commit-config.yaml

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ repos:
1717
- id: fix-byte-order-marker
1818
- id: mixed-line-ending
1919
- id: trailing-whitespace
20-
- repo: https://github.com/pocc/pre-commit-hooks
21-
rev: v1.3.5
22-
hooks:
23-
- id: clang-format
24-
args: ["-i"]
2520
- repo: https://github.com/psf/black
2621
rev: 23.12.0
2722
hooks:
2823
- id: black
24+
args: ["--line-length", "80"]
2925
language_version: python3
3026
- repo: https://github.com/PyCQA/bandit
3127
rev: '1.7.7'
@@ -37,3 +33,12 @@ repos:
3733
hooks:
3834
- id: isort
3935
name: isort (python)
36+
- repo: https://github.com/pycqa/flake8
37+
rev: 5.0.4
38+
hooks:
39+
- id: flake8
40+
- repo: https://github.com/pocc/pre-commit-hooks
41+
rev: v1.3.5
42+
hooks:
43+
- id: clang-format
44+
args: ["-i"]

Diff for: CODE_OF_CONDUCT.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [email protected]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export IMEXROOT=<your-IMEX-install-dir>
2323
## Building sharpy
2424

2525
```bash
26-
python setup.py develop
26+
python -m pip install .
2727
```
2828

2929
If your compiler does not default to a recent (e.g. g++ >= 9) version, try something like `CC=gcc-9 CXX=g++-9 python setup.py develop`

Diff for: Security.md renamed to SECURITY.md

File renamed without changes.

Diff for: examples/black_scholes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ def run(nopt, backend, iterations, datatype):
161161

162162
fini = sync = lambda x=None: None
163163
if comm is not None:
164-
assert comm.Get_size() == 1, "Numpy backend only supports serial execution."
164+
assert (
165+
comm.Get_size() == 1
166+
), "Numpy backend only supports serial execution."
165167
else:
166168
raise ValueError(f'Unknown backend: "{backend}"')
167169

Diff for: examples/shallow_water.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,31 @@ def info(s):
9999

100100
# coordinate arrays
101101
x_t_2d = fromfunction(
102-
lambda i, j: xmin + i * dx + dx / 2, (nx, ny), dtype=dtype, device=device
102+
lambda i, j: xmin + i * dx + dx / 2,
103+
(nx, ny),
104+
dtype=dtype,
105+
device=device,
103106
)
104107
y_t_2d = fromfunction(
105-
lambda i, j: ymin + j * dy + dy / 2, (nx, ny), dtype=dtype, device=device
108+
lambda i, j: ymin + j * dy + dy / 2,
109+
(nx, ny),
110+
dtype=dtype,
111+
device=device,
106112
)
107113
x_u_2d = fromfunction(
108114
lambda i, j: xmin + i * dx, (nx + 1, ny), dtype=dtype, device=device
109115
)
110116
y_u_2d = fromfunction(
111-
lambda i, j: ymin + j * dy + dy / 2, (nx + 1, ny), dtype=dtype, device=device
117+
lambda i, j: ymin + j * dy + dy / 2,
118+
(nx + 1, ny),
119+
dtype=dtype,
120+
device=device,
112121
)
113122
x_v_2d = fromfunction(
114-
lambda i, j: xmin + i * dx + dx / 2, (nx, ny + 1), dtype=dtype, device=device
123+
lambda i, j: xmin + i * dx + dx / 2,
124+
(nx, ny + 1),
125+
dtype=dtype,
126+
device=device,
115127
)
116128
y_v_2d = fromfunction(
117129
lambda i, j: ymin + j * dy, (nx, ny + 1), dtype=dtype, device=device
@@ -189,7 +201,9 @@ def bathymetry(x_t_2d, y_t_2d, lx, ly):
189201
return bath * np.full(T_shape, 1.0, dtype, device=device)
190202

191203
# inital elevation
192-
u0, v0, e0 = exact_solution(0, x_t_2d, y_t_2d, x_u_2d, y_u_2d, x_v_2d, y_v_2d)
204+
u0, v0, e0 = exact_solution(
205+
0, x_t_2d, y_t_2d, x_u_2d, y_u_2d, x_v_2d, y_v_2d
206+
)
193207
e[:, :] = e0
194208
u[:, :] = u0
195209
v[:, :] = v0
@@ -229,10 +243,14 @@ def rhs(u, v, e):
229243
hu[1:-1, :] = 0.5 * (H[:-1, :] + H[1:, :]) * u[1:-1, :]
230244
hv[:, 1:-1] = 0.5 * (H[:, :-1] + H[:, 1:]) * v[:, 1:-1]
231245

232-
dedt = -1.0 * ((hu[1:, :] - hu[:-1, :]) / dx + (hv[:, 1:] - hv[:, :-1]) / dy)
246+
dedt = -1.0 * (
247+
(hu[1:, :] - hu[:-1, :]) / dx + (hv[:, 1:] - hv[:, :-1]) / dy
248+
)
233249

234250
# total depth at F points
235-
H_at_f[1:-1, 1:-1] = 0.25 * (H[1:, 1:] + H[:-1, 1:] + H[1:, :-1] + H[:-1, :-1])
251+
H_at_f[1:-1, 1:-1] = 0.25 * (
252+
H[1:, 1:] + H[:-1, 1:] + H[1:, :-1] + H[:-1, :-1]
253+
)
236254
H_at_f[0, 1:-1] = 0.5 * (H[0, 1:] + H[0, :-1])
237255
H_at_f[-1, 1:-1] = 0.5 * (H[-1, 1:] + H[-1, :-1])
238256
H_at_f[1:-1, 0] = 0.5 * (H[1:, 0] + H[:-1, 0])
@@ -312,6 +330,7 @@ def step(u, v, e, u1, v1, e1, u2, v2, e2):
312330
initial_v = None
313331
initial_e = None
314332
tic = time_mod.perf_counter()
333+
block_tic = 0
315334
for i in range(nt + 1):
316335
sync()
317336
t = i * dt
@@ -356,9 +375,12 @@ def step(u, v, e, u1, v1, e1, u2, v2, e2):
356375
info(
357376
f"{i_export:2d} {i:4d} {t:.3f} elev={elev_max:7.5f} "
358377
f"u={u_max:7.5f} q={q_max:8.5f} dV={diff_v: 6.3e} "
359-
f"PE={total_pe:7.2e} KE={total_ke:7.2e} dE={diff_e: 6.3e}" + tcpu_str
378+
f"PE={total_pe:7.2e} KE={total_ke:7.2e} dE={diff_e: 6.3e}"
379+
+ tcpu_str
360380
)
361-
if not benchmark_mode and (elev_max > 1e3 or not math.isfinite(elev_max)):
381+
if not benchmark_mode and (
382+
elev_max > 1e3 or not math.isfinite(elev_max)
383+
):
362384
raise ValueError(f"Invalid elevation value: {elev_max}")
363385
i_export += 1
364386
next_t_export = i_export * t_export
@@ -372,7 +394,9 @@ def step(u, v, e, u1, v1, e1, u2, v2, e2):
372394
duration = time_mod.perf_counter() - tic
373395
info(f"Duration: {duration:.2f} s")
374396

375-
e_exact = exact_solution(t, x_t_2d, y_t_2d, x_u_2d, y_u_2d, x_v_2d, y_v_2d)[2]
397+
e_exact = exact_solution(t, x_t_2d, y_t_2d, x_u_2d, y_u_2d, x_v_2d, y_v_2d)[
398+
2
399+
]
376400
err2 = (e_exact - e) * (e_exact - e) * dx * dy / lx / ly
377401
err_L2 = math.sqrt(float(np.sum(err2, all_axes)))
378402
info(f"L2 error: {err_L2:7.15e}")

Diff for: examples/stencil-2d.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
import sys
5858

5959
print(
60-
"Python version = ", str(sys.version_info.major) + "." + str(sys.version_info.minor)
60+
"Python version = ",
61+
str(sys.version_info.major) + "." + str(sys.version_info.minor),
6162
)
6263
if sys.version_info >= (33, 3):
6364
from time import process_time as timer
@@ -81,7 +82,8 @@ def main():
8182
if len(sys.argv) < 3:
8283
print("argument count = ", len(sys.argv))
8384
sys.exit(
84-
"Usage: ./stencil <# iterations> <array dimension> [<star/stencil> <radius>]"
85+
"Usage: ./stencil <# iterations> <array dimension>"
86+
" [<star/stencil> <radius>]"
8587
)
8688

8789
iterations = int(sys.argv[1])
@@ -182,7 +184,9 @@ def main():
182184
b = n - r
183185
for s in range(-r, r + 1):
184186
for t in range(-r, r + 1):
185-
B[r:b, r:b] += W[r + t, r + s] * A[r + t : b + t, r + s : b + s]
187+
B[r:b, r:b] += (
188+
W[r + t, r + s] * A[r + t : b + t, r + s : b + s]
189+
)
186190
A[:, :] = A + 1.0
187191

188192
np.sync()
@@ -205,10 +209,17 @@ def main():
205209
if abs(norm - reference_norm) < epsilon:
206210
print("Solution validates")
207211
else:
208-
print("ERROR: L1 norm = ", norm, " Reference L1 norm = ", reference_norm)
212+
print(
213+
"ERROR: L1 norm = ", norm, " Reference L1 norm = ", reference_norm
214+
)
209215
flops = (2 * stencil_size + 1) * active_points
210216
avgtime = stencil_time / iterations
211-
print("Rate (MFlops/s): ", 1.0e-6 * flops / avgtime, " Avg time (s): ", avgtime)
217+
print(
218+
"Rate (MFlops/s): ",
219+
1.0e-6 * flops / avgtime,
220+
" Avg time (s): ",
221+
avgtime,
222+
)
212223

213224

214225
if __name__ == "__main__":

Diff for: examples/wave_equation.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ def info(s):
9999

100100
# coordinate arrays
101101
x_t_2d = fromfunction(
102-
lambda i, j: xmin + i * dx + dx / 2, (nx, ny), dtype=dtype, device=device
102+
lambda i, j: xmin + i * dx + dx / 2,
103+
(nx, ny),
104+
dtype=dtype,
105+
device=device,
103106
)
104107
y_t_2d = fromfunction(
105-
lambda i, j: ymin + j * dy + dy / 2, (nx, ny), dtype=dtype, device=device
108+
lambda i, j: ymin + j * dy + dy / 2,
109+
(nx, ny),
110+
dtype=dtype,
111+
device=device,
106112
)
107113

108114
T_shape = (nx, ny)
@@ -207,6 +213,7 @@ def step(u, v, e, u1, v1, e1, u2, v2, e2):
207213
next_t_export = 0
208214
initial_v = None
209215
tic = time_mod.perf_counter()
216+
block_tic = 0
210217
for i in range(nt + 1):
211218
sync()
212219
t = i * dt
@@ -233,7 +240,9 @@ def step(u, v, e, u1, v1, e1, u2, v2, e2):
233240
f"{i_export:2d} {i:4d} {t:.3f} elev={elev_max:7.5f} "
234241
f"u={u_max:7.5f} dV={diff_v: 6.3e} " + tcpu_str
235242
)
236-
if not benchmark_mode and (elev_max > 1e3 or not math.isfinite(elev_max)):
243+
if not benchmark_mode and (
244+
elev_max > 1e3 or not math.isfinite(elev_max)
245+
):
237246
raise ValueError(f"Invalid elevation value: {elev_max}")
238247
i_export += 1
239248
next_t_export = i_export * t_export

0 commit comments

Comments
 (0)