Dictionary interface to an SQLite database
Project description
sqlitemap
Dictionary interface to an SQLite database.
Intro
…One day I needed an embedded key-value store for a pet project, but didn't find a «good enough» implementation. So, I made my own one.
It's a lightweight wrapper over the standard sqlite3 module. It provides the standard MutableMapping
interface for an SQLite connection and SQLite table.
Connection
You create an instance of Connection
as if it was a normal sqlite3.connect
call:
from sqlitemap import Connection
connection = Connection(':memory:', ...)
It implements the context manager interface, so you use with
to make a transaction as if it was an sqlite3.Connection
. And it implements MutableMapping[str, Collection]
, except for __setitem__
. So you can imagine a Connection
as a dictionary of collections altogether with their names and do virtually everything you could do with a normal dict
:
from sqlitemap import Collection
# Collection is automatically created:
foo: Collection = connection['foo']
# You can iterate over collection names:
names = list(connection)
# Or even over collections:
collections = connection.values()
# Drop collection:
del connection['foo']
# Get number of collections:
len(connection)
# Special one, to close the connection:
connection.close()
Internally, collection is a table with two columns: key: str
and value: bytes
. So, you need some serialization to represent objects as byte strings. By default, sqlitemap
uses the standard json
module. It picks up ujson
or orjson
, if available. These are also available as sqlitemap
extras: sqlitemap[ujson]
and sqlitemap[orjson]
.
Otherwise, you can specify any custom Callable[[Any], bytes]
for encoder and Callable[[bytes], Any]
for decoder:
connection = Connection(':memory:', dumps_=custom_dumps, loads_=custom_loads)
Collection
Collection
also implements the context manager interface to make a transaction, and MutableMapping[str, Any]
:
Setting an item
with raises(KeyError):
_ = collection['foo']
collection['foo'] = 'bar'
assert collection['foo'] == 'bar'
collection['foo'] = 'qux'
assert collection['foo'] == 'qux'
key
column is a primary key.
Retrieving keys
assert list(collection) == []
collection['foo'] = 'bar'
assert list(collection) == ['foo']
Retrieving values
assert collection.values() == []
collection['foo'] = 'bar'
assert collection.values() == ['bar']
Deleting an item
with raises(KeyError):
del collection['foo']
collection['foo'] = 42
del collection['foo']
with raises(KeyError):
del collection['foo']
Using slices
Collection.__getitem__
and Collection.__setitem__
also support slices as their arguments. Slice start
is then converted to key >= start
clause, stop
to key < stop
and step
to key LIKE step
. All of these are combined with the AND
operator. Collection.__getitem__
also applies ORDER BY key
clause, so it's possible to make some more sophisticated queries:
collection['bar'] = 1
collection['foo'] = 2
collection['quw'] = 3
collection['qux'] = 4
collection['quy'] = 5
collection['quz'] = 6
assert collection['foo':] == [2, 3, 4, 5, 6]
assert collection[:'foo'] == [1]
assert collection[::'qu%'] == [3, 4, 5, 6]
assert collection['bar':'quz':'qu%'] == [3, 4, 5]
The same also works with del collection [...]
. It deletes the rows that would be selected with the corresponding __getitem__
call:
collection['bar'] = 1
collection['foo'] = 2
collection['quw'] = 3
collection['qux'] = 4
collection['quy'] = 5
collection['quz'] = 6
del collection['bar':'quz':'qu%']
assert list(collection) == ['bar', 'foo', 'quz']
Controlling transactions
sqlitemap
does nothing special to control transactions. For that refer to the standard library documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sqlitemap-0.2.0.tar.gz
.
File metadata
- Download URL: sqlitemap-0.2.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 456b53b71283910d56ad01ad28272495d51ae7b732d8413b33d36c90bc8c542a |
|
MD5 | cd8d066c662dbd6a89ae51432777f31d |
|
BLAKE2b-256 | 6ba34123f4e4a184f59f6e9002a2bac162acefed162877da97aecf57e08d76d4 |
File details
Details for the file sqlitemap-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: sqlitemap-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb2d11dfd66786e3dbb8de0fe8ece589b0197c8f2838a6ae06624b583be79e35 |
|
MD5 | 3982ab28b94fa9f764dcc4735a967f35 |
|
BLAKE2b-256 | d6e54499c71ff2f45aa5ff7d84313ab0bac7e4f75c3303dd65a70a0a4037a0c4 |