diff --git a/examples/trajectory_io.py b/examples/trajectory_io.py index 19481be7..5acee84d 100644 --- a/examples/trajectory_io.py +++ b/examples/trajectory_io.py @@ -18,12 +18,11 @@ eq = pde.PDE({"s": "-0.1 * s", "v": "-v"}) # get a temporary file to write data to -path = NamedTemporaryFile(suffix=".hdf5") +with NamedTemporaryFile(suffix=".hdf5") as path: + # run a simulation and write the results + writer = pde.FileStorage(path.name, write_mode="truncate") + eq.solve(state, t_range=32, dt=0.01, tracker=writer.tracker(1)) -# run a simulation and write the results -writer = pde.FileStorage(path.name, write_mode="truncate") -eq.solve(state, t_range=32, dt=0.01, tracker=writer.tracker(1)) - -# read the simulation back in again -reader = pde.FileStorage(path.name, write_mode="read_only") -pde.plot_kymographs(reader) + # read the simulation back in again + reader = pde.FileStorage(path.name, write_mode="read_only") + pde.plot_kymographs(reader) diff --git a/pde/tools/docstrings.py b/pde/tools/docstrings.py index eba7716b..95114017 100644 --- a/pde/tools/docstrings.py +++ b/pde/tools/docstrings.py @@ -15,7 +15,7 @@ import re import textwrap from functools import partial -from typing import TypeVar +from typing import Callable, TypeVar DOCSTRING_REPLACEMENTS = { # description of function arguments @@ -121,7 +121,7 @@ def get_text_block(identifier: str) -> str: return "".join(textwrap.dedent(raw_text)) -TFunc = TypeVar("TFunc") +TFunc = TypeVar("TFunc", bound=Callable) def replace_in_docstring( diff --git a/tests/_notebooks/Test PlotTracker for different backend.ipynb b/tests/_notebooks/Test PlotTracker for different backend.ipynb index a615e019..d68d217d 100644 --- a/tests/_notebooks/Test PlotTracker for different backend.ipynb +++ b/tests/_notebooks/Test PlotTracker for different backend.ipynb @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -78,18 +78,18 @@ ], "source": [ "eq = pde.DiffusionPDE()\n", - "movie_file = tempfile.NamedTemporaryFile(delete=False, suffix=\".mov\").name\n", - "plot_tracker = pde.PlotTracker(\n", - " 0.1, title=\"Visible – {time:.2g}\", show=True, movie=movie_file\n", - ")\n", - "_, info = eq.solve(\n", - " field,\n", - " t_range=2,\n", - " dt=0.1,\n", - " backend=\"numpy\",\n", - " ret_info=True,\n", - " tracker=[\"progress\", plot_tracker],\n", - ")" + "with tempfile.NamedTemporaryFile(delete=False, suffix=\".mov\") as movie_file:\n", + " plot_tracker = pde.PlotTracker(\n", + " 0.1, title=\"Visible – {time:.2g}\", show=True, movie=movie_file.name\n", + " )\n", + " _, info = eq.solve(\n", + " field,\n", + " t_range=2,\n", + " dt=0.1,\n", + " backend=\"numpy\",\n", + " ret_info=True,\n", + " tracker=[\"progress\", plot_tracker],\n", + " )" ] }, { @@ -144,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -164,18 +164,18 @@ ], "source": [ "eq = pde.DiffusionPDE()\n", - "movie_file = tempfile.NamedTemporaryFile(delete=False, suffix=\".mov\").name\n", - "plot_tracker = pde.PlotTracker(\n", - " 0.1, title=\"Hidden – {time:.2g}\", show=False, movie=movie_file\n", - ")\n", - "_, info = eq.solve(\n", - " field,\n", - " t_range=2,\n", - " dt=0.1,\n", - " backend=\"numpy\",\n", - " ret_info=True,\n", - " tracker=[\"progress\", plot_tracker],\n", - ")" + "with tempfile.NamedTemporaryFile(delete=False, suffix=\".mov\") as movie_file:\n", + " plot_tracker = pde.PlotTracker(\n", + " 0.1, title=\"Hidden – {time:.2g}\", show=False, movie=movie_file.name\n", + " )\n", + " _, info = eq.solve(\n", + " field,\n", + " t_range=2,\n", + " dt=0.1,\n", + " backend=\"numpy\",\n", + " ret_info=True,\n", + " tracker=[\"progress\", plot_tracker],\n", + " )" ] }, { @@ -236,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -263,22 +263,22 @@ ], "source": [ "eq = pde.PDE({\"a\": \"laplace(a)\", \"b\": \"laplace(b)\"})\n", - "movie_file = tempfile.NamedTemporaryFile(delete=False, suffix=\".mov\").name\n", - "plot_tracker = pde.PlotTracker(\n", - " 0.5,\n", - " title=\"Visible Plot Collection – {time:.2g}\",\n", - " show=True,\n", - " movie=movie_file,\n", - " plot_args={\"colorbar\": True},\n", - ")\n", - "_, info = eq.solve(\n", - " fc,\n", - " t_range=2,\n", - " dt=0.1,\n", - " backend=\"numpy\",\n", - " ret_info=True,\n", - " tracker=[\"progress\", plot_tracker],\n", - ")" + "with tempfile.NamedTemporaryFile(delete=False, suffix=\".mov\") as movie_file:\n", + " plot_tracker = pde.PlotTracker(\n", + " 0.5,\n", + " title=\"Visible Plot Collection – {time:.2g}\",\n", + " show=True,\n", + " movie=movie_file.name,\n", + " plot_args={\"colorbar\": True},\n", + " )\n", + " _, info = eq.solve(\n", + " fc,\n", + " t_range=2,\n", + " dt=0.1,\n", + " backend=\"numpy\",\n", + " ret_info=True,\n", + " tracker=[\"progress\", plot_tracker],\n", + " )" ] }, {