Skip to content

Commit

Permalink
feat(executables): support .get(key, default) like dict (MODFLOW-USGS…
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Nov 21, 2023
1 parent cd644fa commit 5b61a4b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion autotest/test_executables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import subprocess
import sys
from pathlib import Path
from shutil import which
Expand All @@ -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"]
2 changes: 2 additions & 0 deletions docs/md/executables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
3 changes: 3 additions & 0 deletions modflow_devtools/executables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5b61a4b

Please sign in to comment.