summaryrefslogtreecommitdiff
path: root/list_utils.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-09-08 23:29:05 -0700
committerScott Gasch <[email protected]>2021-09-08 23:29:05 -0700
commit709370b2198e09f1dbe195fe8813602a3125b7f6 (patch)
tree5bbe521d7973f86526ed09d4411a5b3ff0e23aaa /list_utils.py
parentb10d30a46e601c9ee1f843241f2d69a1f90f7a94 (diff)
Add doctests to some of this stuff.
Diffstat (limited to 'list_utils.py')
-rw-r--r--list_utils.py35
1 files changed, 29 insertions, 6 deletions
diff --git a/list_utils.py b/list_utils.py
index 7d3355c..993ca8a 100644
--- a/list_utils.py
+++ b/list_utils.py
@@ -5,16 +5,28 @@ from typing import Any, Iterator, List
def shard(lst: List[Any], size: int) -> Iterator[Any]:
- """Yield successive size-sized shards from lst."""
+ """
+ Yield successive size-sized shards from lst.
+
+ >>> for sublist in shard([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3):
+ ... [_ for _ in sublist]
+ [1, 2, 3]
+ [4, 5, 6]
+ [7, 8, 9]
+ [10, 11, 12]
+
+ """
for x in range(0, len(lst), size):
yield islice(lst, x, x + size)
def flatten(lst: List[Any]) -> List[Any]:
- """Flatten out a list:
+ """
+ Flatten out a list:
+
+ >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
+ [1, 2, 3, 4, 5, 6, 7, 8, 9]
- >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
- [1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
if len(lst) == 0:
return lst
@@ -24,6 +36,17 @@ def flatten(lst: List[Any]) -> List[Any]:
def prepend(item: Any, lst: List[Any]) -> List[Any]:
- """Prepend an item to a list."""
- lst = list.insert(0, item)
+ """
+ Prepend an item to a list.
+
+ >>> prepend('foo', ['bar', 'baz'])
+ ['foo', 'bar', 'baz']
+
+ """
+ lst.insert(0, item)
return lst
+
+
+if __name__ == '__main__':
+ import doctest
+ doctest.testmod()