Skip to content

API

DiskStore (MutableMapping) API.

Examples:

>>> from diskstore import DiskStore
>>> ds = DiskStore("/tmp/data.db")
>>> ds["one"] = 1
>>> ds["one"]
1

DiskStore

Bases: DiskRead, MutableMapping

filename property

DiskStore filename for DB.

tablename property

Tablename used to get data from.

timeout property

SQLite connection timeout value in seconds.

__contains__(key)

Check if key exists in the store.

__getitem__(key)

Get value for key, raises KeyError if not found.

__init__(filename, config=None)

SQLite based MutableMapping disk storage.

Parameters:

Name Type Description Default
filename PathLike | str

DiskStore DB filename.

required
config ConfigProtocol | None

Configuration as specified in ConfigProtocol

None

__iter__()

Iterate over keys in insertion order.

__len__()

Return the number of items in the store.

To do this count is used which is not a performant implementation.

__reversed__()

Iterate over keys in reverse insertion order.

close()

Close the database connection if open.

items()

Return a set-like view of (key, value) pairs in the mapping.

keys()

Return a set-like view of keys in the mapping.

open()

Open (or re-open) the database connection and return self.

query(where=None, parameters=None, order=None, limit=None, offset=None)

Query rows with optional filtering, ordering, limit and offset.

Parameters:

Name Type Description Default
where Optional[str]

SQL WHERE clause (without the WHERE keyword).

None
parameters Optional[Sequence | dict]

Parameters for the WHERE clause.

None
order Optional[str]

ORDER BY clause (without the ORDER BY keyword).

None
limit int | None

Maximum number of rows to return.

None
offset int | None

Number of rows to skip.

None

Yields:

Type Description
tuple

(key, value) tuples.

Examples:

>>> from diskstore import DiskRead
>>> ds = DiskRead("/tmp/data.db")
>>> list(ds.query(where="_key > ?", parameters=(1,), limit=5))
[(2, 'two'), (3, 'three')]

values()

Return a set-like view of values in the mapping.

DiskRead (Mapping) API.

Examples:

>>> from diskstore import DiskRead
# DB must exist!
>>> ds = DiskRead("/tmp/data.db")
>>> ds["one"]
1

DiskRead

Bases: Mapping

filename property

DiskStore filename for DB.

tablename property

Tablename used to get data from.

timeout property

SQLite connection timeout value in seconds.

__contains__(key)

Check if key exists in the store.

__getitem__(key)

Get value for key, raises KeyError if not found.

__init__(filename, config=None)

SQLite read only disk storage.

Database is opened read only on demand.

Parameters:

Name Type Description Default
filename PathLike | str

filename for DB to use.

required
config ConfigProtocol | None

Configuration

None

__iter__()

Iterate over keys in insertion order.

__len__()

Return the number of items in the store.

To do this count is used which is not a performant implementation.

__reversed__()

Iterate over keys in reverse insertion order.

close()

Close the database connection if open.

items()

Return a set-like view of (key, value) pairs in the mapping.

keys()

Return a set-like view of keys in the mapping.

open()

Open (or re-open) the database connection and return self.

query(where=None, parameters=None, order=None, limit=None, offset=None)

Query rows with optional filtering, ordering, limit and offset.

Parameters:

Name Type Description Default
where Optional[str]

SQL WHERE clause (without the WHERE keyword).

None
parameters Optional[Sequence | dict]

Parameters for the WHERE clause.

None
order Optional[str]

ORDER BY clause (without the ORDER BY keyword).

None
limit int | None

Maximum number of rows to return.

None
offset int | None

Number of rows to skip.

None

Yields:

Type Description
tuple

(key, value) tuples.

Examples:

>>> from diskstore import DiskRead
>>> ds = DiskRead("/tmp/data.db")
>>> list(ds.query(where="_key > ?", parameters=(1,), limit=5))
[(2, 'two'), (3, 'three')]

values()

Return a set-like view of values in the mapping.

Default constants used in diskstore.

DEFAULT_PRAGMAS = {'auto_vacuum': 0, 'cache_size': 2 ** 13, 'journal_mode': 'wal', 'mmap_size': 2 ** 28, 'synchronous': 1, 'temp_store': 'memory'} module-attribute

default pragma settings

TIMEOUT = 60.0 module-attribute

default timout in seconds

DiskStore configuration classes and helpers for config.

BaseConfig

Bases: ConfigProtocol

Base default configuratin.

ConfigProtocol

Bases: Protocol

Configuration Protocol

Attributes:

Name Type Description
tablename str

Table name as string

key_type str

key type as string or basic Python type like str, int, float

timeout float

Timeout used to wait if someone blocks connections or with writes

pragmas dict

Dictionary with PRAGMAs to set when connections is initialized

fields Iterable

Iterable used for select, update and create statement with field name, type and default. [("value", str), ("value2", int, 0)]

dump_value(value) abstractmethod

Called with the value, should return an Iterable which is used to write to the DB.

load_data(data) abstractmethod

load(db_data): called with the tuple selected from DB. Should be converted to the type normally received as value.