Skip to main content

Persistent compressed expiring dict in Python, backed up by sqlite3 and pickle

Project description

A lightweight wrapper around Python’s sqlite3 database with a MutableMapping interface:

from expiringsqlitedict import SqliteDict
with SqliteDict('./my_db.sqlite') as mydict:
    mydict['some_key'] = any_picklable_object
    print(mydict['some_key'])  # prints the new value
    for key, value in mydict.items():
        print((key, value))
    print(len(mydict)) # etc... all dict functions work

Pickle is used internally by default to serialize the values, and zlib is used to optionally compress (on insertion, the value is compressed, and it’s stored compressed if the compressed value is smaller than uncompressed). Keys are arbitrary strings, values arbitrary pickle-able objects. This must be used within a context manager, and serialization can be overridden with your own. The database is wrapped with a transaction, and any exception thrown out of the context manager rolls back all changes.

This was forked off of sqlitedict in order to add auto-expiring functionality, and initially was quite similar to it. Version 2.0 splits of completely and takes the module into a complete rewrite, mostly to remove unnecesary Ptyhon 2 compatibility, simplify the API, completely enforce a context manager for typical cases, add full typing throughout, and use sqlite triggers for expiration cleanup.

This version also does not vacuum at all automatically. It did in previous versions, but this was kind of a silly behavior to put into the library itself. If you want your database file intermittently vacuumed, you should put such behavior into a crontab or use the sqlite3 module to do it yourself intermittently.

Features

  • Values can be any picklable objects (this can be customized to be as flexible as you need, through custom serializers)

  • Support for access from multiple programs or threads, with locking fully managed by sqlite itself.

  • A very simple codebase that is easy to read, relying on sqlite for as much behavior as possible.

  • A simple autocommit wrapper (AutocommitSqliteDict), if you really can’t handle a context manager and need something that fully handles like a dict.

  • Support for custom serialization or compression:

class JsonSerializer:
    @staticmethod
    def loads(data: bytes) -> Any:
        return json.loads(data.decode('utf-8'))

    @staticmethod
    def dumps(value: Any) -> bytes:
        return json.dumps(value).encode('utf-8')

with SqliteDict('some.db', serializer=JsonSerializer()) as mydict:
    mydict['some_key'] = some_json_encodable_object
    print(mydict['some_key'])

Installation

The module has no dependencies beyond Python itself.

Install or upgrade with:

pip install expiringsqlitedict

or from the source tar.gz:

python setup.py install

This module is a single file, so you could also easily import the module in your own tree, if your workflow needs that.

Testing

You may test this by running test.py with PYTHONPATH set to the current working directory. There is a convenience makefile to do this for you when you run:

make test

Documentation

Standard Python document strings are inside the module:

>>> import expiringsqlitedict
>>> help(expiringsqlitedict)

Comments, bug reports

expiringsqlitedict resides on github. You can file issues or pull requests there.


expiringsqlitedict is open source software released under the Apache 2.0 license. Version <2 Copyright (c) 2011-2018 Radim Řehůřek and contributors. All versions copyright (c) 2018-2021 Absolute Performance, Inc.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

expiringsqlitedict-2.2.1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

expiringsqlitedict-2.2.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file expiringsqlitedict-2.2.1.tar.gz.

File metadata

  • Download URL: expiringsqlitedict-2.2.1.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.1

File hashes

Hashes for expiringsqlitedict-2.2.1.tar.gz
Algorithm Hash digest
SHA256 bb4978b0cf27339f8f21e4378ed22cdb44f87d903cb1a67257722fc26514b6cd
MD5 0080d37f1dea3b27f23d9158a8bb3e7d
BLAKE2b-256 287d3822ef022fa12b4cfcc514a9850c5e6c52047824a259c63c0fc537a5545d

See more details on using hashes here.

File details

Details for the file expiringsqlitedict-2.2.1-py3-none-any.whl.

File metadata

  • Download URL: expiringsqlitedict-2.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.1

File hashes

Hashes for expiringsqlitedict-2.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cfb014aca9180b6f421e39007fc5912a5b9d80fe13bb39a6a295393761ae7905
MD5 98036d39ec56f702bc8b28fe0d00bd0f
BLAKE2b-256 3dabaebef9c05bbf318730dbccc71d39a16dfdd405b66f3da15da7790e5028ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page