Skip to content

Commit 55d1854

Browse files
authored
Merge pull request #163 from zdaq12/pre-commit
adding pre-commit hooks. closes #6
2 parents c1fd203 + c3cdefe commit 55d1854

22 files changed

+412
-287
lines changed

.github/workflows/pre-commit.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: pre-commit-hooks
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- name: Set up Python 3.10
11+
uses: actions/setup-python@v1
12+
with:
13+
python-version: "3.10"
14+
- name: Linting
15+
run: |
16+
pip install pre-commit
17+
pre-commit clean
18+
pre-commit autoupdate
19+
pre-commit run --all-files

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
files: '.py'
2+
exclude: '.git'
3+
default_stages: [commit]
4+
5+
repos:
6+
- repo: https://github.com/psf/black
7+
rev: 22.6.0
8+
hooks:
9+
- id: black
10+
11+
- repo: https://github.com/timothycrosley/isort
12+
rev: 5.10.1
13+
hooks:
14+
- id: isort
15+
16+
- repo: https://github.com/pre-commit/pre-commit-hooks
17+
rev: v4.3.0
18+
hooks:
19+
- id: trailing-whitespace
20+
- id: end-of-file-fixer
21+
- id: debug-statements

PyPartMC/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# pylint: disable=invalid-name,wrong-import-position
22
import os
3+
from collections import namedtuple
34
from contextlib import contextmanager
45
from pathlib import Path
5-
from collections import namedtuple
6+
67

78
# https://github.com/diegoferigo/cmake-build-extension/blob/master/src/cmake_build_extension/__init__.py
89
@contextmanager
@@ -11,8 +12,8 @@ def __build_extension_env():
1112
# https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew
1213
if hasattr(os, "add_dll_directory"):
1314
basepath = os.path.dirname(os.path.abspath(__file__))
14-
dllspath = os.path.join(basepath, '..')
15-
os.environ['PATH'] = dllspath + os.pathsep + os.environ['PATH']
15+
dllspath = os.path.join(basepath, "..")
16+
os.environ["PATH"] = dllspath + os.pathsep + os.environ["PATH"]
1617
for path in os.environ.get("PATH", "").split(os.pathsep):
1718
if path and Path(path).is_absolute() and Path(path).is_dir():
1819
cookies.append(os.add_dll_directory(path))
@@ -22,25 +23,24 @@ def __build_extension_env():
2223
for cookie in cookies:
2324
cookie.close()
2425

