Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional custom print callable #2121

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check buff is the same when custom_print=print and when not explicitly provided? Maybe also good to check that buff changes with a custom callable.

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
Loading