-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from maurergroup/run_utils_enhancements
Run utils enhancements - Made run_utils decorator more user friendly. Arguments are now optionally and specified in the decorator rather than the function being decoraed - Added tests for run_utils - Minor docstring improvements - CI will now only build docs on PR, and will deploy on push
- Loading branch information
Showing
10 changed files
with
120 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,48 @@ | ||
import os.path | ||
import warnings | ||
from functools import wraps | ||
|
||
|
||
def no_repeat(func): | ||
def no_repeat( | ||
original_func=None, | ||
*, | ||
output_file: str = "aims.out", | ||
calc_dir: str = "./", | ||
force: bool = False, | ||
): | ||
""" | ||
Don't repeat the calculation if aims.out exists in the calculation directory. | ||
A kwarg must be given to the decorated function called `calc_dir` which is the | ||
directory where the calculation is to be performed. | ||
Parameters | ||
---------- | ||
output_file : str, default='aims.out' | ||
The name of the output file to check for. | ||
calc_dir : str, default="./" | ||
The directory where the calculation is performed | ||
force : bool, default=False | ||
If True, the calculation is performed even if aims.out exists in the calculation | ||
directory. | ||
Raises | ||
------- | ||
ValueError | ||
if the `calc_dir` kwarg is not a directory | ||
""" | ||
|
||
@wraps(func) | ||
def wrapper_no_repeat(*args, **kwargs): | ||
if "calc_dir" in kwargs and "force" in kwargs: | ||
if not os.path.isdir(kwargs["calc_dir"]): | ||
raise ValueError(f"{kwargs.get('calc_dir')} is not a directory.") | ||
|
||
if kwargs["force"]: | ||
def _no_repeat(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
if not os.path.isdir(calc_dir): | ||
raise ValueError(f"{calc_dir} is not a directory.") | ||
if force: | ||
return func(*args, **kwargs) | ||
if not os.path.isfile(f"{kwargs.get('calc_dir')}/aims.out"): | ||
if not os.path.isfile(f"{calc_dir}/{output_file}"): | ||
return func(*args, **kwargs) | ||
else: | ||
print( | ||
f"aims.out already exists in {kwargs.get('calc_dir')}. Skipping " | ||
"calculation." | ||
) | ||
print(f"aims.out already exists in {calc_dir}. Skipping calculation.") | ||
|
||
return wrapper | ||
|
||
else: | ||
warnings.warn( | ||
"'calc_dir' and/or 'force' kwarg not provided: ignoring decorator" | ||
) | ||
if original_func: | ||
return _no_repeat(original_func) | ||
|
||
return wrapper_no_repeat | ||
return _no_repeat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
import os | ||
from dfttoolkit.utils.run_utils import no_repeat | ||
|
||
|
||
class TestNoRepeat: | ||
|
||
calc_dir = ( | ||
f"{os.path.dirname(os.path.realpath(__file__))}/fixtures/default_aims_calcs/1" | ||
) | ||
|
||
def test_specified_dir_not_found(self): | ||
@no_repeat(calc_dir="bogus") | ||
def to_be_decorated(): | ||
return True | ||
|
||
with pytest.raises(ValueError) as excinfo: | ||
to_be_decorated() | ||
|
||
assert "bogus is not a directory." == str(excinfo.value) | ||
|
||
@pytest.mark.parametrize( | ||
("calc_dir", "force", "expected"), | ||
[ | ||
("./", False, True), | ||
( | ||
f"{os.path.dirname(os.path.realpath(__file__))}/fixtures/default_aims_calcs/1", | ||
True, | ||
True, | ||
), | ||
], | ||
) | ||
def test_default_dir_no_args(self, calc_dir, force, expected): | ||
@no_repeat(calc_dir=calc_dir, force=force) | ||
def to_be_decorated(): | ||
return True | ||
|
||
assert to_be_decorated() == expected | ||
|
||
def test_no_repeat(self, capfd): | ||
@no_repeat(calc_dir=self.calc_dir) | ||
def to_be_decorated(): | ||
return True | ||
|
||
to_be_decorated() | ||
|
||
out, err = capfd.readouterr() | ||
assert ( | ||
out | ||
== f"aims.out already exists in {self.calc_dir}. Skipping calculation.\n" | ||
) | ||
assert err == "" |