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

form and const formatting #550

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion automol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Level 1: No dependencies; no interdependencies

- const
- const
- util
- error
- mult
Expand Down
43 changes: 20 additions & 23 deletions automol/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ class ReactionClass(str, enum.Enum):
SUBSTITUTION = "substitution"

def __str__(self):
"""Construct a string from Reaction Class.
"""Convert a reaction class object to a string.

:return: String from self
:return: The reaction class string
"""
return self.value

def __repr__(self):
"""_summary_.
"""Get a string literal describing the reaction class object.

:return: _description_
:rtype: _type_
:return: The reaction class string literal
"""
return repr(self.value)

Expand Down Expand Up @@ -65,7 +64,7 @@ def reverse(cls, value: str):

@classmethod
def is_reversible(cls, value: str) -> bool:
"""Is this reaction class reversible?.
"""Whether this reaction class is reversible.

:param value: A reaction class
:return: `True` if it is, `False` if it isn't
Expand All @@ -74,7 +73,7 @@ def is_reversible(cls, value: str) -> bool:

@classmethod
def is_bimolecular(cls, value: str) -> bool:
"""Is this reaction class bimolecular?.
"""Whether this reaction class is bimolecular.

:param value: The reaction class
:return: `True` if it is, `False` if it isn't
Expand All @@ -90,7 +89,7 @@ def is_bimolecular(cls, value: str) -> bool:

@classmethod
def requires_spin_designation(cls, value: str) -> bool:
"""Is this a reaction class that requires a spin designation?.
"""Whether this is a reaction class that requires a spin designation.

:param value: The reaction class
:return: `True` if it is, `False` if it isn't
Expand All @@ -103,7 +102,7 @@ def requires_spin_designation(cls, value: str) -> bool:

@classmethod
def is_defined(cls, value: str) -> bool:
"""Is this reaction class defined?.
"""Whether this reaction class is defined.

:param value: The reaction class
:return: `True` if it is, `False` if it isn't
Expand All @@ -112,25 +111,23 @@ def is_defined(cls, value: str) -> bool:


class ReactionSpin(str, enum.Enum):
"""reaction spin types."""
"""Reaction spin types."""

LOW = "low-spin"
HIGH = "high-spin"
NONE = "unspecified spin"

def __str__(self):
"""_summary_.
"""Convert a reaction class object to a string.

:return: _description_
:rtype: _type_
:return: The reaction class string
"""
return self.value

def __repr__(self):
"""_summary_.
"""Get a string literal describing the reaction class object.

:return: _description_
:rtype: _type_
:return: The reaction class string literal
"""
return repr(self.value)

Expand Down Expand Up @@ -192,31 +189,31 @@ def reaction_spin(self) -> ReactionSpin:
return ReactionSpin(self.spin)

def is_radical_radical(self) -> bool:
"""Is this a radical radical reaction?."""
"""Whether this is a radical radical reaction."""
return self.is_rad_rad

def is_intersystem_crossing(self) -> bool:
"""Is this an intersystem crossing?."""
"""Whether this is an intersystem crossing."""
return self.is_isc

def is_barrierless(self) -> bool:
"""Is this a barrierless reaction?."""
"""Whether this is a barrierless reaction."""
return self.is_radical_radical() and not self.is_high_spin()

def is_low_spin(self) -> bool:
"""Is this a low-spin reaction?."""
"""WHether this is a low-spin reaction."""
return self.reaction_spin() == ReactionSpin.LOW

def is_high_spin(self) -> bool:
"""Is this a high-spin reaction?."""
"""Whether this is a high-spin reaction."""
return self.reaction_spin() == ReactionSpin.HIGH

def has_no_spin_designation(self) -> bool:
"""Is this the only possible spin-state?."""
"""Whether this is the only possible spin-state."""
return self.reaction_spin() == ReactionSpin.NONE

