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

Input Writer #188

Merged
merged 26 commits into from
Mar 12, 2021
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3c30091
Add Gaussian input writer
wilhadams Aug 19, 2020
75e0b53
Add write_input to api.py module
FarnazH Aug 19, 2020
41aa3eb
Test gaussian input writer using default template
FarnazH Aug 20, 2020
0ea6726
Test gaussian input writer using user template
FarnazH Aug 20, 2020
9e5b2e6
Add __all__ to inputs/gaussian.py module
FarnazH Aug 20, 2020
17d5c88
Fix pylint no-else-return and wrong-import-order
FarnazH Aug 20, 2020
2709a3e
Fix docstring & update default values
FarnazH Aug 20, 2020
7cef0c9
Add write_input function for orca
lmacaya Aug 22, 2020
c4d733e
Add tests for orca write_input
lmacaya Aug 22, 2020
27a2a61
Add template for orca write_input testing
lmacaya Aug 22, 2020
eb988c3
Add __init__.py for relative import
lmacaya Aug 22, 2020
2a9961a
Add default docstring to gaussian write_input for future decorator
lmacaya Aug 22, 2020
e4eb9b2
Use attr.asdict in Gaussian input writer
FarnazH Aug 24, 2020
713e410
Add expected test inputs to test/data/input_*.txt
FarnazH Aug 24, 2020
8319cf0
Remove empty lines & unsed import in test_inputs
FarnazH Aug 24, 2020
a87c0f2
Add recurse=False in orca module & remove test fix
FarnazH Aug 24, 2020
23442ef
Remove attrname argument of _select_input_module
FarnazH Aug 24, 2020
328f38b
Fix pylint wrong-import-order
FarnazH Aug 24, 2020
0891a63
Add header to __init__
lmacaya Sep 2, 2020
6027980
Add write_input decorator and minor fixes
lmacaya Sep 4, 2020
22233a4
Update docstrings in api.py
lmacaya Sep 4, 2020
6a97b0d
Add populate_fields func to common
lmacaya Sep 4, 2020
28c167c
Reformat gaussian write_input
lmacaya Sep 4, 2020
0f628f8
Reformat orca write_input
lmacaya Sep 4, 2020
2d32f4e
Merge branch 'master' into write_input
tovrstra Mar 12, 2021
5c68134
Fix minor issues and include input writer in docs
tovrstra Mar 12, 2021
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
Prev Previous commit
Next Next commit
Add write_input to api.py module
  • Loading branch information
FarnazH committed Aug 19, 2020
commit 75e0b5345804ea28497de3a31d347d769160ea36
61 changes: 60 additions & 1 deletion iodata/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .utils import LineIterator


__all__ = ['load_one', 'load_many', 'dump_one', 'dump_many']
__all__ = ['load_one', 'load_many', 'dump_one', 'dump_many', 'write_input']


def _find_format_modules():
Expand Down Expand Up @@ -78,6 +78,43 @@ def _select_format_module(filename: str, attrname: str, fmt: str = None) -> Modu
attrname, filename))


def _find_input_modules():
"""Return all input modules found with importlib."""
result = {}
for module_info in iter_modules(import_module('iodata.inputs').__path__):
if not module_info.ispkg:
format_module = import_module('iodata.inputs.' + module_info.name)
result[module_info.name] = format_module
return result


INPUT_MODULES = _find_input_modules()


def _select_input_module(attrname: str, fmt: str) -> ModuleType:
"""Find an input module with the requested attribute name.

Parameters
----------
attrname
tovrstra marked this conversation as resolved.
Show resolved Hide resolved
The required attribute of the input module.
fmt
The name of the input module to use.

Returns
-------
format_module
The module implementing the required input format.

"""
if fmt in INPUT_MODULES:
if not hasattr(INPUT_MODULES[fmt], attrname):
raise ValueError(f'{fmt} input module does not have {attrname}!')
return INPUT_MODULES[fmt]
else:
raise ValueError(f"Could not find input format {fmt}!")


def load_one(filename: str, fmt: str = None, **kwargs) -> IOData:
"""Load data from a file.

Expand Down Expand Up @@ -188,3 +225,25 @@ def dump_many(iodatas: Iterator[IOData], filename: str, fmt: str = None, **kwarg
format_module = _select_format_module(filename, 'dump_many', fmt)
with open(filename, 'w') as f:
format_module.dump_many(f, iodatas, **kwargs)


def write_input(iodata: IOData, filename: str, fmt: str, template: str = None, **kwargs):
"""Write input file using an instance of IOData for the specified software format.

Parameters
----------
iodatas
FarnazH marked this conversation as resolved.
Show resolved Hide resolved
An IOData instance containing the information needed to write input.
filename : str
The input file name.
fmt : str
The name of the software for which input file is generated.
template : str, optional
The template input file.
**kwargs
Keyword arguments are passed on to the input-specific write_input function.
"""

input_module = _select_input_module('write_input', fmt)
with open(filename, 'w') as f:
input_module.write_input(f, iodata, template=template, **kwargs)