MCPcopy Index your code
hub / github.com/msiemens/tinydb / insert_multiple

Method insert_multiple

tinydb/table.py:181–223  ·  view source on GitHub ↗

Insert multiple documents into the table. :param documents: an Iterable of documents to insert :returns: a list containing the inserted documents' IDs

(self, documents: Iterable[Mapping])

Source from the content-addressed store, hash-verified

179 return doc_id
180
181 def insert_multiple(self, documents: Iterable[Mapping]) -> List[int]:
182 """
183 Insert multiple documents into the table.
184
185 :param documents: an Iterable of documents to insert
186 :returns: a list containing the inserted documents' IDs
187 """
188 doc_ids = []
189
190 def updater(table: dict):
191 for document in documents:
192
193 # Make sure the document implements the ``Mapping`` interface
194 if not isinstance(document, Mapping):
195 raise ValueError('Document is not a Mapping')
196
197 if isinstance(document, self.document_class):
198 # Check if document does not override an existing document
199 if document.doc_id in table:
200 raise ValueError(
201 f'Document with ID {str(document.doc_id)} '
202 f'already exists'
203 )
204
205 # Store the doc_id, so we can return all document IDs
206 # later. Then save the document with its doc_id and
207 # skip the rest of the current loop
208 doc_id = document.doc_id
209 doc_ids.append(doc_id)
210 table[doc_id] = dict(document)
211 continue
212
213 # Generate new document ID for this document
214 # Store the doc_id, so we can return all document IDs
215 # later, then save the document with the new doc_id
216 doc_id = self._get_next_id()
217 doc_ids.append(doc_id)
218 table[doc_id] = dict(document)
219
220 # See below for details on ``Table._update``
221 self._update_table(updater)
222
223 return doc_ids
224
225 def all(self) -> List[Document]:
226 """

Callers 14

test_one_tableFunction · 0.80
test_table_is_iterableFunction · 0.80
dbFunction · 0.80
test_insert_multipleFunction · 0.80
test_unique_idsFunction · 0.80
test_lastid_after_openFunction · 0.80
test_doc_ids_jsonFunction · 0.80
test_insert_stringFunction · 0.80
test_insert_invalid_dictFunction · 0.80

Calls 1

_update_tableMethod · 0.95

Tested by 14

test_one_tableFunction · 0.64
test_table_is_iterableFunction · 0.64
dbFunction · 0.64
test_insert_multipleFunction · 0.64
test_unique_idsFunction · 0.64
test_lastid_after_openFunction · 0.64
test_doc_ids_jsonFunction · 0.64
test_insert_stringFunction · 0.64
test_insert_invalid_dictFunction · 0.64