Skip to content

Commit

Permalink
Modernize syntax to Python 3.8+
Browse files Browse the repository at this point in the history
  • Loading branch information
schneiderfelipe committed Apr 5, 2022
1 parent fb99f6c commit 7622789
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 51 deletions.
2 changes: 1 addition & 1 deletion overreact/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def _find_package(package):


def _check_package(
package: Text, found_package: bool, extra_flag: Optional[Text] = None
package: str, found_package: bool, extra_flag: Optional[str] = None
) -> None:
"""Raise an issue if a package was not found.
Expand Down
28 changes: 14 additions & 14 deletions overreact/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ def get_enthalpies(compounds: dict, qrrho: bool = True, temperature: float = 298

def get_entropies(
compounds: dict,
environment: Optional[Text] = None,
method: Text = "standard",
environment: str | None = None,
method: str = "standard",
qrrho: bool = True,
temperature: float = 298.15,
pressure: float = constants.atm,
Expand Down Expand Up @@ -260,7 +260,7 @@ def get_entropies(
return np.array(entropies)


def _check_qrrho(qrrho: Union[bool, tuple[bool, bool]]) -> tuple[bool, bool]:
def _check_qrrho(qrrho: bool | tuple[bool, bool]) -> tuple[bool, bool]:
"""Get options for QRRHO for both enthalpy and entropy.
Parameters
Expand Down Expand Up @@ -306,9 +306,9 @@ def _check_qrrho(qrrho: Union[bool, tuple[bool, bool]]) -> tuple[bool, bool]:
def get_freeenergies(
compounds: dict,
bias: float = 0.0,
environment: Optional[Text] = None,
method: Text = "standard",
qrrho: Union[bool, tuple[bool, bool]] = True,
environment: str | None = None,
method: str = "standard",
qrrho: bool | tuple[bool, bool] = True,
temperature: float = 298.15,
pressure: float = constants.atm,
):
Expand Down Expand Up @@ -398,16 +398,16 @@ def get_freeenergies(

def get_k(
scheme: Scheme,
compounds: Optional[dict] = None,
compounds: dict | None = None,
bias: float = 0.0,
tunneling: Text = "eckart",
qrrho: Union[bool, tuple[bool, bool]] = True,
scale: Text = "l mol-1 s-1",
tunneling: str = "eckart",
qrrho: bool | tuple[bool, bool] = True,
scale: str = "l mol-1 s-1",
temperature: float = 298.15,
pressure: float = constants.atm,
delta_freeenergies: Optional[float] = None,
molecularity: Optional[float] = None,
volume: Optional[float] = None,
delta_freeenergies: float | None = None,
molecularity: float | None = None,
volume: float | None = None,
) -> float:
r"""Obtain reaction rate constants for a given reaction scheme.
Expand Down Expand Up @@ -633,7 +633,7 @@ def get_k(
def get_kappa(
scheme: Scheme,
compounds: dict,
method: Text = "eckart",
method: str = "eckart",
qrrho: bool = True,
temperature: float = 298.15,
):
Expand Down
20 changes: 9 additions & 11 deletions overreact/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Scheme(NamedTuple):
See `overreact.io.parse_model`.
"""

compounds: Sequence[Text]
compounds: Sequence[str]
"""A descriptor of compounds."""
reactions: Sequence[Text]
reactions: Sequence[str]
"""A descriptor of reactions."""
is_half_equilibrium: Sequence[bool]
"""An indicator of whether a reaction is half-equilibrium."""
Expand All @@ -50,7 +50,7 @@ class Scheme(NamedTuple):
}


def _check_scheme(scheme_or_text: Union[Scheme, Text]) -> Scheme:
def _check_scheme(scheme_or_text: Union[Scheme, str]) -> Scheme:
"""Interface transparently between strings and schemes.
Parameters
Expand Down Expand Up @@ -143,7 +143,7 @@ def get_transition_states(A, B, is_half_equilibrium):

# TODO(schneiderfelipe): some of the more esoteric doctests should become
# real tests.
def unparse_reactions(scheme: Scheme) -> Text:
def unparse_reactions(scheme: Scheme) -> str:
"""Unparse a kinetic model.
Parameters
Expand Down Expand Up @@ -398,7 +398,7 @@ def is_transition_state(name):
return False


