From aa6baf12800493de3ffedcf79ef4335b56e4d35f Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 11:20:26 -0400 Subject: [PATCH 01/10] Dict fully updated w/ edits --- automol/__init__.py | 2 +- automol/util/dict_/_dict_.py | 49 ++++++++++++++++++++++++++---------- automol/util/dict_/multi.py | 31 ++++++++++++----------- docs/source/automol/util.md | 8 ++++++ lint.sh | 1 + 5 files changed, 63 insertions(+), 28 deletions(-) diff --git a/automol/__init__.py b/automol/__init__.py index ba94f3bb..e3695b80 100644 --- a/automol/__init__.py +++ b/automol/__init__.py @@ -14,7 +14,7 @@ - util - error - mult - - formula + - form - inchi_key - vmat - prop diff --git a/automol/util/dict_/_dict_.py b/automol/util/dict_/_dict_.py index 0b4079fe..682dc152 100644 --- a/automol/util/dict_/_dict_.py +++ b/automol/util/dict_/_dict_.py @@ -1,7 +1,7 @@ """Helper functions for working with Python dictionaries.""" import itertools -from collections.abc import Callable +from collections.abc import Callable, Sequence from copy import deepcopy from typing import Any @@ -69,7 +69,7 @@ def by_value(dct: dict[object, object], func: Callable = lambda x: x): :param func: Function :return: Dictionary with keys. """ # noqa: D401 - keys = keys_by_value(dct, func) + keys: Sequence[object] = keys_by_value(dct, func) return by_key(dct, keys) @@ -77,7 +77,7 @@ def values_by_key(dct: dict[object, object], keys: object, fill_val: object = No """Return dictionary values for specific keys, filling missing entries. :param dct: Dictionary of values :param keys: Keys for entries - :param fill_val: Values to fill the missing keys + :param fill_val: Fill where value is none :return: Dictionary with new keys. """ return tuple(dct[key] if key in dct else fill_val for key in keys) @@ -100,7 +100,7 @@ def value_in_floatkey_dct(dct: dict[object, object], key: object, tol=1.0e-5): decimal places or strings. :param dct: Dictionary of values :param keys: Keys for entries - :param tol: Tolerance for the floats + :param tol: Tolerance 1.0e-5 :return: Dictionary with new values. """ if isinstance(key, float): @@ -153,34 +153,57 @@ def transform_keys(dct: dict[object, object], func: Callable = lambda x: x): def transform_values(dct: dict[object, object], func: Callable = lambda x: x): - """Apply a function to each value.""" + """Apply a function to each value. + :param dct: Dictionary + :param func: Callable function + :return: New values in a dictionary. + """ return dict[object, object](zip(dct.keys(), map(func, dct.values()), strict=False)) def transform_items_to_values(dct: dict[object, object], func: Callable = lambda x: x): - """Apply a function to each value.""" + """Apply a function to each value. + :param dct: Dictionary + :param func: Callable function + :return: Dictionary with new values. + """ return dict[object, object]( zip(dct.keys(), itertools.starmap(func, dct.items()), strict=False) ) def keys_sorted_by_value(dct: dict[object, object]): - """Dictionary keys sorted by their associated values.""" # noqa: D401 + """Dictionary keys sorted by their associated values. + :param dct: Dictionary + :param func: Callable function + :return:Dictionary with sorted values. + """ # noqa: D401 return tuple(key for key, _ in sorted(dct.items(), key=lambda x: x[1])) def values_sorted_by_key(dct: dict[object, object]): - """Dictionary values sorted by their associated keys.""" # noqa: D401 + """Dictionary values sorted by their associated keys. + :param dct: Dictionary + :return: Dictionary with sorted values. + """ # noqa: D401 return tuple(val for _, val in sorted(dct.items())) def filter_by_key(dct: dict[object, object], func: Callable = lambda x: x): - """Filter dictionary entries by their values.""" + """Filter dictionary entries by their values. + :param dct: Dictionary + :param func: Callable function + :return:Dictionary with sorted values. + """ return {key: val for key, val in dct.items() if func(key)} def filter_by_value(dct: dict[object, object], func: Callable = lambda x: x): - """Filter dictionary entries by their values.""" + """Filter dictionary entries by their values. + :param dct: Dictionary + :param func: Callable function + :return:Dictionary with sorted values. + """ return {key: val for key, val in dct.items() if func(val)} @@ -190,8 +213,8 @@ def filter_keys( """Given two dictionaries (dct1 bigger dct2), filter out from 1 all the entries present in 2. - :param dct1: - :param dct2: + :param dct1: First dictionary + :param dct2: Second dictionary :return: filtered dct1 """ dct_ret = deepcopy(dct_1) @@ -207,7 +230,7 @@ def filter_keys( return dct_ret -def merge_sequence(dcts: dict[object, object]): +def merge_sequence(dcts: Sequence[dict[object, object]]): """Merge a sequence of dictionaries. :param dcts: Dictionary to merge :return: Merged dictionaries. diff --git a/automol/util/dict_/multi.py b/automol/util/dict_/multi.py index ed9b4ca2..e85dbe79 100644 --- a/automol/util/dict_/multi.py +++ b/automol/util/dict_/multi.py @@ -1,39 +1,42 @@ -""" multi-valued dictionary helpers +"""multi-valued dictionary helpers. dictionary values must all be tuples of the same length """ + from collections.abc import Mapping as _Mapping from ._dict_ import transform_values as _transform_values from ._dict_ import values_by_key as _values_by_key +MultiDict = dict[object, tuple[object, ...]] + -def is_multidict(mdct): - """ is this a multi-valued dictionary? - """ +def is_multidict(mdct: MultiDict) -> bool: + """Is this a multi-valued dictionary?.""" assert isinstance(mdct, _Mapping) vals = mdct.values() - return (not vals or all(isinstance(val, tuple) for val in vals) and - len(set(map(len, vals))) == 1) + return ( + not vals + or all(isinstance(val, tuple) for val in vals) + and len(set(map(len, vals))) == 1 + ) -def by_key_by_position(mdct, keys, pos): - """ position values, as a dictionary with these keys - """ +def by_key_by_position(mdct, keys, pos) -> dict[object, object]: + """Position values, as a dictionary with these keys.""" assert is_multidict(mdct) assert set(keys) <= set(mdct.keys()) dct = {} if keys: keys = list(keys) vals = [row[pos] for row in _values_by_key(mdct, keys)] - dct = dict(zip(keys, vals)) + dct = dict(zip(keys, vals, strict=True)) return dct -def set_by_key_by_position(mdct, dct, pos): - """ set values by position and key - """ +def set_by_key_by_position(mdct, dct, pos) -> MultiDict: + """Set values by position and key.""" assert is_multidict(mdct) assert set(dct.keys()) <= set(mdct.keys()) if dct: @@ -45,7 +48,7 @@ def set_by_key_by_position(mdct, dct, pos): def _unfreeze(mdct): - return dict(zip(mdct.keys(), map(list, mdct.values()))) + return dict(zip(mdct.keys(), map(list, mdct.values()), strict=True)) def _freeze(mdct): diff --git a/docs/source/automol/util.md b/docs/source/automol/util.md index c419defb..1328fa43 100644 --- a/docs/source/automol/util.md +++ b/docs/source/automol/util.md @@ -25,4 +25,12 @@ some words ## automol.util.vector ```{eval-rst} .. automodule:: automol.util.vector +``` +## automol.util.dict_ +```{eval-rst} +.. automodule:: automol.util.dict_ +``` +## automol.util.dict_.multi +```{eval-rst} +.. automodule:: automol.util.dict_.multi ``` \ No newline at end of file diff --git a/lint.sh b/lint.sh index 35adb965..c8c7852e 100755 --- a/lint.sh +++ b/lint.sh @@ -12,6 +12,7 @@ FILES=( "automol/util/vector.py" "automol/util/zmat.py" "automol/util/dict_/_dict_.py" + "automol/util/dict_/multi.py" ) ( From 5603c8ff59d90e87d87b5a4048eeae6e5d3d0015 Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 11:39:51 -0400 Subject: [PATCH 02/10] Multi.py updated with comments and class types --- automol/util/dict_/_dict_.py | 3 ++- automol/util/dict_/multi.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/automol/util/dict_/_dict_.py b/automol/util/dict_/_dict_.py index 682dc152..5d991eb9 100644 --- a/automol/util/dict_/_dict_.py +++ b/automol/util/dict_/_dict_.py @@ -44,13 +44,14 @@ def compose( def by_key( dct: dict[object, object], - keys, + keys: object, fill: bool = True, fill_val: bool | None = None, ) -> dict[object, object]: """Dictionary on a set of keys, filling missing entries. :param fill: Fill in missing values + :param keys: Keys for missing values :param fill_val: If `fill` is `True`, fill missing entries with this value. :return: 'fill; or dictionary with filled values """ # noqa: D401 diff --git a/automol/util/dict_/multi.py b/automol/util/dict_/multi.py index e85dbe79..0cd5a946 100644 --- a/automol/util/dict_/multi.py +++ b/automol/util/dict_/multi.py @@ -13,7 +13,10 @@ def is_multidict(mdct: MultiDict) -> bool: - """Is this a multi-valued dictionary?.""" + """Is this a multi-valued dictionary?. + :param mdict: Dictionary + :return: True if it is a multi-valued dictionary, False if not. + """ assert isinstance(mdct, _Mapping) vals = mdct.values() return ( @@ -23,8 +26,12 @@ def is_multidict(mdct: MultiDict) -> bool: ) -def by_key_by_position(mdct, keys, pos) -> dict[object, object]: - """Position values, as a dictionary with these keys.""" +def by_key_by_position(mdct: MultiDict, keys: object, pos: int) -> dict[object, object]: + """Position values, as a dictionary with these keys. + :param mdct: Multi-valued dictionary + :param keys: Keys for sorting + :param pos: Position to select column for dictionary. + """ assert is_multidict(mdct) assert set(keys) <= set(mdct.keys()) dct = {} @@ -35,8 +42,13 @@ def by_key_by_position(mdct, keys, pos) -> dict[object, object]: return dct -def set_by_key_by_position(mdct, dct, pos) -> MultiDict: - """Set values by position and key.""" +def set_by_key_by_position(mdct: MultiDict, dct: dict, pos: int) -> MultiDict: + """Set values by position and key. + :param mdct: Multi-valued dictionary + :param dct: Dictionary + :param pos:Position to select column for dictionary. + :return: New multi-valued dictionary set by key and position. + """ assert is_multidict(mdct) assert set(dct.keys()) <= set(mdct.keys()) if dct: From 897d90d69ebc3fe3ee84942882cc518f7a62136b Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 11:42:48 -0400 Subject: [PATCH 03/10] Index includes form.md --- docs/source/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/index.md b/docs/source/index.md index c3c411f9..741681d8 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -6,4 +6,5 @@ Welcome to my documentation website... :hidden: automol/inchi_key.md automol/util.md +automol/form.md ``` From 9a6410829c8721d3883ea36db1565443190bd221 Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 11:47:43 -0400 Subject: [PATCH 04/10] Make form.md --- docs/source/automol/form.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/source/automol/form.md diff --git a/docs/source/automol/form.md b/docs/source/automol/form.md new file mode 100644 index 00000000..edfb9132 --- /dev/null +++ b/docs/source/automol/form.md @@ -0,0 +1,5 @@ +## automol.util +some words +```{eval-rst} +.. automodule:: automol.util +``` \ No newline at end of file From f3140bdc330b88a76c9c732c929b97b66451f10d Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 12:13:30 -0400 Subject: [PATCH 05/10] Some edits to form --- automol/form/_form.py | 65 +++++++++++++++---------------------- docs/source/automol/form.md | 4 +-- lint.sh | 1 + 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/automol/form/_form.py b/automol/form/_form.py index 2fbb2888..2a8494a5 100644 --- a/automol/form/_form.py +++ b/automol/form/_form.py @@ -1,11 +1,10 @@ -""" Library for dealing with molecular formula, - represented as dict[atom symbol: atom number] +"""Library for dealing with molecular formula, +represented as dict[atom symbol: atom number]. """ import collections import functools import itertools -from typing import List import pyparsing as pp from pyparsing import pyparsing_common as ppc @@ -20,14 +19,12 @@ FORMULA = pp.OneOrMore(ATOM_COUNT) -def electron_count(fml): +def electron_count(fml:dict[str:int])->int: """Count the number of electrons for the atoms in a molecular formula. - :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :rtype: int + :param fml: Stochiometric chemical formula + :return: Number of electrons. """ - assert _is_standard(fml) elec_count = 0 @@ -38,56 +35,52 @@ def electron_count(fml): return elec_count -def atom_count(fml): +def atom_count(fml:dict[str:int])->int: """Count the number of atoms in this molecular formula. - :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :rtype: int + :param fml: Stochiometric chemical formula + :return: Number of atoms. """ - assert _is_standard(fml) return sum(fml.values()) -def heavy_atom_count(fml): +def heavy_atom_count(fml:dict[str:int])->int: """Count the number of heavy atoms in this molecular formula. - :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :rtype: int + :param fml: Stochiometric chemical formula + :return: Number of heavy atoms """ assert _is_standard(fml) fml = without(fml, ["H"]) return sum(fml.values()) -def element_count(fml, symb): +def element_count(fml:dict[str:int], symb:str)-> int: """Count the number of a given element in this molecular formula. - :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :param symb: atomic symbol of element to be counted - :type symb: str - :rtype: int + :param fml: Stochiometric chemical formula + :param symb: Atomic symbol of element to be counted + :return: Number of a certain element in the formula """ - assert _is_standard(fml) return fml[symb] if symb in fml else 0 def without(fml: dict[str, int], symbs: tuple = ()) -> dict[str, int]: - """Return a formula without hydrogen + """Return a formula without hydrogen. - :param fml: chemical formula, without hydrogen + :param fml: Chemical formula, without hydrogen + :symbs: Chemical symbols + :return: Dictionary with new formula, without hydrogen """ return {k: v for k, v in fml.items() if k not in symbs} def match(fml1: dict[str, int], fml2: dict[str, int]) -> bool: - """Check for a match between two formulas, allowing wildcard values + """Check for a match between two formulas, allowing wildcard values. A stoichiometry of -1 indicates a wildcard value @@ -105,7 +98,7 @@ def match(fml1: dict[str, int], fml2: dict[str, int]) -> bool: def add_element(fml, symb, num=1): - """add or subtract (if num < 0) this element from the molecular formula + """Add or subtract (if num < 0) this element from the molecular formula. :param fml: stochiometric chemical formula :type fml: dict[str:int] @@ -115,7 +108,6 @@ def add_element(fml, symb, num=1): :type num: int :rtype: dict[str:int] """ - assert ptab.to_number(symb) assert _is_standard(fml) @@ -140,7 +132,6 @@ def join(fml1, fml2): :type fml2: dict[str:int] :rtype: int """ - fml = dict(fml1) for symb, num in fml2.items(): fml = add_element(fml, symb, num=num) @@ -149,18 +140,17 @@ def join(fml1, fml2): def join_sequence(fmls): - """Join a sequence of formulas together: + """Join a sequence of formulas together. :param fml: stochiometric chemical formula :type fml: dict[str:int] - :rtype: int + :rtype: int. """ - return functools.reduce(join, fmls) -def sorted_symbols_in_sequence(fmls: List[dict]) -> List[dict]: - """Sort a sequence of formulas based on Hill-sorting +def sorted_symbols_in_sequence(fmls: list[dict]) -> list[dict]: + """Sort a sequence of formulas based on Hill-sorting. :param fmls: A sequence of formulas :type fmls: List[dict] @@ -181,7 +171,6 @@ def string(fml, hyd=True): :type hyd: bool :rtype: str """ - fml_lst = [ (symb, fml[symb]) for symb in sorted_symbols(fml.keys()) if symb != "H" or hyd ] @@ -201,7 +190,6 @@ def string2(fml): :type fml: dict[str:int] :rtype: str """ - fml = collections.OrderedDict(sorted(fml.items())) fml_str = "".join(map(str, itertools.chain.from_iterable(fml.items()))) @@ -288,7 +276,7 @@ def _sort_key(entry): def sort_vector(fml, symbs=None): - """Generate a sort vector for sorting various formulas against each other + """Generate a sort vector for sorting various formulas against each other. :param fml_str: stochiometric chemical formula string :type fml_str: str @@ -309,7 +297,6 @@ def _is_standard(fml): :type fml: dict[str:int] :rtype: bool """ - symbs = list(fml.keys()) return symbs == list(filter(ptab.to_number, map(ptab.to_symbol, symbs))) diff --git a/docs/source/automol/form.md b/docs/source/automol/form.md index edfb9132..67a78f6b 100644 --- a/docs/source/automol/form.md +++ b/docs/source/automol/form.md @@ -1,5 +1,5 @@ -## automol.util +## automol.form some words ```{eval-rst} -.. automodule:: automol.util +.. automodule:: automol.form ``` \ No newline at end of file diff --git a/lint.sh b/lint.sh index c8c7852e..700a88bb 100755 --- a/lint.sh +++ b/lint.sh @@ -13,6 +13,7 @@ FILES=( "automol/util/zmat.py" "automol/util/dict_/_dict_.py" "automol/util/dict_/multi.py" + "automol/form/_form.py" ) ( From d1dee14ce0f9791b11e2ca27d2051d70aec4494b Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 12:14:57 -0400 Subject: [PATCH 06/10] Linting --- automol/form/_form.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/automol/form/_form.py b/automol/form/_form.py index 2a8494a5..a1f97c6e 100644 --- a/automol/form/_form.py +++ b/automol/form/_form.py @@ -19,7 +19,7 @@ FORMULA = pp.OneOrMore(ATOM_COUNT) -def electron_count(fml:dict[str:int])->int: +def electron_count(fml: dict[str:int]) -> int: """Count the number of electrons for the atoms in a molecular formula. :param fml: Stochiometric chemical formula @@ -35,7 +35,7 @@ def electron_count(fml:dict[str:int])->int: return elec_count -def atom_count(fml:dict[str:int])->int: +def atom_count(fml: dict[str:int]) -> int: """Count the number of atoms in this molecular formula. :param fml: Stochiometric chemical formula @@ -46,7 +46,7 @@ def atom_count(fml:dict[str:int])->int: return sum(fml.values()) -def heavy_atom_count(fml:dict[str:int])->int: +def heavy_atom_count(fml: dict[str:int]) -> int: """Count the number of heavy atoms in this molecular formula. :param fml: Stochiometric chemical formula @@ -57,7 +57,7 @@ def heavy_atom_count(fml:dict[str:int])->int: return sum(fml.values()) -def element_count(fml:dict[str:int], symb:str)-> int: +def element_count(fml: dict[str:int], symb: str) -> int: """Count the number of a given element in this molecular formula. :param fml: Stochiometric chemical formula From 4f9c266c835caf2ea5bc6b15f3afcdf3af8c33e3 Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 13:43:28 -0400 Subject: [PATCH 07/10] form with new syntax --- automol/form/_form.py | 84 +++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/automol/form/_form.py b/automol/form/_form.py index a1f97c6e..474814e3 100644 --- a/automol/form/_form.py +++ b/automol/form/_form.py @@ -5,6 +5,7 @@ import collections import functools import itertools +from _collections_abc import Sequence import pyparsing as pp from pyparsing import pyparsing_common as ppc @@ -97,16 +98,13 @@ def match(fml1: dict[str, int], fml2: dict[str, int]) -> bool: return fml1 == fml2 -def add_element(fml, symb, num=1): +def add_element(fml:dict[str:int], symb:str, num:int=1)-> dict[str:int]: """Add or subtract (if num < 0) this element from the molecular formula. - :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :param symb: atomic symbol of element to be added - :type symb: str - :param num: number of the element to add to the formula - :type num: int - :rtype: dict[str:int] + :param fml: Stochiometric chemical formula + :param symb: Atomic symbol of element to be added + :param num: Number of the element to add to the formula + :return: Formula with added element """ assert ptab.to_number(symb) assert _is_standard(fml) @@ -123,14 +121,12 @@ def add_element(fml, symb, num=1): return fml -def join(fml1, fml2): +def join(fml1:dict[str:int], fml2:dict[str:int])->int: """Join two formulas together. - :param fml1: stochiometric chemical formula 1 - :type fml1: dict[str:int] - :param fml2: stochiometric chemical formula 2 - :type fml2: dict[str:int] - :rtype: int + :param fml1: Stochiometric chemical formula 1 + :param fml2: Stochiometric chemical formula 2 + :return: Formula with the sum of both formulas """ fml = dict(fml1) for symb, num in fml2.items(): @@ -139,12 +135,11 @@ def join(fml1, fml2): return fml -def join_sequence(fmls): +def join_sequence(fmls:dict[str:int])->int: """Join a sequence of formulas together. - :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :rtype: int. + :param fml: Stochiometric chemical formula + :return: Sum of the formulas """ return functools.reduce(join, fmls) @@ -153,23 +148,19 @@ def sorted_symbols_in_sequence(fmls: list[dict]) -> list[dict]: """Sort a sequence of formulas based on Hill-sorting. :param fmls: A sequence of formulas - :type fmls: List[dict] :return: The same sequence, but sorted - :rtype: List[dict] """ return sorted_symbols(join_sequence(fmls).keys()) # Str<->Dict Converters -def string(fml, hyd=True): +def string(fml:dict[str:int], hyd:bool=True)->str: """Convert formula dictionary to formula string in the Hill convention. Resultant string is identical to InChI formula string. :param fml: stochiometric chemical formula - :type fml: dict[str:int] :param hyd: include hydrogens? - :type hyd: bool - :rtype: str + :return: True if formula includes hydrogen, False if no hydrogen """ fml_lst = [ (symb, fml[symb]) for symb in sorted_symbols(fml.keys()) if symb != "H" or hyd @@ -182,13 +173,12 @@ def string(fml, hyd=True): return fml_str -def string2(fml): +def string2(fml:dict[str:int])->str: """Convert formula dictionary to formula string that includes 1s in when there is only one atom. :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :rtype: str + :return: Formula which includes 1s when there is only one atom """ fml = collections.OrderedDict(sorted(fml.items())) @@ -211,17 +201,14 @@ def from_string(fml_str: str) -> dict[str, int]: return fml -def sorted_symbols(seq, symbs_first=("C", "H"), symbs_last=()): +def sorted_symbols(seq:Sequence, symbs_first:Sequence=("C", "H"), symbs_last=())->Sequence: """Produce a sorted list of atomic symbols; some elements given priority. By default, C placed first, then H, then others in alphabetical order. - :param seq: formula or sequence of atomic symbols - :type seq: dict, list, or tuple - :param symbs_first: atomic symbols to place first - :type symbs_first: sequence of strings - :param symbs_last: atomic symbols to place last - :type symbs_last: sequence of strings - :rtyp: tuple(str) + :param seq: Formula or sequence of atomic symbols + :param symbs_first: Atomic symbols to place first + :param symbs_last: Atomic symbols to place last + :return: Sorted list """ def _sort_key(char): @@ -236,18 +223,15 @@ def _sort_key(char): return tuple(sorted(seq, key=_sort_key)) -def argsort_symbols(seq, symbs_first=("C", "H"), symbs_last=(), idx=None): +def argsort_symbols(seq:Sequence, symbs_first:Sequence=("C", "H"), + symbs_last:Sequence=(), idx:int | None=None)-> tuple(int): # type: ignore """Determine the sort order for a sequence of atomic symbols. - :param seq: formula or sequence of atomic symbols - :type seq: dict, list, or tuple - :param symbs_first: atomic symbols to place first - :type symbs_first: sequence of strings - :param symbs_last: atomic symbols to place last - :type symbs_last: sequence of strings - :param idx: index of symbol for sorting - :type idx: int - :rtype: tuple(int) + :param seq: Formula or sequence of atomic symbols + :param symbs_first: Atomic symbols to place first + :param symbs_last: Atomic symbols to place last + :param idx: Index of symbol for sorting + :return: Sorted syymboles """ def _sort_key(entry): @@ -275,13 +259,12 @@ def _sort_key(entry): ) -def sort_vector(fml, symbs=None): +def sort_vector(fml:str, symbs:list[str]=None): """Generate a sort vector for sorting various formulas against each other. :param fml_str: stochiometric chemical formula string - :type fml_str: str :param symbs: atomic symbols in the desired sort order (optional) - :type symbs: list[str] + :return: Sorted vector """ if symbs is None: symbs = sorted_symbols(fml.keys()) @@ -290,12 +273,11 @@ def sort_vector(fml, symbs=None): return vec -def _is_standard(fml): +def _is_standard(fml:dict[str:int])-> bool: """Assess if the formula conforms to the standard form. :param fml: stochiometric chemical formula - :type fml: dict[str:int] - :rtype: bool + :return: True if formula is in standard form, false if not """ symbs = list(fml.keys()) From ec997df68d774f0081ae01a0ced93f66f67505a4 Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 13:44:04 -0400 Subject: [PATCH 08/10] linted form --- automol/form/_form.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/automol/form/_form.py b/automol/form/_form.py index 474814e3..491e9e72 100644 --- a/automol/form/_form.py +++ b/automol/form/_form.py @@ -5,9 +5,9 @@ import collections import functools import itertools -from _collections_abc import Sequence import pyparsing as pp +from _collections_abc import Sequence from pyparsing import pyparsing_common as ppc from phydat import ptab @@ -98,7 +98,7 @@ def match(fml1: dict[str, int], fml2: dict[str, int]) -> bool: return fml1 == fml2 -def add_element(fml:dict[str:int], symb:str, num:int=1)-> dict[str:int]: +def add_element(fml: dict[str:int], symb: str, num: int = 1) -> dict[str:int]: """Add or subtract (if num < 0) this element from the molecular formula. :param fml: Stochiometric chemical formula @@ -121,7 +121,7 @@ def add_element(fml:dict[str:int], symb:str, num:int=1)-> dict[str:int]: return fml -def join(fml1:dict[str:int], fml2:dict[str:int])->int: +def join(fml1: dict[str:int], fml2: dict[str:int]) -> int: """Join two formulas together. :param fml1: Stochiometric chemical formula 1 @@ -135,7 +135,7 @@ def join(fml1:dict[str:int], fml2:dict[str:int])->int: return fml -def join_sequence(fmls:dict[str:int])->int: +def join_sequence(fmls: dict[str:int]) -> int: """Join a sequence of formulas together. :param fml: Stochiometric chemical formula @@ -154,7 +154,7 @@ def sorted_symbols_in_sequence(fmls: list[dict]) -> list[dict]: # Str<->Dict Converters -def string(fml:dict[str:int], hyd:bool=True)->str: +def string(fml: dict[str:int], hyd: bool = True) -> str: """Convert formula dictionary to formula string in the Hill convention. Resultant string is identical to InChI formula string. @@ -173,7 +173,7 @@ def string(fml:dict[str:int], hyd:bool=True)->str: return fml_str -def string2(fml:dict[str:int])->str: +def string2(fml: dict[str:int]) -> str: """Convert formula dictionary to formula string that includes 1s in when there is only one atom. @@ -201,7 +201,9 @@ def from_string(fml_str: str) -> dict[str, int]: return fml -def sorted_symbols(seq:Sequence, symbs_first:Sequence=("C", "H"), symbs_last=())->Sequence: +def sorted_symbols( + seq: Sequence, symbs_first: Sequence = ("C", "H"), symbs_last=() +) -> Sequence: """Produce a sorted list of atomic symbols; some elements given priority. By default, C placed first, then H, then others in alphabetical order. @@ -223,8 +225,12 @@ def _sort_key(char): return tuple(sorted(seq, key=_sort_key)) -def argsort_symbols(seq:Sequence, symbs_first:Sequence=("C", "H"), - symbs_last:Sequence=(), idx:int | None=None)-> tuple(int): # type: ignore +def argsort_symbols( + seq: Sequence, + symbs_first: Sequence = ("C", "H"), + symbs_last: Sequence = (), + idx: int | None = None, +) -> tuple(int): # type: ignore """Determine the sort order for a sequence of atomic symbols. :param seq: Formula or sequence of atomic symbols @@ -259,7 +265,7 @@ def _sort_key(entry): ) -def sort_vector(fml:str, symbs:list[str]=None): +def sort_vector(fml: str, symbs: list[str] | None = None): """Generate a sort vector for sorting various formulas against each other. :param fml_str: stochiometric chemical formula string @@ -273,7 +279,7 @@ def sort_vector(fml:str, symbs:list[str]=None): return vec -def _is_standard(fml:dict[str:int])-> bool: +def _is_standard(fml: dict[str:int]) -> bool: """Assess if the formula conforms to the standard form. :param fml: stochiometric chemical formula From ae067822e6d01ac0ae7f147e560f4bcccfb96689 Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 13:49:21 -0400 Subject: [PATCH 09/10] Changed seq to seq[str] in form --- automol/form/_form.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/automol/form/_form.py b/automol/form/_form.py index 491e9e72..f12b5f67 100644 --- a/automol/form/_form.py +++ b/automol/form/_form.py @@ -202,7 +202,9 @@ def from_string(fml_str: str) -> dict[str, int]: def sorted_symbols( - seq: Sequence, symbs_first: Sequence = ("C", "H"), symbs_last=() + seq: Sequence, + symbs_first: Sequence[str] = ("C", "H"), + symbs_last: Sequence[str] = (), ) -> Sequence: """Produce a sorted list of atomic symbols; some elements given priority. By default, C placed first, then H, then others in alphabetical order. @@ -227,8 +229,8 @@ def _sort_key(char): def argsort_symbols( seq: Sequence, - symbs_first: Sequence = ("C", "H"), - symbs_last: Sequence = (), + symbs_first: Sequence[str] = ("C", "H"), + symbs_last: Sequence[str] = (), idx: int | None = None, ) -> tuple(int): # type: ignore """Determine the sort order for a sequence of atomic symbols. From edc0d915b463d32eef9a74579550a071ec09f63c Mon Sep 17 00:00:00 2001 From: Rosalba Date: Tue, 30 Jul 2024 13:57:26 -0400 Subject: [PATCH 10/10] edited form --- automol/form/_form.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/automol/form/_form.py b/automol/form/_form.py index f12b5f67..094ecdb4 100644 --- a/automol/form/_form.py +++ b/automol/form/_form.py @@ -232,7 +232,7 @@ def argsort_symbols( symbs_first: Sequence[str] = ("C", "H"), symbs_last: Sequence[str] = (), idx: int | None = None, -) -> tuple(int): # type: ignore +) -> tuple[str, ...]: """Determine the sort order for a sequence of atomic symbols. :param seq: Formula or sequence of atomic symbols