keyv
Transform SQLite into a powerful and fast key-value store.
keyv is a lightweight library that turns SQLite into a NoSQL key-value database, combining the best of both worlds: the simplicity of key-value stores with the reliability of SQLite. Perfect for applications that need a robust local database without the complexity of a full DBMS.
Usage
pip install keyv
import keyv
# initialize the database
db = keyv.connect('keyv.db') # or .anything, since is sqlite3 engine, you can choose any
# create or get a collection
collection = db.collection('my_collection')
# insert a key-value pair
collection.set('key1', 'value1')
# by default the .set method will replace existing values
# if you want to prevent overwriting existing keys, you can
# use replace_if_exists=False
collection.set('key1', 'value1', replace_if_exists=False)
# retrieve a value by key
collection.get('key1')
# >>> 'value1'
# note: the get method returns None for non-existing keys
# update a value
collection.update('key1', 123)
collection.get('key1')
# >>> 123
# delete a key-value pair
collection.delete('key1')
# search for keys by value
collection.set('key2', 'common_value')
collection.set('key3', 'common_value')
collection.search('common_value')
# >>> ['key2', 'key3']
# retrieve all keys, values and items
collection.keys()
# >>> ['key2', 'key3']
# retrieve all values
collection.values()
# >>> ['common_value', 'common_value']
# retrieve all key-value pairs
collection.items()
# >>> [('key2', 'common_value'), ('key3', 'common_value')]
# check if a key exists
collection.key_exists('key2')
# >>> True
# rename a collection
collection.change_name('new_collection_name')
# delete a collection
db.delete_collection('new_collection_name')
# get list of all collections
db.collections()
# >>> ['collection1', 'collection2', ...]
# iterate over key-value pairs
for key, value in collection.iteritems():
print(f"{key}: {value}")
# iterate over keys
for key in collection.iterkeys():
print(key)
# iterate over values
for value in collection.itervalues():
print(value)
# get with default value for missing keys
collection.get('missing_key', default='default_value')
# >>> 'default_value'
# raise exception for missing keys
try:
collection.get('missing_key', raise_if_missing=True)
except ValueError:
print("Key not found!")
Collections allow you to organize your data into separate namespaces within the same database file. In practice, they are tables inside the SQLite file.
Quick Start
For simple use cases, you can create a default collection right away:
# Create a database with a default collection
db = keyv.connect('keyv.db').collection('main')
# Now you can use the collection methods directly
db.set('key1', 'value1')
db.get('key1')
# >>> 'value1'
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a PR.
Metadata
Release files for keyv 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| keyv-0.4.0.tar.gz | 5.6 kB | Without provenance | Details for keyv-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| keyv-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for keyv-0.4.0-py3-none-any.whl |
Total release size: 11.6 kB
Release files / keyv-0.4.0.tar.gz
| Download URL | keyv-0.4.0.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7433d89c62215a168412dc3a6d2596af2b4ac114ee88bdf9e0d0c6bedd91fa3a
|
|
BLAKE2b-256 checksum How to use checksums |
13ab3388700107beb6cbc2ce9eb5bf698216e5b3a36deb8049db262ca3a77e67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.1 CPython/3.13.1 Windows/11
|
Release files / keyv-0.4.0-py3-none-any.whl
| Download URL | keyv-0.4.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
316b3b0a94ead0f0478e0e34e5571d195516f9758c3d876e4d3735bf12173daf
|
|
BLAKE2b-256 checksum How to use checksums |
187c0136321372ee4245df0cf82e8bd153462653b8d5d0b40e428f627eb662a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.1 CPython/3.13.1 Windows/11
|
Security for keyv 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.