Adds a __repr__ to graph.
[pyutils.git] / src / pyutils / list_utils.py
index 01bd76a72dfe9d2eeb9d4772b366b9c6f33b3f27..f0a4e826a28f27e57ee06acb9668499853dbd512 100644 (file)
@@ -1,13 +1,22 @@
 #!/usr/bin/env python3
 
 #!/usr/bin/env python3
 
-# © Copyright 2021-2022, Scott Gasch
+# © Copyright 2021-2023, Scott Gasch
 
 """This module contains helper functions for dealing with Python lists."""
 
 import random
 from collections import Counter
 from itertools import chain, combinations, islice
 
 """This module contains helper functions for dealing with Python lists."""
 
 import random
 from collections import Counter
 from itertools import chain, combinations, islice
-from typing import Any, Iterator, List, MutableSequence, Sequence, Tuple
+from typing import (
+    Any,
+    Generator,
+    Iterator,
+    List,
+    MutableSequence,
+    Sequence,
+    Tuple,
+    TypeVar,
+)
 
 
 def shard(lst: List[Any], size: int) -> Iterator[Any]:
 
 
 def shard(lst: List[Any], size: int) -> Iterator[Any]:
@@ -120,7 +129,7 @@ def population_counts(lst: Sequence[Any]) -> Counter:
     return Counter(lst)
 
 
     return Counter(lst)
 
 
-def most_common(lst: List[Any], *, count=1) -> Any:
+def most_common(lst: List[Any], *, count: int = 1) -> Any:
     """
     Return the N most common item in the list.
 
     """
     Return the N most common item in the list.
 
@@ -147,7 +156,7 @@ def most_common(lst: List[Any], *, count=1) -> Any:
     return remove_list_if_one_element([_[0] for _ in p.most_common()[0:count]])
 
 
     return remove_list_if_one_element([_[0] for _ in p.most_common()[0:count]])
 
 
-def least_common(lst: List[Any], *, count=1) -> Any:
+def least_common(lst: List[Any], *, count: int = 1) -> Any:
     """
     Return the N least common item in the list.
 
     """
     Return the N least common item in the list.
 
@@ -252,7 +261,10 @@ def transpose(lst: List[Any]) -> List[Any]:
     return [list(_) for _ in transposed]
 
 
     return [list(_) for _ in transposed]
 
 
-def ngrams(lst: Sequence[Any], n: int):
+T = TypeVar('T')
+
+
+def ngrams(lst: Sequence[T], n: int) -> Generator[Sequence[T], T, None]:
     """
     Return the ngrams in the sequence.
 
     """
     Return the ngrams in the sequence.
 
@@ -288,7 +300,7 @@ def ngrams(lst: Sequence[Any], n: int):
         yield lst[i : i + n]
 
 
         yield lst[i : i + n]
 
 
-def permute(seq: str):
+def permute(seq: str) -> Generator[str, str, None]:
     """
     Returns all permutations of a sequence.
 
     """
     Returns all permutations of a sequence.
 
@@ -314,7 +326,7 @@ def permute(seq: str):
     yield from _permute(seq, "")
 
 
     yield from _permute(seq, "")
 
 
-def _permute(seq: str, path: str):
+def _permute(seq: str, path: str) -> Generator[str, str, None]:
     """Internal helper to permute items recursively."""
     seq_len = len(seq)
     if seq_len == 0:
     """Internal helper to permute items recursively."""
     seq_len = len(seq)
     if seq_len == 0:
@@ -441,7 +453,7 @@ def powerset(seq: Sequence[Any]) -> Iterator[Sequence[Any]]:
     return chain.from_iterable(combinations(seq, r) for r in range(len(seq) + 1))
 
 
     return chain.from_iterable(combinations(seq, r) for r in range(len(seq) + 1))
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     import doctest
 
     doctest.testmod()
     import doctest
 
     doctest.testmod()