MCPcopy
hub / github.com/msiemens/tinydb / get

Method get

tinydb/table.py:283–344  ·  view source on GitHub ↗

Get exactly one document specified by a query or a document ID. However, if multiple document IDs are given then returns all documents in a list. Returns ``None`` if the document doesn't exist. :param cond: the condition to check against :pa

(
        self,
        cond: Optional[QueryLike] = None,
        doc_id: Optional[int] = None,
        doc_ids: Optional[List] = None
    )

Source from the content-addressed store, hash-verified

281 return docs
282
283 def get(
284 self,
285 cond: Optional[QueryLike] = None,
286 doc_id: Optional[int] = None,
287 doc_ids: Optional[List] = None
288 ) -> Optional[Union[Document, List[Document]]]:
289 """
290 Get exactly one document specified by a query or a document ID.
291 However, if multiple document IDs are given then returns all
292 documents in a list.
293
294 Returns ``None`` if the document doesn't exist.
295
296 :param cond: the condition to check against
297 :param doc_id: the document's ID
298 :param doc_ids: the document's IDs(multiple)
299
300 :returns: the document(s) or ``None``
301 """
302 table = self._read_table()
303
304 if doc_id is not None:
305 # Retrieve a document specified by its ID
306 raw_doc = table.get(str(doc_id), None)
307
308 if raw_doc is None:
309 return None
310
311 # Convert the raw data to the document class
312 return self.document_class(raw_doc, doc_id)
313
314 elif doc_ids is not None:
315 # Filter the table by extracting out all those documents which
316 # have doc id specified in the doc_id list.
317
318 # Since document IDs will be unique, we make it a set to ensure
319 # constant time lookup
320 doc_ids_set = set(str(doc_id) for doc_id in doc_ids)
321
322 # Now return the filtered documents in form of list
323 return [
324 self.document_class(doc, self.document_id_class(doc_id))
325 for doc_id, doc in table.items()
326 if doc_id in doc_ids_set
327 ]
328
329 elif cond is not None:
330 # Find a document specified by a query
331 # The trailing underscore in doc_id_ is needed so MyPy
332 # doesn't think that `doc_id_` (which is a string) needs
333 # to have the same type as `doc_id` which is this function's
334 # parameter and is an optional `int`.
335 for doc_id_, doc in self._read_table().items():
336 if cond(doc):
337 return self.document_class(
338 doc,
339 self.document_id_class(doc_id_)
340 )

Callers 15

containsMethod · 0.95
searchMethod · 0.45
getFunction · 0.45
test_json_readFunction · 0.45
test_one_tableFunction · 0.45
test_deleteFunction · 0.45
test_add_intFunction · 0.45
test_add_strFunction · 0.45
test_subtractFunction · 0.45
test_setFunction · 0.45
test_incrementFunction · 0.45
test_decrementFunction · 0.45

Calls 2

_read_tableMethod · 0.95
setFunction · 0.85

Tested by 15

getFunction · 0.36
test_json_readFunction · 0.36
test_one_tableFunction · 0.36
test_deleteFunction · 0.36
test_add_intFunction · 0.36
test_add_strFunction · 0.36
test_subtractFunction · 0.36
test_setFunction · 0.36
test_incrementFunction · 0.36
test_decrementFunction · 0.36