InMemoryStorage#

class scikitplot.corpus.InMemoryStorage[source]#

Thread-safe in-memory dict store.

Stores documents as CorpusDocument objects directly in a dict keyed by doc_id. Insertion order is preserved (Python 3.7+ dict guarantee).

Warning

This backend is intended for testing and prototyping only. Data is lost when the process exits.

Parameters:
None

Examples

>>> store = InMemoryStorage()
>>> store.save(doc)
>>> store.count()
1
>>> store.get(doc.doc_id).text == doc.text
True
clear()[source]#

Remove all documents from the store.

Return type:

None

close()[source]#

Release the backend’s resources; safe to call more than once.

The default holds nothing and does nothing. A backend that holds a connection or a file (SQLiteStorage) overrides it.

Notes

User. Prefer with SQLiteStorage(path) as store: ...: the connection is closed when the block ends, also on an error. An unclosed SQLite connection is reported as a ResourceWarning by Python 3.13 and later when it is garbage-collected (R3-8).

Return type:

None

count()[source]#

Return total stored document count in O(1).

Return type:

int

get(doc_id)[source]#

Return the document with the given doc_id, or None.

Parameters:
doc_idstr
Parameters:

doc_id (str)

Return type:

CorpusDocument | None

query(q)[source]#

Filter documents by the query parameters.

Full-text search (q.full_text) is emulated by a case-insensitive substring scan and reported as FilterSupport.EMULATED – there is no stemming, tokenisation or relevance ranking. It is never silently ignored (finding F-R07-01).

Parameters:
qStorageQuery
Returns:
QueryResult
Parameters:

q (StorageQuery)

Return type:

QueryResult

save(doc)[source]#

Store doc by doc_id. Overwrites if already present.

Parameters:
docCorpusDocument
Parameters:

doc (CorpusDocument)

Return type:

None

save_batch(docs)[source]#

Store a batch of documents atomically (single lock acquisition).

Parameters:
docssequence of CorpusDocument
Parameters:

docs (Sequence[CorpusDocument])

Return type:

None