Skip to content

Commit

Permalink
Feat: input files in calculation module (#159)
Browse files Browse the repository at this point in the history
* Expose input files via py4vasp.calculation
* Add test for calculation module
  • Loading branch information
martin-schlipf authored May 27, 2024
1 parent 3029c4a commit f227110
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/py4vasp/calculation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class provides a more flexible interface with which you can determine the source
In the latter example, you can change the path from which the data is extracted.
"""
import importlib
import pathlib

from py4vasp import exception
from py4vasp import control, exception
from py4vasp._util import convert

__all__ = (
_input_files = ("INCAR", "KPOINTS", "POSCAR")
_quantities = (
"band",
"bandgap",
"born_effective_charge",
Expand Down Expand Up @@ -77,13 +79,20 @@ class provides a more flexible interface with which you can determine the source
"workfunction",
)
_private = ("dispersion",)
__all__ = _quantities + _input_files


path = pathlib.Path(".")


def __getattr__(attr):
if attr in (__all__ + _private):
if attr in (_quantities + _private):
module = importlib.import_module(f"py4vasp.calculation._{attr}")
class_ = getattr(module, convert.to_camelcase(attr))
return class_.from_path(".")
elif attr in (_input_files):
class_ = getattr(control, attr)
return class_(".")
else:
message = f"Could not find {attr} in the possible attributes, please check the spelling"
raise exception.MissingAttribute(message)
8 changes: 4 additions & 4 deletions src/py4vasp/calculation/_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def POSCAR(self, poscar):


def _add_all_refinement_classes(calc, add_single_class):
for name in calculation.__all__:
for name in calculation._quantities:
calc = add_single_class(calc, name)
return calc

Expand Down Expand Up @@ -155,7 +155,7 @@ def _add_to_documentation(calc, name):


def _add_input_files(calc):
calc._INCAR = control.INCAR(calc.path())
calc._KPOINTS = control.KPOINTS(calc.path())
calc._POSCAR = control.POSCAR(calc.path())
for name in calculation._input_files:
file_ = getattr(control, name)(calc.path())
setattr(calc, f"_{name}", file_)
return calc
24 changes: 24 additions & 0 deletions tests/calculation/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright © VASP Software GmbH,
# Licensed under the Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
import pytest

from py4vasp import Calculation, calculation, exception


def test_access_of_attributes():
calc = Calculation.from_path(".")
for key in filter(attribute_included, dir(calc)):
getattr(calculation, key)


def attribute_included(attr):
if attr.startswith("_"): # do not include private attributes
return False
if attr.startswith("from"): # do not include classmethods
return False
return True


def test_nonexisting_attribute():
with pytest.raises(exception.MissingAttribute):
calculation.does_not_exist
2 changes: 1 addition & 1 deletion tests/calculation/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def test_repr():
for name in calculation.__all__:
for name in calculation._quantities:
instance = getattr(calculation, name)
class_name = convert.to_camelcase(name)
module = importlib.import_module(f"py4vasp.calculation._{name}")
Expand Down

0 comments on commit f227110

Please sign in to comment.