def parse_reactions(text: Union[Text, Sequence[Text]]) -> Scheme:
def parse_reactions(text: Union[str, Sequence[str]]) -> Scheme:
"""
Parse a kinetic model as a chemical reaction scheme.
Expand Down Expand Up @@ -592,9 +592,9 @@ def parse_reactions(text: Union[Text, Sequence[Text]]) -> Scheme:
[open an issue](https://github.com/geem-lab/overreact/issues/), we'll be
happy to hear from you.
"""
compounds: dict[Text, int] = dict()
compounds: dict[str, int] = dict()
reactions: dict[
tuple[Text, Text, bool, Text], tuple[tuple[tuple[int, Text], ...], bool]
tuple[str, str, bool, str], tuple[tuple[tuple[int, str], ...], bool]
] = dict()
A = list() # coefficients between reactants and products
B = list() # coefficients between reactants and transition states
Expand Down Expand Up @@ -637,11 +637,9 @@ def _add_reaction(reactants, products, is_half_equilibrium, transition):
A.append(A_vector)
B.append(B_vector)

after_transitions: dict[
tuple[tuple[int, Text], ...], list[tuple[int, Text]]
] = dict()
after_transitions: dict[tuple[tuple[int, str], ...], list[tuple[int, str]]] = dict()
before_transitions: dict[
tuple[tuple[int, Text], ...], list[tuple[int, Text]]
tuple[tuple[int, str], ...], list[tuple[int, str]]
] = dict()

for reactants, products, is_half_equilibrium in _parse_reactions(text):
Expand Down
18 changes: 9 additions & 9 deletions overreact/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
logger = logging.getLogger(__name__)


def parse_model(path: Text, force_compile: bool = False):
def parse_model(path: str, force_compile: bool = False):
"""Parse either a source or model input file, whichever is available.
A **source input file** (also known as a `.k` file) contains all the information needed
Expand Down Expand Up @@ -170,7 +170,7 @@ def _parse_model(file_or_path):
try:
model = json.load(file_or_path)
except AttributeError:
with open(file_or_path, "r") as stream:
with open(file_or_path) as stream:
model = json.load(stream)

if "scheme" in model:
Expand Down Expand Up @@ -233,7 +233,7 @@ def _parse_source(file_path_or_str):

path = ("",)
try:
with open(file_path_or_str, "r") as stream:
with open(file_path_or_str) as stream:
lines = stream.readlines()
dirname = os.path.dirname(file_path_or_str)
if dirname not in path:
Expand Down Expand Up @@ -433,7 +433,7 @@ def _check_compounds(compounds):
(-0.011061, -0.030431, -0.027036)))}}
"""
for name in compounds:
if isinstance(compounds[name], Text):
if isinstance(compounds[name], str):
compounds[name] = read_logfile(compounds[name])
return dict(compounds)

Expand Down Expand Up @@ -531,13 +531,13 @@ def parse_compounds(text, path=("",), select=None):
compounds = defaultdict(dict)
for line in lines:
if ":" in line:
name, line = [x.strip() for x in line.split(":", 1)]
name, line = (x.strip() for x in line.split(":", 1))
if not line:
continue

if name is not None:
if "=" in line:
key, value = [x.strip() for x in line.split("=", 1)]
key, value = (x.strip() for x in line.split("=", 1))
else:
key, value = "logfile", line

