From 206e568e418a77b9f1a5c5e12767b194f09961e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Mu=CC=88ller?= Date: Tue, 5 Mar 2024 18:29:22 +0100 Subject: [PATCH 1/5] Add optional custom print callable --- flopy/mbase.py | 11 +++++++++++ flopy/mf6/mfsimbase.py | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/flopy/mbase.py b/flopy/mbase.py index c569944ff8..ee01950512 100644 --- a/flopy/mbase.py +++ b/flopy/mbase.py @@ -1744,6 +1744,7 @@ def run_model( normal_msg="normal termination", use_async=False, cargs=None, + custom_print=None, ) -> Tuple[bool, List[str]]: """ Run the model using subprocess.Popen, optionally collecting stdout and printing @@ -1782,12 +1783,22 @@ def run_model( cargs : str or list, optional, default None Additional command line arguments to pass to the executable. (Default is None) + custom_print: callable + Optional callbale for printing. It will replace the builtin + print function. This is useful for shorter prints or integration + into other systems such as GUIs. + default is None, i.e. use the builtion print Returns ------- success : boolean buff : list of lines of stdout (empty if report is False) """ + if custom_print is not None: + print = custom_print + else: + print = __builtins__['print'] + success = False buff = [] diff --git a/flopy/mf6/mfsimbase.py b/flopy/mf6/mfsimbase.py index e84c4dfc51..163eab0d9c 100644 --- a/flopy/mf6/mfsimbase.py +++ b/flopy/mf6/mfsimbase.py @@ -1631,6 +1631,7 @@ def run_simulation( normal_msg="normal termination", use_async=False, cargs=None, + custom_print=None, ): """ Run the simulation. @@ -1657,6 +1658,11 @@ def run_simulation( cargs : str or list of strings Additional command line arguments to pass to the executable. default is None + custom_print: callable + Optional callbale for printing. It will replace the builtin + print function. This is useful for shorter prints or integration + into other systems such as GUIs. + default is None, i.e. use the builtion print Returns -------- @@ -1683,6 +1689,7 @@ def run_simulation( normal_msg=normal_msg, use_async=use_async, cargs=cargs, + custom_print=custom_print, ) def delete_output_files(self): From 57c6b0703eae05ac0db445e9cd2498b80213c5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Mu=CC=88ller?= Date: Wed, 6 Mar 2024 07:04:43 +0100 Subject: [PATCH 2/5] Run black and turn single into double quotes --- flopy/mbase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flopy/mbase.py b/flopy/mbase.py index ee01950512..fc22c1b9b6 100644 --- a/flopy/mbase.py +++ b/flopy/mbase.py @@ -1797,7 +1797,7 @@ def run_model( if custom_print is not None: print = custom_print else: - print = __builtins__['print'] + print = __builtins__["print"] success = False buff = [] From 30da7147dc1133e8ff3d09e79719c4061924d424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Mu=CC=88ller?= Date: Wed, 6 Mar 2024 07:37:49 +0100 Subject: [PATCH 3/5] Add test for custom_print Since the `print` function is used, not changes in the output are expected.` --- autotest/test_mbase.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/autotest/test_mbase.py b/autotest/test_mbase.py index 8e3ee5d828..6c8f788e05 100644 --- a/autotest/test_mbase.py +++ b/autotest/test_mbase.py @@ -160,3 +160,32 @@ def test_run_model_exe_rel_path(mf6_model_path, function_tmpdir, use_ext): assert success assert any(buff) assert any(ws.glob("*.lst")) + + +@pytest.mark.mf6 +@requires_exe("mf6") +@pytest.mark.parametrize("use_paths", [True, False]) +@pytest.mark.parametrize( + "exe", + [ + "mf6", + Path(which("mf6") or ""), + relpath_safe(Path(which("mf6") or "")), + ], +) +def test_run_model_custom_print(mf6_model_path, function_tmpdir, use_paths, exe): + ws = function_tmpdir / "ws" + copytree(mf6_model_path, ws) + + success, buff = run_model( + exe_name=exe, + namefile="mfsim.nam", + model_ws=ws if use_paths else str(ws), + silent=False, + report=True, + custom_print=print, + ) + + assert success + assert any(buff) + assert any(ws.glob("*.lst")) From 4d05b404fb673ca539d8a0fcc540f9267597f605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Mu=CC=88ller?= Date: Fri, 22 Mar 2024 00:42:17 +0100 Subject: [PATCH 4/5] Fix typos and improve wording --- flopy/mbase.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/flopy/mbase.py b/flopy/mbase.py index fc22c1b9b6..ee7bbd63fb 100644 --- a/flopy/mbase.py +++ b/flopy/mbase.py @@ -1784,10 +1784,10 @@ def run_model( Additional command line arguments to pass to the executable. (Default is None) custom_print: callable - Optional callbale for printing. It will replace the builtin - print function. This is useful for shorter prints or integration - into other systems such as GUIs. - default is None, i.e. use the builtion print + Optional callable for printing. It will replace the builtin print + function. This is useful for a shorter print output or integration into + other systems such as GUIs. + default is None, i.e. use the builtin print Returns ------- success : boolean From e319098b7d32d6e806e4bb2eb7454208f8b000f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Mu=CC=88ller?= Date: Mon, 25 Mar 2024 19:47:51 +0100 Subject: [PATCH 5/5] Lint with ruff --- autotest/test_mbase.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autotest/test_mbase.py b/autotest/test_mbase.py index 6c8f788e05..2e8688cb3a 100644 --- a/autotest/test_mbase.py +++ b/autotest/test_mbase.py @@ -173,7 +173,9 @@ def test_run_model_exe_rel_path(mf6_model_path, function_tmpdir, use_ext): relpath_safe(Path(which("mf6") or "")), ], ) -def test_run_model_custom_print(mf6_model_path, function_tmpdir, use_paths, exe): +def test_run_model_custom_print( + mf6_model_path, function_tmpdir, use_paths, exe +): ws = function_tmpdir / "ws" copytree(mf6_model_path, ws)