Skip to content

Commit

Permalink
Merge pull request #246 from SCM-NV/template
Browse files Browse the repository at this point in the history
ENH: Ensure that the qmflows templates are always copied
  • Loading branch information
BvB93 committed Jun 25, 2021
2 parents 16530e4 + 03ede4e commit 2ee3d22
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest]
version: [3.6, 3.7, 3.8, 3.9]
version: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def readme():
"qmflows": ['data/dictionaries/*yaml',
'py.typed']
},
python_requires='>=3.7',
classifiers=[
'Intended Audience :: Science/Research',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
Expand Down
22 changes: 21 additions & 1 deletion src/qmflows/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""QMFlows API."""

from __future__ import annotations

from .__version__ import __version__

from .logger import logger
Expand All @@ -13,7 +15,7 @@
from .packages import (
adf, cp2k, cp2k_mm, dftb, orca, run, PackageWrapper)

from .templates import (freq, geometry, singlepoint, ts, md, cell_opt)
from . import templates
from .settings import Settings
from .examples import (example_H2O2_TS, example_freqs, example_generic_constraints,
example_partial_geometry_opt)
Expand All @@ -28,3 +30,21 @@
'example_partial_geometry_opt',
'freq', 'geometry', 'singlepoint', 'ts', 'md', 'cell_opt',
'find_first_job', 'select_max', 'select_min']

_TEMPLATES = frozenset(templates.__all__)
_DIR_CACHE: None | list[str] = None


def __getattr__(name: str) -> Settings:
"""Ensure that the qmflows templates are always copied before returning."""
if name in _TEMPLATES:
return getattr(templates, name).copy()
raise AttributeError(f"module {__name__} has no attribute {name}")


def __dir__() -> list[str]:
"""Manually insert the qmflows templates into :func:`dir`."""
global _DIR_CACHE
if _DIR_CACHE is None:
_DIR_CACHE = sorted(list(globals()) + templates.__all__)
return _DIR_CACHE
14 changes: 13 additions & 1 deletion test/test_templates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Test that the templates behave correctly."""
from qmflows import Settings
import qmflows
from qmflows import Settings, templates
from qmflows.templates import freq, geometry, singlepoint, ts
from assertionlib import assertion

import pytest


def transform(x: Settings) -> Settings:
"""Transform `x` to dict and back to Settings."""
Expand All @@ -17,3 +20,12 @@ def test_templates():
b4 = ts == transform(ts)

assertion.truth(all((b1, b2, b3, b4)))


@pytest.mark.parametrize("name", templates.__all__)
def test_id(name: str) -> None:
"""Test that getting a template returns a copy."""
s1 = getattr(qmflows, name)
s2 = getattr(qmflows, name)
assertion.eq(s1, s2)
assertion.is_not(s1, s2)

0 comments on commit 2ee3d22

Please sign in to comment.