From 5b61a4b393b0bcd40aafeb87d1e80b3e557e0f05 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Tue, 21 Nov 2023 10:36:35 -0500 Subject: [PATCH] feat(executables): support .get(key, default) like dict (#125) --- autotest/test_executables.py | 4 +++- docs/md/executables.md | 2 ++ modflow_devtools/executables.py | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/autotest/test_executables.py b/autotest/test_executables.py index 7d55a27..c06edc3 100644 --- a/autotest/test_executables.py +++ b/autotest/test_executables.py @@ -1,4 +1,3 @@ -import subprocess import sys from pathlib import Path from shutil import which @@ -24,3 +23,6 @@ def exes(): def test_access(exes): # support both attribute and dictionary style access assert exes.pytest == exes["pytest"] == exe_path + # .get() works too + assert exes.get("not a target") is None + assert exes.get("not a target", exes["pytest"]) == exes["pytest"] diff --git a/docs/md/executables.md b/docs/md/executables.md index cfc8d79..ef1085f 100644 --- a/docs/md/executables.md +++ b/docs/md/executables.md @@ -28,4 +28,6 @@ The `targets` fixture can then be injected into test functions: def test_targets(targets): # attribute- and dictionary-style access is supported assert targets["mf6"] == targets.mf6 + # .get() works too + assert targets.get("not a target") is None ``` diff --git a/modflow_devtools/executables.py b/modflow_devtools/executables.py index 9ec0370..0c9705e 100644 --- a/modflow_devtools/executables.py +++ b/modflow_devtools/executables.py @@ -17,6 +17,9 @@ def __setitem__(self, key, item): def __getitem__(self, key): return self.__dict__[key] + def get(self, key, default=None): + return self.as_dict().get(key, default) + def as_dict(self) -> Dict[str, Path]: """ Returns a dictionary mapping executable names to paths.