From 70cbf7706ce0abff010a1ef80d8767bd6bff8c33 Mon Sep 17 00:00:00 2001 From: Nick Papior Date: Mon, 28 Oct 2024 11:08:53 +0100 Subject: [PATCH] changed all _A to dict handling of InfoAttr Added more typing in orca and vasp siles Signed-off-by: Nick Papior --- src/sisl/io/orca/stdout.py | 61 +++++++++++++++++++------------------- src/sisl/io/orca/txt.py | 32 ++++++++++---------- src/sisl/io/vasp/car.py | 9 ++---- src/sisl/io/vasp/outcar.py | 19 +++++------- 4 files changed, 57 insertions(+), 64 deletions(-) diff --git a/src/sisl/io/orca/stdout.py b/src/sisl/io/orca/stdout.py index b16dd9309..d100a250b 100644 --- a/src/sisl/io/orca/stdout.py +++ b/src/sisl/io/orca/stdout.py @@ -3,6 +3,8 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. from __future__ import annotations +from typing import Literal, Optional, Union + import numpy as np from sisl._internal import set_module @@ -18,37 +20,34 @@ __all__ = ["outputSileORCA", "stdoutSileORCA"] -_A = SileORCA.InfoAttr - - @set_module("sisl.io.orca") class stdoutSileORCA(SileORCA): """Output file from ORCA""" _info_attributes_ = [ - _A( - "na", - r".*Number of atoms", - lambda attr, instance, match: int(match.string.split()[-1]), + dict( + name="na", + searcher=r".*Number of atoms", + parser=lambda attr, instance, match: int(match.string.split()[-1]), not_found="error", ), - _A( - "no", - r".*Number of basis functions", - lambda attr, instance, match: int(match.string.split()[-1]), + dict( + name="no", + searcher=r".*Number of basis functions", + parser=lambda attr, instance, match: int(match.string.split()[-1]), not_found="error", ), - _A( - "vdw_correction", - r".*DFT DISPERSION CORRECTION", - lambda attr, instance, match: True, + dict( + name="vdw_correction", + searcher=r".*DFT DISPERSION CORRECTION", + parser=lambda attr, instance, match: True, default=False, not_found="ignore", ), - _A( - "completed", - r".*ORCA TERMINATED NORMALLY", - lambda attr, instance, match: True, + dict( + name="completed", + searcher=r".*ORCA TERMINATED NORMALLY", + parser=lambda attr, instance, match: True, default=False, not_found="warn", ), @@ -85,7 +84,7 @@ def no(self): @SileBinder(postprocess=np.array) @sile_fh_open() - def read_electrons(self): + def read_electrons(self) -> Optional[tuple[float, float]]: """Read number of electrons (alpha, beta) Returns @@ -105,25 +104,25 @@ def read_electrons(self): @sile_fh_open() def read_charge( self, - name="mulliken", - projection="orbital", + name: Literal["mulliken", "loewdin"] = "mulliken", + projection: Literal["orbital", "atom"] = "orbital", orbitals=None, - reduced=True, - spin=False, - ): + reduced: bool = True, + spin: bool = False, + ) -> Union[PropertyDict, np.ndarray]: """Reads from charge (or spin) population analysis Parameters ---------- - name : {'mulliken', 'loewdin'} + name : name of the charge scheme to be read - projection : {'orbital', 'atom'} + projection : whether to get orbital- or atom-resolved quantities orbitals : str, optional allows to extract the atom-resolved orbitals matching this keyword - reduced : bool, optional + reduced : whether to search for full or reduced orbital projections - spin : bool, optional + spin : whether to return the spin block instead of charge Returns @@ -276,7 +275,7 @@ def read_block(step_to): @SileBinder() @sile_fh_open() - def read_energy(self, units: UnitsVar = "eV"): + def read_energy(self, units: UnitsVar = "eV") -> Optional[PropertyDict]: """Reads the energy blocks Parameters @@ -329,7 +328,7 @@ def read_energy(self, units: UnitsVar = "eV"): @SileBinder() @sile_fh_open() - def read_orbital_energies(self, units: UnitsVar = "eV"): + def read_orbital_energies(self, units: UnitsVar = "eV") -> Optional[np.ndarray]: """Reads the "ORBITAL ENERGIES" blocks Parameters diff --git a/src/sisl/io/orca/txt.py b/src/sisl/io/orca/txt.py index 99ffa9834..e245f8b67 100644 --- a/src/sisl/io/orca/txt.py +++ b/src/sisl/io/orca/txt.py @@ -3,6 +3,8 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. from __future__ import annotations +from typing import Optional + import numpy as np from sisl._core.geometry import Geometry @@ -18,30 +20,28 @@ __all__ = ["txtSileORCA"] -_A = SileORCA.InfoAttr - @set_module("sisl.io.orca") class txtSileORCA(SileORCA): """Output from the ORCA property.txt file""" _info_attributes_ = [ - _A( - "na", - r".*Number of atoms:", - lambda attr, instance, match: int(match.string.split()[-1]), + dict( + name="na", + searcher=r".*Number of atoms:", + parser=lambda attr, instance, match: int(match.string.split()[-1]), not_found="error", ), - _A( - "no", - r".*number of basis functions:", - lambda attr, instance, match: int(match.string.split()[-1]), + dict( + name="no", + searcher=r".*number of basis functions:", + parser=lambda attr, instance, match: int(match.string.split()[-1]), not_found="error", ), - _A( - "vdw_correction", - r".*\$ VdW_Correction", - lambda attr, instance, match: True, + dict( + name="vdw_correction", + searcher=r".*\$ VdW_Correction", + parser=lambda attr, instance, match: True, default=False, not_found="ignore", ), @@ -65,7 +65,7 @@ def no(self): @SileBinder(postprocess=np.array) @sile_fh_open() - def read_electrons(self): + def read_electrons(self) -> Optional[tuple[float, float]]: """Read number of electrons (alpha, beta) Returns @@ -83,7 +83,7 @@ def read_electrons(self): @SileBinder() @sile_fh_open() - def read_energy(self, units: UnitsVar = "eV"): + def read_energy(self, units: UnitsVar = "eV") -> PropertyDict: """Reads the energy blocks Parameters diff --git a/src/sisl/io/vasp/car.py b/src/sisl/io/vasp/car.py index 1fbe26556..aacec2fb5 100644 --- a/src/sisl/io/vasp/car.py +++ b/src/sisl/io/vasp/car.py @@ -18,9 +18,6 @@ __all__ = ["carSileVASP"] -_A = SileVASP.InfoAttr - - def _search_name(info, instance, line): """Names for CAR files are always in the first line""" return instance._line == 1 @@ -34,9 +31,9 @@ class carSileVASP(SileVASP): """ _info_attributes_ = [ - _A( - "name", - _search_name, + dict( + name="name", + searcher=_search_name, default="", not_found="error", ), diff --git a/src/sisl/io/vasp/outcar.py b/src/sisl/io/vasp/outcar.py index 99a7e5a33..3b2a68264 100644 --- a/src/sisl/io/vasp/outcar.py +++ b/src/sisl/io/vasp/outcar.py @@ -18,25 +18,22 @@ __all__ = ["outcarSileVASP"] -_A = SileVASP.InfoAttr - - @set_module("sisl.io.vasp") class outcarSileVASP(SileVASP): """OUTCAR file from VASP""" _info_attributes_ = [ - _A( - "completed", - r".*General timing and accounting", - lambda attr, instance, match: lambda: True, + dict( + name="completed", + searcher=r".*General timing and accounting", + parser=lambda attr, instance, match: lambda: True, default=lambda: False, not_found="warn", ), - _A( - "accuracy_reached", - r".*reached required accuracy", - lambda attr, instance, match: lambda: True, + dict( + name="accuracy_reached", + searcher=r".*reached required accuracy", + parser=lambda attr, instance, match: lambda: True, default=lambda: False, not_found="warn", ),