From 01ad5e21ef573868ded73c574d9fdbf72f5b8066 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Wed, 27 Nov 2024 12:23:55 +0100 Subject: [PATCH 01/12] added tests for show_anim() and show_plot() --- test_notebooks.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test_notebooks.py b/test_notebooks.py index 70baf99..374e9f6 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -182,3 +182,33 @@ def test_cell_contains_output(notebook_filename): for cell in nb.cells: if cell.cell_type == "code" and cell.source != "": assert cell.execution_count is not None + +def test_show_plot_used_instead_of_matplotlib(notebook): + """checks if plotting is done with open_atmos_jupyter_utils show_plot()""" + with open(notebook_filename, encoding="utf8") as fp: + nb = nbformat.read(fp, nbformat.NO_CONVERT) + matplot_used = False + show_plot_used = False + for cell in nb.cells: + if cell.cell_type == "code": + if "matplotlib" or "pyplot" in cell.source: + matplot_used = True + if "show_plot()" in cell.source: + show_plot_used = True + if matplot_used and not show_plot_used: + raise AssertionError("if using matplotlib, please use open_atmos_jupyter_utils.show_plot()") + +def test_show_anim_used_instead_of_matplotlib(notebook): + """checks if animation generation is done with open_atmos_jupyter_utils show_anim()""" + with open(notebook_filename, encoding="utf8") as fp: + nb = nbformat.read(fp, nbformat.NO_CONVERT) + matplot_used = False + show_anim_used = False + for cell in nb.cells: + if cell.cell_type == "code": + if "funcAnimation" in cell.source: + matplot_used = True + if "show_anim()" in cell.source: + show_anim_used = True + if matplot_used and not show_anim_used: + raise AssertionError("if using matplotlib for animations, please use open_atmos_jupyter_utils.show_anim()") From 4c9493b0df26bb4a246fca2a73fd788c07213403 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Wed, 27 Nov 2024 14:54:13 +0100 Subject: [PATCH 02/12] fixed formatting errors --- test_notebooks.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test_notebooks.py b/test_notebooks.py index 374e9f6..22f70df 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -183,7 +183,8 @@ def test_cell_contains_output(notebook_filename): if cell.cell_type == "code" and cell.source != "": assert cell.execution_count is not None -def test_show_plot_used_instead_of_matplotlib(notebook): + +def test_show_plot_used_instead_of_matplotlib(notebook_filename): """checks if plotting is done with open_atmos_jupyter_utils show_plot()""" with open(notebook_filename, encoding="utf8") as fp: nb = nbformat.read(fp, nbformat.NO_CONVERT) @@ -191,14 +192,17 @@ def test_show_plot_used_instead_of_matplotlib(notebook): show_plot_used = False for cell in nb.cells: if cell.cell_type == "code": - if "matplotlib" or "pyplot" in cell.source: + if "matplotlib" in cell.source or "pyplot" in cell.source: matplot_used = True if "show_plot()" in cell.source: show_plot_used = True if matplot_used and not show_plot_used: - raise AssertionError("if using matplotlib, please use open_atmos_jupyter_utils.show_plot()") + raise AssertionError( + "if using matplotlib, please use open_atmos_jupyter_utils.show_plot()" + ) -def test_show_anim_used_instead_of_matplotlib(notebook): + +def test_show_anim_used_instead_of_matplotlib(notebook_filename): """checks if animation generation is done with open_atmos_jupyter_utils show_anim()""" with open(notebook_filename, encoding="utf8") as fp: nb = nbformat.read(fp, nbformat.NO_CONVERT) @@ -211,4 +215,6 @@ def test_show_anim_used_instead_of_matplotlib(notebook): if "show_anim()" in cell.source: show_anim_used = True if matplot_used and not show_anim_used: - raise AssertionError("if using matplotlib for animations, please use open_atmos_jupyter_utils.show_anim()") + raise AssertionError( + "if using matplotlib for animations, please use open_atmos_jupyter_utils.show_anim()" + ) From 86cf40c4bdd1dcc735ad30dcc3c2bc053defdc7d Mon Sep 17 00:00:00 2001 From: sfonxu Date: Thu, 28 Nov 2024 23:01:55 +0100 Subject: [PATCH 03/12] fixed string formatting --- test_notebooks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test_notebooks.py b/test_notebooks.py index 22f70df..94ec904 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -216,5 +216,6 @@ def test_show_anim_used_instead_of_matplotlib(notebook_filename): show_anim_used = True if matplot_used and not show_anim_used: raise AssertionError( - "if using matplotlib for animations, please use open_atmos_jupyter_utils.show_anim()" + """if using matplotlib for animations, + please use open_atmos_jupyter_utils.show_anim()""" ) From 0c374410ad0dc7b1cd5e35bf92acd10b1394e127 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Tue, 18 Mar 2025 16:44:25 +0100 Subject: [PATCH 04/12] changes to conditions in both tests --- test_notebooks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_notebooks.py b/test_notebooks.py index 94ec904..b8be580 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -192,7 +192,7 @@ def test_show_plot_used_instead_of_matplotlib(notebook_filename): show_plot_used = False for cell in nb.cells: if cell.cell_type == "code": - if "matplotlib" in cell.source or "pyplot" in cell.source: + if "pyplot.show()" in cell.source or "plt.show()" in cell.source or ("show()" and "from matplotlib import pyplot" in cell.source): matplot_used = True if "show_plot()" in cell.source: show_plot_used = True @@ -210,7 +210,7 @@ def test_show_anim_used_instead_of_matplotlib(notebook_filename): show_anim_used = False for cell in nb.cells: if cell.cell_type == "code": - if "funcAnimation" in cell.source: + if "funcAnimation" in cell.source or "matplotlib.animation" in cell.source or "from matplotlib import animation" in cell.source: matplot_used = True if "show_anim()" in cell.source: show_anim_used = True From 7cec63d48898fba3bec1b8c2ba01c21c17de1e82 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Thu, 15 May 2025 14:08:30 +0200 Subject: [PATCH 05/12] change macos and python versions in checks --- .github/workflows/checks.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 7be39a2..9d7d9c9 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -19,7 +19,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + - uses: actions/setup-python@v5 - run: pip install pylint - run: pip install -r requirements.txt - run: pylint --unsafe-load-any-extension=y --disable=fixme $(git ls-files '*.py') @@ -39,12 +39,12 @@ jobs: needs: [pylint, precommit] strategy: matrix: - platform: [ubuntu-latest, macos-12, windows-latest] - python-version: ["3.8", "3.11"] + platform: [ubuntu-latest, macos-14, windows-latest] + python-version: ["3.9", "3.13"] runs-on: ${{ matrix.platform }} steps: - uses: actions/checkout@v2 - - uses: actions/setup-python@v1 + - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - run: pip install -r requirements.txt From e91c233c7aeb6e7b9364fd519dd857c2df65dbe9 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Thu, 15 May 2025 14:11:05 +0200 Subject: [PATCH 06/12] linting fixes --- test_notebooks.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test_notebooks.py b/test_notebooks.py index b8be580..5b0e098 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -192,7 +192,11 @@ def test_show_plot_used_instead_of_matplotlib(notebook_filename): show_plot_used = False for cell in nb.cells: if cell.cell_type == "code": - if "pyplot.show()" in cell.source or "plt.show()" in cell.source or ("show()" and "from matplotlib import pyplot" in cell.source): + if ( + "pyplot.show()" in cell.source + or "plt.show()" in cell.source + or ("show()" and "from matplotlib import pyplot" in cell.source) + ): matplot_used = True if "show_plot()" in cell.source: show_plot_used = True @@ -210,7 +214,11 @@ def test_show_anim_used_instead_of_matplotlib(notebook_filename): show_anim_used = False for cell in nb.cells: if cell.cell_type == "code": - if "funcAnimation" in cell.source or "matplotlib.animation" in cell.source or "from matplotlib import animation" in cell.source: + if ( + "funcAnimation" in cell.source + or "matplotlib.animation" in cell.source + or "from matplotlib import animation" in cell.source + ): matplot_used = True if "show_anim()" in cell.source: show_anim_used = True From 20954cd260d0b01a6886c9953b061df513c7505d Mon Sep 17 00:00:00 2001 From: sfonxu Date: Thu, 15 May 2025 14:12:46 +0200 Subject: [PATCH 07/12] fix condition --- test_notebooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_notebooks.py b/test_notebooks.py index 5b0e098..ecd2733 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -195,7 +195,7 @@ def test_show_plot_used_instead_of_matplotlib(notebook_filename): if ( "pyplot.show()" in cell.source or "plt.show()" in cell.source - or ("show()" and "from matplotlib import pyplot" in cell.source) + or "from matplotlib import pyplot" in cell.source ): matplot_used = True if "show_plot()" in cell.source: From 5b096f907bf400ae6e8c16fab6736cd8819f6022 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Thu, 15 May 2025 14:14:52 +0200 Subject: [PATCH 08/12] black fixes --- .pre-commit-config.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 43cfd35..8155bbd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,20 +1,20 @@ files: '.py' exclude: '.git' -default_stages: [commit] +default_stages: [pre-commit] repos: - repo: https://github.com/psf/black - rev: 24.3.0 + rev: 25.1.0 hooks: - id: black - repo: https://github.com/timothycrosley/isort - rev: 5.13.2 + rev: 6.0.1 hooks: - id: isort - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v5.0.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer From 16a84c729ae5623d09b39735182a4094fe0d07f2 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Thu, 15 May 2025 14:19:02 +0200 Subject: [PATCH 09/12] updated pre-commit fixes --- test_notebooks.py | 2 +- test_todos_annotated.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test_notebooks.py b/test_notebooks.py index ecd2733..ba28eb4 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -1,4 +1,4 @@ -""" executes all Jupyter notebooks tracked by git """ +"""executes all Jupyter notebooks tracked by git""" # pylint: disable=wrong-import-position # https://bugs.python.org/issue37373 diff --git a/test_todos_annotated.py b/test_todos_annotated.py index ba247c1..bfd2684 100644 --- a/test_todos_annotated.py +++ b/test_todos_annotated.py @@ -1,5 +1,5 @@ -""" utilities to ensure all TO-DO comments in the code are annotated - with an id of an open GitHub issue """ +"""utilities to ensure all TO-DO comments in the code are annotated +with an id of an open GitHub issue""" import os import re From 831e85ef2f14b0613696221fafb50775e0f32b16 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Thu, 15 May 2025 14:54:17 +0200 Subject: [PATCH 10/12] change test logic for show_plot() --- test_notebooks.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/test_notebooks.py b/test_notebooks.py index ba28eb4..16352dc 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -188,22 +188,14 @@ def test_show_plot_used_instead_of_matplotlib(notebook_filename): """checks if plotting is done with open_atmos_jupyter_utils show_plot()""" with open(notebook_filename, encoding="utf8") as fp: nb = nbformat.read(fp, nbformat.NO_CONVERT) - matplot_used = False - show_plot_used = False for cell in nb.cells: - if cell.cell_type == "code": - if ( - "pyplot.show()" in cell.source - or "plt.show()" in cell.source - or "from matplotlib import pyplot" in cell.source - ): - matplot_used = True - if "show_plot()" in cell.source: - show_plot_used = True - if matplot_used and not show_plot_used: - raise AssertionError( - "if using matplotlib, please use open_atmos_jupyter_utils.show_plot()" - ) + if cell.cell_type != "code": + continue + if cell.outputs[0].data.starts_with("image/"): + assert ( + cell.source[-1].starts_with("show_plot"), + "if using matplotlib, please use open_atmos_jupyter_utils.show_plot()", + ) def test_show_anim_used_instead_of_matplotlib(notebook_filename): @@ -220,7 +212,7 @@ def test_show_anim_used_instead_of_matplotlib(notebook_filename): or "from matplotlib import animation" in cell.source ): matplot_used = True - if "show_anim()" in cell.source: + if "show_anim(" in cell.source: show_anim_used = True if matplot_used and not show_anim_used: raise AssertionError( From 1665d86475bb5e65ddf84f18fd00d09029005bc0 Mon Sep 17 00:00:00 2001 From: sfonxu Date: Sat, 17 May 2025 10:24:18 +0200 Subject: [PATCH 11/12] change assert to if-raise pair to allow for an error message --- test_notebooks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test_notebooks.py b/test_notebooks.py index 452361b..fcf54cd 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -192,10 +192,10 @@ def test_show_plot_used_instead_of_matplotlib(notebook_filename): if cell.cell_type != "code": continue if cell.outputs[0].data.starts_with("image/"): - assert ( - cell.source[-1].starts_with("show_plot"), - "if using matplotlib, please use open_atmos_jupyter_utils.show_plot()", - ) + if not cell.source[-1].starts_with("show_plot"): + raise AssertionError( + "if using matplotlib, please use open_atmos_jupyter_utils.show_plot()" + ) def test_show_anim_used_instead_of_matplotlib(notebook_filename): From 71800797694d840a7a3bbdf05a1164de393dbcdc Mon Sep 17 00:00:00 2001 From: sfonxu Date: Sat, 17 May 2025 10:30:44 +0200 Subject: [PATCH 12/12] add a condition to check if output is present --- test_notebooks.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test_notebooks.py b/test_notebooks.py index fcf54cd..f4aee0f 100644 --- a/test_notebooks.py +++ b/test_notebooks.py @@ -191,11 +191,12 @@ def test_show_plot_used_instead_of_matplotlib(notebook_filename): for cell in nb.cells: if cell.cell_type != "code": continue - if cell.outputs[0].data.starts_with("image/"): - if not cell.source[-1].starts_with("show_plot"): - raise AssertionError( - "if using matplotlib, please use open_atmos_jupyter_utils.show_plot()" - ) + if len(cell.outputs) > 0: + if cell.outputs[0].data.starts_with("image/"): + if not cell.source[-1].starts_with("show_plot"): + raise AssertionError( + "if using matplotlib, please use open_atmos_jupyter_utils.show_plot()" + ) def test_show_anim_used_instead_of_matplotlib(notebook_filename):