def requires_spin_designation(self) -> bool:
"""Is a spin designation required for this reaction?."""
"""Whether a spin designation is required for this reaction."""
spin_req_classes = (
ReactionClass.HYDROGEN_ABSTRACTION, # AVC: Why is this in here??
ReactionClass.ADDITION,
Expand Down
30 changes: 16 additions & 14 deletions automol/form/_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
STOICH = pp.Opt(pp.Literal("*") | ppc.integer).setParseAction(lambda x: x if x else [1])
ATOM_COUNT = pp.Group(SYMBOL + STOICH)
FORMULA = pp.OneOrMore(ATOM_COUNT)
Formul = dict[str, int]
Formula = dict[str, int]


def electron_count(fml: Formul) -> int:
def electron_count(fml: Formula) -> int:
"""Count the number of electrons for the atoms in a molecular formula.

:param fml: Stochiometric chemical formula
Expand All @@ -37,7 +37,7 @@ def electron_count(fml: Formul) -> int:
return elec_count


def atom_count(fml: Formul) -> int:
def atom_count(fml: Formula) -> int:
"""Count the number of atoms in this molecular formula.

:param fml: Stochiometric chemical formula
Expand All @@ -48,7 +48,7 @@ def atom_count(fml: Formul) -> int:
return sum(fml.values())


def heavy_atom_count(fml: Formul) -> int:
def heavy_atom_count(fml: Formula) -> int:
"""Count the number of heavy atoms in this molecular formula.

:param fml: Stochiometric chemical formula
Expand All @@ -59,7 +59,7 @@ def heavy_atom_count(fml: Formul) -> int:
return sum(fml.values())


def element_count(fml: Formul, symb: str) -> int:
def element_count(fml: Formula, symb: str) -> int:
"""Count the number of a given element in this molecular formula.

:param fml: Stochiometric chemical formula
Expand Down Expand Up @@ -99,7 +99,7 @@ def match(fml1: dict[str, int], fml2: dict[str, int]) -> bool:
return fml1 == fml2


def add_element(fml: Formul, symb: str, num: int = 1) -> Formul:
def add_element(fml: Formula, symb: str, num: int = 1) -> Formula:
"""Add or subtract (if num < 0) this element from the molecular formula.

:param fml: Stochiometric chemical formula
Expand All @@ -122,7 +122,7 @@ def add_element(fml: Formul, symb: str, num: int = 1) -> Formul:
return fml


def join(fml1: Formul, fml2: Formul) -> int:
def join(fml1: Formula, fml2: Formula) -> int:
"""Join two formulas together.

:param fml1: Stochiometric chemical formula 1
Expand All @@ -136,7 +136,7 @@ def join(fml1: Formul, fml2: Formul) -> int:
return fml


def join_sequence(fmls: Formul) -> int:
def join_sequence(fmls: Formula) -> int:
"""Join a sequence of formulas together.

:param fml: Stochiometric chemical formula
Expand All @@ -155,7 +155,7 @@ def sorted_symbols_in_sequence(fmls: list[dict]) -> list[dict]:


# Str<->Dict Converters
def string(fml: Formul, hyd: bool = True) -> str:
def string(fml: Formula, hyd: bool = True) -> str:
"""Convert formula dictionary to formula string in the Hill convention.
Resultant string is identical to InChI formula string.

Expand All @@ -174,7 +174,7 @@ def string(fml: Formul, hyd: bool = True) -> str:
return fml_str


def string2(fml: Formul) -> str:
def string2(fml: Formula) -> str:
"""Convert formula dictionary to formula string that includes 1s in when
there is only one atom.

Expand All @@ -188,7 +188,7 @@ def string2(fml: Formul) -> str:
return fml_str


def from_string(fml_str: str) -> dict[str, int]:
def from_string(fml_str: str) -> Formula:
"""Convert formula string to formula dictionary.

Wildcard values can be specified as, for example, 'O2H*', which will be interpreted
Expand All @@ -206,7 +206,7 @@ def sorted_symbols(
seq: Sequence,
symbs_first: Sequence[str] = ("C", "H"),
symbs_last: Sequence[str] = (),
) -> Sequence:
) -> tuple[str, ...]:
"""Produce a sorted list of atomic symbols; some elements given priority.
By default, C placed first, then H, then others in alphabetical order.

Expand Down Expand Up @@ -268,7 +268,9 @@ def _sort_key(entry):
)


def sort_vector(fml: str, symbs: list[str] | None = None):
def sort_vector(
fml: Sequence[str], symbs: Sequence[str] | None = None
) -> tuple[int, ...]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fml should be Sequence[Formula].

"""Generate a sort vector for sorting various formulas against each other.

:param fml_str: stochiometric chemical formula string
Expand All @@ -282,7 +284,7 @@ def sort_vector(fml: str, symbs: list[str] | None = None):
return vec


def _is_standard(fml: Formul) -> bool:
def _is_standard(fml: Formula) -> bool:
"""Assess if the formula conforms to the standard form.

:param fml: stochiometric chemical formula
Expand Down
6 changes: 3 additions & 3 deletions automol/form/reac.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from _collections_abc import Sequence

from ._form import Formul, add_element, join_sequence
from ._form import Formula, add_element, join_sequence


def is_valid_reaction(rct_fmls: Sequence[Formul], prd_fmls: Sequence[Formul]) -> bool:
def is_valid_reaction(rct_fmls: Sequence[Formula], prd_fmls: Sequence[Formula]) -> bool:
"""Use the formula to see if a reaction preserves stoichiometry.

:param rct_fmls: stoichiometries of the reactants
Expand All @@ -17,7 +17,7 @@ def is_valid_reaction(rct_fmls: Sequence[Formul], prd_fmls: Sequence[Formul]) ->


def argsort_hydrogen_abstraction(
rct_fmls: Sequence[Formul], prd_fmls: Sequence[Formul]
rct_fmls: Sequence[Formula], prd_fmls: Sequence[Formula]
) -> tuple[tuple[int, int], tuple[int, int]] | None:
"""Generate the indices which allows the reactants and products of
a hydrogen abstraction reaction can be sorted as RH + Q => R + QH.
Expand Down
Loading