Skip to content

Commit

Permalink
Enable pre commit and fix code style once (#29)
Browse files Browse the repository at this point in the history
* Enable linting

* Make sure we run on main

* Disable interrogate, codespell, mypy

* Satisfy linter

* Fix isort
  • Loading branch information
willirath committed Jun 9, 2022
1 parent 696ebdf commit eddcf62
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 122 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/linting.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: linting with pre-commit

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: "3.8"
- uses: pre-commit/action@v2.0.0
16 changes: 0 additions & 16 deletions .github/workflows/optional/linting.yaml

This file was deleted.

127 changes: 74 additions & 53 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,61 +1,82 @@
repos:
# - repo: https://github.com/econchick/interrogate
# rev: 1.2.0
# hooks:
# - id: interrogate
# exclude: ^(docs|setup.py|tests)
# args: [--config=pyproject.toml]

- repo: https://github.com/econchick/interrogate
rev: 1.2.0
hooks:
- id: interrogate
exclude: ^(docs|setup.py|tests)
args: [--config=pyproject.toml]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
hooks:
- id: trailing-whitespace
exclude: tests/data
- id: check-ast
- id: debug-statements
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-added-large-files
- id: requirements-txt-fixer
- id: file-contents-sorter
files: requirements-dev.txt

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
hooks:
- id: trailing-whitespace
exclude: tests/data
- id: check-ast
- id: debug-statements
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-added-large-files
- id: requirements-txt-fixer
- id: file-contents-sorter
files: requirements-dev.txt
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
exclude: docs/source/conf.py
args:
[
--max-line-length=105,
--ignore=E203,
E501,
W503,
--select=select=C,
E,
F,
W,
B,
B950,
]

- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
exclude: docs/source/conf.py
args: [--max-line-length=105, --ignore=E203,E501,W503, --select=select=C,E,F,W,B,B950]
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
- id: isort
additional_dependencies: [toml]
args:
[
--project=gcm_filters,
--multi-line=3,
--lines-after-imports=2,
--lines-between-types=1,
--trailing-comma,
--force-grid-wrap=0,
--use-parentheses,
--line-width=88,
]

- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
- id: isort
additional_dependencies: [toml]
args: [--project=gcm_filters, --multi-line=3, --lines-after-imports=2, --lines-between-types=1, --trailing-comma, --force-grid-wrap=0, --use-parentheses, --line-width=88]
- repo: https://github.com/asottile/seed-isort-config
rev: v2.1.1
hooks:
- id: seed-isort-config

- repo: https://github.com/asottile/seed-isort-config
rev: v2.1.1
hooks:
- id: seed-isort-config
- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
language_version: python3

- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
language_version: python3
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v0.770
# hooks:
# - id: mypy
# exclude: docs/source/conf.py
# args: [--ignore-missing-imports]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.770
hooks:
- id: mypy
exclude: docs/source/conf.py
args: [--ignore-missing-imports]

- repo: https://github.com/codespell-project/codespell
rev: v1.16.0
hooks:
- id: codespell
args:
- --quiet-level=2
# - repo: https://github.com/codespell-project/codespell
# rev: v1.16.0
# hooks:
# - id: codespell
# args:
# - --quiet-level=2
1 change: 0 additions & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1 change: 1 addition & 0 deletions climate_index_collection/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .cli import run


if __name__ == "__main__":
run()
3 changes: 2 additions & 1 deletion climate_index_collection/data_loading.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import xarray as xr
from pathlib import Path

import xarray as xr


def find_data_files(data_path="data/test_data/", data_source_name="FOCI"):
"""Find all files for given data source.
Expand Down
6 changes: 3 additions & 3 deletions climate_index_collection/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

def southern_annular_mode(data_set, slp_name="sea-level-pressure"):
"""Calculate the southern annular mode index.
Parameters
----------
data_set: xarray.DataSet
Dataset containing a SLP field.
slp_name: str
Name of the Sea-Level Pressure field. Defaults to "sea-level-pressure".
Returns
-------
xarray.DataArray
Time series containing the SAM index.
"""
slp = data_set[slp_name]

Expand Down
26 changes: 13 additions & 13 deletions climate_index_collection/reductions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def mean_unweighted(dobj, dim=None):
"""Calculate an unweighted mean for one or more dimensions.
Parameters
----------
dobj: xarray.Dataset or xarray.DataArray
Expand All @@ -12,14 +12,14 @@ def mean_unweighted(dobj, dim=None):
-------
xarray.Dataset or xarray.DataArray
Averaged data. Has the same variable name(s) as dobj.
"""
return dobj.mean(dim)


def mean_weighted(dobj, weights=None, dim=None):
"""Calculate a weighted mean.
Parameters
----------
dobj: xarray.Dataset or xarray.DataArray
Expand All @@ -33,7 +33,7 @@ def mean_weighted(dobj, weights=None, dim=None):
-------
xarray.Dataset or xarray.DataArray
Averaged data. Has the same variable name(s) as dobj.
"""
numerator = (dobj * weights).sum(dim)
denominator = weights.where(~dobj.isnull()).sum(dim)
Expand All @@ -46,7 +46,7 @@ def variance_weighted(dobj, weights=None, dim=None):
This uses a weighted mean <.>_w and defines variance as
var[a] = <a**2>_w - (<a>_w)**2.
Parameters
----------
dobj: xarray.Dataset or xarray.DataArray
Expand All @@ -60,9 +60,9 @@ def variance_weighted(dobj, weights=None, dim=None):
-------
xarray.Dataset or xarray.DataArray
Variance data. Has the same variable name(s) as dobj.
"""
mean_of_squares = mean_weighted(dobj ** 2, weights=weights, dim=dim)
mean_of_squares = mean_weighted(dobj**2, weights=weights, dim=dim)
square_of_means = mean_weighted(dobj, weights=weights, dim=dim) ** 2
variance = mean_of_squares - square_of_means
return variance
Expand All @@ -82,7 +82,7 @@ def variance_unweighted(dobj, dim=None):
-------
xarray.Dataset or xarray.DataArray
Variance data. Has the same variable name(s) as dobj.
"""
return dobj.var(dim)

Expand All @@ -92,7 +92,7 @@ def stddev_weighted(dobj, weights=None, dim=None):
This uses a weighted mean <.>_w, defines variance as
var[a] = <a**2>_w - (<a>_w)**2, and std dev as var***0.5.
Parameters
----------
dobj: xarray.Dataset or xarray.DataArray
Expand All @@ -106,16 +106,16 @@ def stddev_weighted(dobj, weights=None, dim=None):
-------
xarray.Dataset or xarray.DataArray
Stddev data. Has the same variable name(s) as dobj.
"""
variance = variance_weighted(dobj, weights=weights, dim=dim)
std_dev = variance ** 0.5
std_dev = variance**0.5
return std_dev


def stddev_unweighted(dobj, dim=None):
"""Calculate an unweighted stddev.
Parameters
----------
dobj: xarray.Dataset or xarray.DataArray
Expand All @@ -127,6 +127,6 @@ def stddev_unweighted(dobj, dim=None):
-------
xarray.Dataset or xarray.DataArray
Stddev data. Has the same variable name(s) as dobj.
"""
return dobj.std(dim)
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ help:
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
.. automodule:: climate_index_collection.dummy
:members:
:undoc-members:
:show-inheritance:
:show-inheritance:
17 changes: 9 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
import pathlib
import sys

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
# see https://pypi.org/project/setuptools-scm/ for details
from pkg_resources import get_distribution


print("python exec:", sys.executable)
print("sys.path:", sys.path)
Expand All @@ -29,14 +35,9 @@
copyright = "2021, Marco Landt-Hayen"
author = "Marco Landt-Hayen"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
# see https://pypi.org/project/setuptools-scm/ for details
from pkg_resources import get_distribution
release = get_distribution('climate_index_collection').version
release = get_distribution("climate_index_collection").version
# for example take major/minor
version = '.'.join(release.split('.')[:2])
version = ".".join(release.split(".")[:2])

# -- General configuration ---------------------------------------------------

Expand Down Expand Up @@ -84,4 +85,4 @@

# -- nbsphinx specific options ----------------------------------------------
# this allows notebooks to be run even if they produce errors.
nbsphinx_allow_errors = True
nbsphinx_allow_errors = True
2 changes: 1 addition & 1 deletion docs/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ dependencies:
- jupyter_client
- sphinx_pangeo_theme
- sphinx-copybutton
- sphinxcontrib-srclinks
- sphinxcontrib-srclinks
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Indices and tables
* :ref:`modindex`
* :ref:`search`

.. _reStructuredText: https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html
.. _reStructuredText: https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html
2 changes: 1 addition & 1 deletion docs/new_section.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Some new exciting Section
=========================
=========================
2 changes: 1 addition & 1 deletion docs/new_section_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}

0 comments on commit eddcf62

Please sign in to comment.