Skip to content

Commit

Permalink
feat: add optional custom print callable (#2121)
Browse files Browse the repository at this point in the history
  • Loading branch information
pya authored Mar 25, 2024
1 parent 43e5178 commit f75853f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
31 changes: 31 additions & 0 deletions autotest/test_mbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,34 @@ 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"))
11 changes: 11 additions & 0 deletions flopy/mbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 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
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 = []

Expand Down
7 changes: 7 additions & 0 deletions flopy/mf6/mfsimbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,7 @@ def run_simulation(
normal_msg="normal termination",
use_async=False,
cargs=None,
custom_print=None,
):
"""
Run the simulation.
Expand All @@ -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
--------
Expand All @@ -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):
Expand Down

0 comments on commit f75853f

Please sign in to comment.