Expand Down Expand Up @@ -720,7 +720,7 @@ def _read_orca_hess(path):
[ 0.0160095 , -0.20678153, -0.26060801, -0.00337537, -0.02800411,
-0.02595917, -0.01263597, 0.23474251, 0.28651783]])
"""
with open(path, "r") as file:
with open(path) as file:
while file:
try:
line = next(file)
Expand Down Expand Up @@ -791,7 +791,7 @@ def _read_orca_logfile(path, minimal=True):
atommasses = None
vibfreqs = None
hessian = None
with open(path, "r") as file:
with open(path) as file:
while file:
try:
line = next(file)
Expand Down Expand Up @@ -876,7 +876,7 @@ def _read_orca_logfile(path, minimal=True):
data.update({"logfile": path, "mult": int(mult)})

if atomcoords is None:
with open(path.replace(".out", ".xyz"), "r") as file:
with open(path.replace(".out", ".xyz")) as file:
n = int(next(file))
next(file)

Expand Down
8 changes: 4 additions & 4 deletions overreact/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ def convert_rate_constant(


def eyring(
delta_freeenergy: Union[float, np.ndarray],
molecularity: Optional[int] = None,
temperature: Union[float, np.ndarray] = 298.15,
delta_freeenergy: float | np.ndarray,
molecularity: int | None = None,
temperature: float | np.ndarray = 298.15,
pressure: float = constants.atm,
volume: Optional[float] = None,
volume: float | None = None,
):
r"""Calculate a reaction rate constant.
Expand Down
14 changes: 7 additions & 7 deletions overreact/thermo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,11 @@ def get_delta(transform, property):


def equilibrium_constant(
delta_freeenergy: Union[float, np.ndarray],
delta_moles: Optional[Union[int, np.ndarray]] = None,
temperature: Union[float, np.ndarray] = 298.15,
delta_freeenergy: float | np.ndarray,
delta_moles: int | np.ndarray | None = None,
temperature: float | np.ndarray = 298.15,
pressure: float = constants.atm,
volume: Optional[float] = None,
volume: float | None = None,
):
r"""Calculate an equilibrium constant from a reaction [Gibbs free energy](https://en.wikipedia.org/wiki/Gibbs_free_energy).
Expand Down Expand Up @@ -749,11 +749,11 @@ def equilibrium_constant(

def change_reference_state(
new_reference: float = 1.0 / constants.liter,
old_reference: Optional[float] = None,
old_reference: float | None = None,
sign: int = 1,
temperature: Union[float, np.ndarray] = 298.15,
temperature: float | np.ndarray = 298.15,
pressure: float = constants.atm,
volume: Optional[float] = None,
volume: float | None = None,
):
r"""Calculate an additive entropy correction to a change in reference states.
Expand Down
10 changes: 5 additions & 5 deletions overreact/tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _check_nu(vibfreq: float) -> float:
return np.abs(vibfreq) * constants.c / constants.centi


def wigner(vibfreq: float, temperature: Union[float, np.ndarray] = 298.15) -> float:
def wigner(vibfreq: float, temperature: float | np.ndarray = 298.15) -> float:
"""Calculate the Wigner correction to quantum tunneling.
Parameters
Expand Down Expand Up @@ -101,9 +101,9 @@ def wigner(vibfreq: float, temperature: Union[float, np.ndarray] = 298.15) -> fl
def eckart(
vibfreq: float,
delta_forward: float,
delta_backward: Optional[float] = None,
temperature: Union[float, np.ndarray] = 298.15,
) -> Union[float, np.ndarray]:
delta_backward: float | None = None,
temperature: float | np.ndarray = 298.15,
) -> float | np.ndarray:
"""Calculate the Eckart correction to quantum tunneling.
References are
Expand Down Expand Up @@ -199,7 +199,7 @@ def eckart(


@np.vectorize
def _eckart(u: float, alpha1: float, alpha2: Optional[float] = None) -> float:
def _eckart(u: float, alpha1: float, alpha2: float | None = None) -> float:
"""Implement of the (unsymmetrical) Eckart tunneling approximation.
This is based on DOI:10.1021/j100809a040 and DOI:10.6028/jres.086.014.
Expand Down

0 comments on commit 7622789

Please sign in to comment.