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(executables): support .get(key, default) like dict #125

Merged
merged 1 commit into from
Nov 21, 2023
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
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