mbudisic commited on
Commit
9e054dd
·
1 Parent(s): 94a6b26

doc: Added docstring for flatten function in utils.py

Browse files
Files changed (1) hide show
  1. pstuts_rag/pstuts_rag/utils.py +52 -1
pstuts_rag/pstuts_rag/utils.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import Dict, Type
2
 
3
  from langchain_openai import ChatOpenAI
4
  from langchain_openai.embeddings import OpenAIEmbeddings
@@ -90,3 +90,54 @@ EmbeddingsAPISelector: Dict[
90
  ModelAPI.OPENAI: OpenAIEmbeddings,
91
  ModelAPI.OLLAMA: OllamaEmbeddings,
92
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Type, Any, Iterator
2
 
3
  from langchain_openai import ChatOpenAI
4
  from langchain_openai.embeddings import OpenAIEmbeddings
 
90
  ModelAPI.OPENAI: OpenAIEmbeddings,
91
  ModelAPI.OLLAMA: OllamaEmbeddings,
92
  }
93
+
94
+
95
+ def flatten(lst: List[Any]):
96
+ """
97
+ Recursively flatten a nested list structure into a single-level generator.
98
+
99
+ Takes a list that may contain nested lists and yields all elements
100
+ in a flat sequence. Uses recursive generators to handle arbitrary
101
+ nesting depth efficiently.
102
+
103
+ Args:
104
+ lst (List[Any]): The input list which may contain nested lists
105
+
106
+ Yields:
107
+ Any: Individual elements from the flattened list structure
108
+
109
+ Example:
110
+ >>> list(flatten([1, [2, 3], [4, [5, 6]]]))
111
+ [1, 2, 3, 4, 5, 6]
112
+
113
+ >>> list(flatten(['a', ['b', 'c'], 'd']))
114
+ ['a', 'b', 'c', 'd']
115
+ """
116
+ for item in lst:
117
+ if isinstance(item, list):
118
+ yield from flatten(item)
119
+ else:
120
+ yield item
121
+
122
+
123
+ def batch(iterable: List[Any], size: int = 16) -> Iterator[List[Any]]:
124
+ """
125
+ Batch an iterable into chunks of specified size.
126
+
127
+ Yields successive chunks from the input iterable, each containing
128
+ at most 'size' elements. Useful for processing large collections
129
+ in manageable batches to avoid memory issues or API rate limits.
130
+
131
+ Args:
132
+ iterable (List[Any]): The input list to be batched
133
+ size (int, optional): Maximum size of each batch. Defaults to 16.
134
+
135
+ Yields:
136
+ List[Any]: Successive batches of the input iterable
137
+
138
+ Example:
139
+ >>> list(batch([1, 2, 3, 4, 5], 2))
140
+ [[1, 2], [3, 4], [5]]
141
+ """
142
+ for i in range(0, len(iterable), size):
143
+ yield iterable[i : i + size]