26+
2527
# TODO #113: 2 x loop over prefixes and units
26-
si = namedtuple("SI", (
27-
"m", "cm", "um", "nm",
28-
"kg", "g",
29-
"s",
30-
"K",
31-
"Pa", "hPa",
32-
"mol"
33-
))(
34-
m=1., cm=.01, um=1e-6, nm=1e-9,
35-
kg=1., g=1e-3,
36-
s=1.,
37-
K=1.,
38-
Pa=1., hPa=100.,
39-
mol=1.
28+
si = namedtuple("SI", ("m", "cm", "um", "nm", "kg", "g", "s", "K", "Pa", "hPa", "mol"))(
29+
m=1.0,
30+
cm=0.01,
31+
um=1e-6,
32+
nm=1e-9,
33+
kg=1.0,
34+
g=1e-3,
35+
s=1.0,
36+
K=1.0,
37+
Pa=1.0,
38+
hPa=100.0,
39+
mol=1.0,
4040
)
4141
""" TODO #113 """
4242

4343
with __build_extension_env():
44+
import _PyPartMC
4445
from _PyPartMC import *
4546
from _PyPartMC import __all__, __version__
46-
import _PyPartMC

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ warning: no files found matching 'gitmodules/...
9696
```
9797
Since git clone was done without recursive option, try: `git submodule update --init`
9898

99+
## Notes for developers
99100
#### How to debug
100101
```sh
101102
git clone --recursive git+https://github.com/open-atmos/PyPartMC.git
@@ -104,6 +105,8 @@ DEBUG=1 VERBOSE=1 pip --verbose install -e .
104105
gdb python
105106
(gdb) run -m pytest -s -vv -We -p no:unraisableexception tests
106107
```
108+
#### Pre-commit hooks
109+
PyPartMC codebase benefits from Pylint, Black and isort code analysis (which are all part of the CI workflows where we also use pre-commit hooks. The pre-commit hooks can be run locally, and then the resultant changes need to be staged before committing. To set up the hooks locally, install pre-commit via `pip install pre-commit` and set up the git hooks via `pre-commit install` (this needs to be done every time you clone the project). To run all pre-commit hooks, run `pre-commit run --all-files`. The `.pre-commit-config.yaml` file can be modified in case new hooks are to be added or existing ones need to be altered.
107110

108111
## Credits
109112

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import subprocess
1010
import sys
1111
from pathlib import Path
12-
from setuptools import Extension, setup, find_packages
12+
13+
from setuptools import Extension, find_packages, setup
1314
from setuptools.command.build_ext import build_ext
1415

1516
# Convert distutils Windows platform specifiers to CMake -A arguments
@@ -140,11 +141,11 @@ def build_extension(self, ext): # pylint: disable=too-many-branches
140141
zip_safe=False,
141142
python_requires=">=3.7",
142143
setup_requires=["setuptools_scm"],
143-
install_requires=['numpy'],
144+
install_requires=["numpy"],
144145
license="GPL-3.0",
145146
project_urls={
146147
"Tracker": "https://github.com/open-atmos/PyPartMC/issues",
147148
"Documentation": "https://open-atmos.github.io/PyPartMC",
148149
"Source": "https://github.com/open-atmos/PyPartMC/",
149-
}
150+
},
150151
)

src/pypartmc.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ PYBIND11_MODULE(_PyPartMC, m) {
4848
py::class_<AeroData>(m, "AeroData",
4949
R"pbdoc(
5050
Aerosol material properties and associated data.
51-
51+
5252
The data in this structure is constant, as it represents physical
5353
quantities that cannot change over time.
54-
54+
5555
Each aerosol species is identified by an index <tt>i =
5656
1,...,aero_data_n_spec(aero_data)</tt>. Then \c name(i) is the name of
5757
that species, \c density(i) is its density, etc. The ordering of the
5858
species is arbitrary and should not be relied upon (currently it is the
5959
order in the species data file). The only exception is that it is
6060
possible to find out which species is water from the \c i_water
6161
variable.
62-
62+
6363
The names of the aerosol species are not important to PartMC, as
6464
only the material properties are used. The names are used for
6565
input and output, and also for communication with MOSAIC. For the
@@ -86,7 +86,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
8686
py::class_<AeroParticle>(m, "AeroParticle",
8787
R"pbdoc(
8888
Single aerosol particle data structure.
89-
89+
9090
The \c vol array stores the total volumes of the different
9191
species that make up the particle. This array must have length
9292
equal to aero_data%%n_spec, so that \c vol(i) is the volume (in
@@ -122,12 +122,12 @@ PYBIND11_MODULE(_PyPartMC, m) {
122122
py::class_<AeroState>(m, "AeroState",
123123
R"pbdoc(
124124
The current collection of aerosol particles.
125-
125+
126126
The particles in \c aero_state_t are stored in a single flat
127127
array (the \c apa data member), with a sorting into size bins and
128128
weight groups/classes possibly stored in the \c aero_sorted data
129129
member (if \c valid_sort is true).
130-
130+
131131
Every time we remove particles we keep track of the particle ID
132132
and the action performed in the aero_info_array_t structure. This
133133
is typically cleared each time we output data to disk.
@@ -160,7 +160,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
160160
py::class_<GasData>(m, "GasData",
161161
R"pbdoc(
162162
Constant gas data.
163-
163+
164164
Each gas species is identified by an integer \c i between 1 and
165165
\c gas_data_n_spec(gas_data). Species \c i has name \c gas_data%%name(i).
166166
The variable gas data describing the current mixing ratios is stored
@@ -178,7 +178,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
178178
"EnvState",
179179
R"pbdoc(
180180
Current environment state.
181-
181+
182182
All quantities are instantaneous, describing the state at a
183183
particular instant of time. Constant data and other data not
184184
associated with the current environment state is stored in
@@ -218,7 +218,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
218218
"Scenario",
219219
R"pbdoc(
220220
This is everything needed to drive the scenario being simulated.
221-
221+
222222
The temperature, pressure, emissions and background states are profiles
223223
prescribed as functions of time by giving a number of times and
224224
the corresponding data. Simple data such as temperature and pressure is
@@ -244,11 +244,11 @@ PYBIND11_MODULE(_PyPartMC, m) {
244244
"GasState",
245245
R"pbdoc(
246246
Current state of the gas mixing ratios in the system.
247-
247+
248248
The gas species are defined by the gas_data_t structure, so that
249249
\c gas_state%%mix_rat(i) is the current mixing ratio of the gas
250250
with name \c gas_data%%name(i), etc.
251-
251+
252252
By convention, if gas_state_is_allocated() return \c .false.,
253253
then the gas_state is treated as zero for all operations on
254254
it. This will be the case for new \c gas_state_t structures.
@@ -265,7 +265,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
265265
"returns a string with JSON representation of the object")
266266
;
267267

268-
py::class_<RunPartOpt>(m,
268+
py::class_<RunPartOpt>(m,
269269
"RunPartOpt",
270270
"Options controlling the execution of run_part()."
271271
)

0 commit comments

Comments
 (0)