Skip to content

Commit

Permalink
Review code using ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
schneiderfelipe committed Jan 4, 2023
1 parent a469f01 commit cddc09d
Show file tree
Hide file tree
Showing 26 changed files with 733 additions and 1,053 deletions.
9 changes: 0 additions & 9 deletions .flake8

This file was deleted.

4 changes: 2 additions & 2 deletions gendocs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ pdoc overreact \
--logo-link "/overreact" \
--math \
--search \
--show-source
# --output-directory docs/
--show-source \
"$@"
67 changes: 44 additions & 23 deletions overreact/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,57 @@

"""
.. include:: ../README.md
"""
""" # noqa: D200, D400
__docformat__ = "restructuredtext"

import pkg_resources as _pkg_resources

from overreact.api import get_enthalpies # noqa: F401
from overreact.api import get_entropies # noqa: F401
from overreact.api import get_freeenergies # noqa: F401
from overreact.api import get_internal_energies # noqa: F401
from overreact.api import get_k # noqa: F401
from overreact.api import get_kappa # noqa: F401
from overreact.core import Scheme # noqa: F401
from overreact.core import get_transition_states # noqa: F401
from overreact.core import is_transition_state # noqa: F401
from overreact.core import parse_reactions # noqa: F401
from overreact.core import unparse_reactions # noqa: F401
from overreact.io import parse_compounds # noqa: F401
from overreact.io import parse_model # noqa: F401
from overreact.simulate import get_bias # noqa: F401
from overreact.simulate import get_dydt # noqa: F401
from overreact.simulate import get_fixed_scheme # noqa: F401
from overreact.simulate import get_y # noqa: F401
from overreact.thermo import change_reference_state # noqa: F401
from overreact.thermo import get_delta # noqa: F401
from overreact.thermo import get_reaction_entropies # noqa: F401
from overreact.api import (
get_enthalpies,
get_entropies,
get_freeenergies,
get_internal_energies,
get_k,
get_kappa,
)
from overreact.core import (
Scheme,
get_transition_states,
is_transition_state,
parse_reactions,
unparse_reactions,
)
from overreact.io import parse_compounds, parse_model
from overreact.simulate import get_bias, get_dydt, get_fixed_scheme, get_y
from overreact.thermo import change_reference_state, get_delta, get_reaction_entropies

__all__ = [
"Scheme",
"change_reference_state",
"get_bias",
"get_delta",
"get_dydt",
"get_enthalpies",
"get_entropies",
"get_fixed_scheme",
"get_freeenergies",
"get_internal_energies",
"get_k",
"get_kappa",
"get_reaction_entropies",
"get_transition_states",
"get_y",
"is_transition_state",
"parse_compounds",
"parse_model",
"parse_reactions",
"unparse_reactions",
]

__version__ = _pkg_resources.get_distribution(__name__).version
__license__ = "MIT" # I'm too lazy to get it from setup.py...

__headline__ = "📈 Create and analyze chemical microkinetic models built from computational chemistry data."
__headline__ = "📈 Create and analyze chemical microkinetic models built from computational chemistry data." # noqa: E501

__url_repo__ = "https://github.com/geem-lab/overreact"
__url_issues__ = f"{__url_repo__}/issues"
Expand Down Expand Up @@ -65,7 +86,7 @@
year = {2021},
month = {Nov}
}
""".replace(
""".replace( # noqa: E501
"DOI_PLACEHOLDER", __doi__
)
.replace("ZENODO_DOI_PLACEHOLDER", __zenodo_doi__)
Expand Down
46 changes: 25 additions & 21 deletions overreact/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Report:
qrrho : bool, optional
Apply both the quasi-rigid rotor harmonic oscillator (QRRHO)
approximations of M. Head-Gordon and others (enthalpy correction, see
[*J. Phys. Chem. C* **2015**, 119, 4, 18401850](http://dx.doi.org/10.1021/jp509921r))
[*J. Phys. Chem. C* **2015**, 119, 4, 1840-1850](http://dx.doi.org/10.1021/jp509921r))
and S. Grimme (entropy correction, see
[*Theory. Chem. Eur. J.*, **2012**, 18: 9955-9964](https://doi.org/10.1002/chem.201200497))
on top of the classical RRHO.
Expand Down Expand Up @@ -130,10 +130,10 @@ class Report:
For half-equilibria, only ratios make sense: in simulations, equilibria will be
adjusted to be faster than all other reactions.
────────────────────────────────────────────────────────────────────────────────
"""
""" # noqa: E501

def __init__(
self,
self, # noqa: ANN101
model,
concentrations=None,
savepath=None,
Expand All @@ -148,7 +148,7 @@ def __init__(
rtol=1e-3,
atol=1e-6,
box_style=box.SIMPLE,
):
) -> None:
"""Initialize a Report object."""
self.model = model
self.concentrations = concentrations
Expand All @@ -173,7 +173,7 @@ def __init__(
self.atol = atol
self.box_style = box_style

def __rich_console__(self, console, options):
def __rich_console__(self, console, options): # noqa: ANN101, ARG002, ANN204
"""
Implement Rich Console protocol.
Expand All @@ -190,7 +190,7 @@ def __rich_console__(self, console, options):
yield from self._yield_kinetics()
yield Markdown("---")

def _yield_scheme(self):
def _yield_scheme(self): # noqa: ANN101
"""Produce a renderables describing the reaction scheme.
This is meant to be used from within `__rich_console__`.
Expand Down Expand Up @@ -232,7 +232,7 @@ def _yield_scheme(self):
parsed_table.add_row(*row)
yield parsed_table

def _yield_compounds(self):
def _yield_compounds(self): # noqa: ANN101
"""Produce a renderables describing the compounds.
This is meant to be used from within `__rich_console__`.
Expand All @@ -251,7 +251,9 @@ def _yield_compounds(self):
if not self.model.compounds[name]:
undefined_compounds.append(name)
if undefined_compounds:
raise ValueError(f"undefined compounds: {', '.join(undefined_compounds)}")
raise ValueError(
f"undefined compounds: {', '.join(undefined_compounds)}" # noqa: EM102
) # noqa: RUF100

logfiles_table = Table(
Column("no", justify="right"),
Expand Down Expand Up @@ -298,7 +300,7 @@ def _yield_compounds(self):
yield logfiles_table
yield compounds_table

def _yield_thermochemistry(self):
def _yield_thermochemistry(self): # noqa: ANN101
"""Produce a renderables describing the thermochemistry of the reaction scheme.
This is meant to be used from within `__rich_console__`.
Expand Down Expand Up @@ -374,7 +376,7 @@ def _yield_thermochemistry(self):
* rx.get_reaction_entropies(
scheme.A, temperature=self.temperature, pressure=self.pressure
),
), "reaction free energies do not match reaction enthalpies and reaction entropies"
), "reaction free energies do not match reaction enthalpies and reaction entropies" # noqa: E501

delta_activation_mass = rx.get_delta(scheme.B, molecular_masses)
delta_activation_energies = rx.get_delta(scheme.B, energies)
Expand All @@ -396,7 +398,7 @@ def _yield_thermochemistry(self):
* rx.get_reaction_entropies(
scheme.B, temperature=self.temperature, pressure=self.pressure
),
), "activation free energies do not match activation enthalpies and activation entropies"
), "activation free energies do not match activation enthalpies and activation entropies" # noqa: E501

circ_table = Table(
Column("no", justify="right"),
Expand Down Expand Up @@ -471,7 +473,7 @@ def _yield_thermochemistry(self):
yield circ_table
yield dagger_table

def _yield_kinetics(self):
def _yield_kinetics(self): # noqa: ANN101, C901
"""Produce a renderables describing the kinetics of the system.
This is meant to be used from within `__rich_console__`.
Expand Down Expand Up @@ -585,7 +587,7 @@ def _yield_kinetics(self):
f"{self.bias} J/mol."
)
yield Markdown(
"For **half-equilibria**, only ratios make sense: in simulations, **equilibria will be adjusted to be faster than all other reactions**."
"For **half-equilibria**, only ratios make sense: in simulations, **equilibria will be adjusted to be faster than all other reactions**." # noqa: E501
)

if self.concentrations is not None and self.concentrations:
Expand Down Expand Up @@ -657,7 +659,7 @@ def _yield_kinetics(self):
for i, name in enumerate(scheme.compounds):
if not rx.is_transition_state(name):
res = minimize_scalar(
lambda t: -r(t)[i],
lambda t: -r(t)[i], # noqa: B023
bounds=(y.t_min, (t_max + y.t_max) / 2),
method="bounded",
)
Expand Down Expand Up @@ -716,8 +718,9 @@ def _prepare_simulation(scheme, k, concentrations):
try:
quantity = float(quantity)
except (IndexError, ValueError):
raise ValueError(
"badly formatted concentrations: " f"'{' '.join(concentrations)}'"
raise ValueError( # noqa: B904
"badly formatted concentrations: " # noqa: EM102
f"'{' '.join(concentrations)}'" # noqa: RUF100
)

d[name] = quantity
Expand Down Expand Up @@ -754,7 +757,7 @@ def main(arguments=None):
)
parser.add_argument(
"path",
help="path to a source (`.k`) or compiled (`.jk`) model input file (if a source "
help="path to a source (`.k`) or compiled (`.jk`) model input file (if a source " # noqa: E501
"input file is given, but there is a compiled file available, the compiled "
"file will be used; use --compile|-c to force recompilation of the "
"source input file instead)",
Expand Down Expand Up @@ -792,9 +795,7 @@ def main(arguments=None):
"'active' species only (i.e., the ones that actually change "
"concentration during the simulation) or a single compound name (e.g. "
"'NH3(w)')",
# TODO(schneiderfelipe): validate inputs to avoid "ValueError:
# tuple.index(x): x not in tuple"
# choices=["active", "all", "none"],
# TODO(schneiderfelipe): validate inputs to avoid ValueError?
default="none",
)
parser.add_argument(
Expand Down Expand Up @@ -831,7 +832,8 @@ def main(arguments=None):
parser.add_argument(
"-p",
"--pressure",
help="set working pressure (in pascals) to be used in " "thermochemistry",
help="set working pressure (in pascals) to be used in "
"thermochemistry", # noqa: RUF100
type=float,
default=constants.atm,
)
Expand Down Expand Up @@ -944,4 +946,6 @@ def main(arguments=None):


if __name__ == "__main__":
# TODO: catch exceptions here and print a nice error
# (we can reraise them later if we want).
main()
55 changes: 37 additions & 18 deletions overreact/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@
"""

import numpy as np
from scipy.constants import N_A # noqa: F401
from scipy.constants import R # noqa: F401
from scipy.constants import angstrom # noqa: F401
from scipy.constants import atm # noqa: F401
from scipy.constants import atomic_mass # noqa: F401
from scipy.constants import bar # noqa: F401
from scipy.constants import c # noqa: F401
from scipy.constants import calorie # noqa: F401
from scipy.constants import centi # noqa: F401
from scipy.constants import eV # noqa: F401
from scipy.constants import giga # noqa: F401
from scipy.constants import h # noqa: F401
from scipy.constants import hbar # noqa: F401
from scipy.constants import k # noqa: F401
from scipy.constants import kilo # noqa: F401
from scipy.constants import liter # noqa: F401
from scipy.constants import torr # noqa: F401
from scipy.constants import physical_constants
from scipy.constants import (
N_A,
R,
angstrom,
atm,
atomic_mass,
bar,
c,
calorie,
centi,
eV,
giga,
h,
hbar,
k,
kilo,
liter,
physical_constants,
torr,
)

__all__ = [
"N_A",
"R",
"angstrom",
"atm",
"atomic_mass",
"bar",
"c",
"eV",
"giga",
"h",
"hbar",
"k",
"liter",
"torr",
]

hartree, _, _ = physical_constants["Hartree energy"]
bohr, _, _ = physical_constants["Bohr radius"]
Expand Down
2 changes: 1 addition & 1 deletion overreact/_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
if __name__ == "__main__":
for name in logfiles:
for compound in logfiles[name]:
print(name, compound, logfiles[name][compound].logfile)
print(name, compound, logfiles[name][compound].logfile) # noqa: T201
Loading

0 comments on commit cddc09d

Please sign in to comment.