Skip to content

Commit

Permalink
_util and ring updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalbam1 committed Jul 10, 2024
1 parent 12af840 commit 02141e7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
21 changes: 13 additions & 8 deletions automol/util/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def flatten(lst: Collection) -> Iterator:
"""Flatten an arbitrarily nested list of lists (iterator).
Source: https://stackoverflow.com/a/2158532 .
:param lst: An arbitrarily nested list or tuple
:return: An iterator over the flattened list of values
:return: An iterator over the flattened list of values.
"""
for elem in lst:
if isinstance(elem, Iterable) and not isinstance(elem, str | bytes):
Expand Down Expand Up @@ -195,18 +195,18 @@ def breakby(lst: Sequence, elem) -> tuple[tuple, tuple]:
Analogous to '<char>'.split('<string>') for strings.
:param lst: The list
:param elem: The element to break the list by, gets deleted
:return:The chunks between the break points of the input list
:return:The chunks between the break points of the input list.
"""
lsts = tuple(
tuple(g) for k, g in itertools.groupby(lst, lambda x: x == elem) if not k
)
return lsts


def separate_negatives(lst: Sequence)-> tuple [tuple,tuple]:
def separate_negatives(lst: Sequence) -> tuple[tuple, tuple]:
"""Seperate a list of numbers into negative and nonnegative (>= 0).
:param lst: The list
:return: Value for negatives and for positives.
:return: Value for negatives and for non-negatives.
"""
neg_lst = tuple(val for val in lst if val < 0)
pos_lst = tuple(val for val in lst if val >= 0)
Expand All @@ -228,9 +228,9 @@ def scale_iterable(
iterable: Collection[float], scale_factor: float
) -> Collection[float]:
"""Scale some type of iterable of floats by a scale factor.
:param iterable: A list of numbers.
:param iterable: A list of numbers.
:param scale_factor: A factor to scale by.
:return:
:return: The scaled list of numbers.
"""
if isinstance(iterable, list):
iterable = [val * scale_factor for val in iterable]
Expand All @@ -240,8 +240,13 @@ def scale_iterable(
return iterable


def remove_duplicates_with_order(lst: list):
"""Remove all duplicates of a list while not reordering the list."""
def remove_duplicates_with_order(lst: Sequence) -> Sequence:
"""Remove all duplicates of a list while not reordering the list.
:param lst: A list.
:return: A list, without duplicates.
Note: To be deprecated
and replaced with calls to more_itertools.unique_justseen.
"""
if isinstance(lst, list):
lst = [n for i, n in enumerate(lst) if n not in lst[:i]]
if isinstance(lst, tuple):
Expand Down
5 changes: 2 additions & 3 deletions automol/util/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ def string(mat: Sequence[Sequence[float]], val_format="{0:>8.3f}") -> str:
"""Write a matrix to a string.
:param mat: matrix to form string with
:type mat: tuple(tuple(float))
:param precision: number of integers past decimal
:type precision: int
:param val_format: A number-formatting string, such as "{:.3f}"
:return: Matrix as a string
"""
mat_str = ""
for row in mat:
Expand Down
10 changes: 0 additions & 10 deletions automol/util/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def cycle_item_to_front(
:param items: The ring items
:parm item: The item to cycle to the font
:param end_item: optionally, ensure that this is the last item in the ring
:type end_item: object
:returns: The ring items with `item` cycled to the front and `end_item` to the end
:rtype: List[int]
"""
items = cycle_items_to_front(items, [item])

Expand Down Expand Up @@ -135,11 +133,8 @@ def cycle_items_to_back(items: list[object], back_items: list[object]) -> list[o
"""Cycle ring items until a group of adjacent items is at the end of the list.
:param items: The ring items
:type items: List[object]
:parm back_items: The item to cycle to the end
:type back_items: List[object]
:returns: The ring items `back_items` cycled to the end
:rtype: List[int]
"""
nitems = len(items)

Expand All @@ -165,7 +160,6 @@ def cycle_to_split(
:param split_pair: The pair of items to split
:type split_pair: Tuple[object, object]
:return: The ring items with one item in the pair at the start and one at the end
:rtype: List[object]
"""
nitems = len(items)
split_pair = set(split_pair)
Expand Down Expand Up @@ -201,13 +195,9 @@ def cycle_to_optimal_split(
to the end of the list.
:param items: The ring items
:type items: List[object]
:param split_pairs: Pairs where the cycle can be split
:type split_pairs: List[Tuple[object, object]]
:param back_items: Adjacent items that should be as close to the end as possible
:type back_items: List[object]
:return: The cycle with the optimal split
:rtype: List[object]
"""
orig_items = items
split_pairs = list(split_pairs)
Expand Down

0 comments on commit 02141e7

Please sign in to comment.