Skip to main content

Persistent dict in Python, backed up by sqlite3 and pickle, multithread-safe.

Project description

Travis Downloads License

A lightweight wrapper around Python’s sqlite3 database, with a dict-like interface and multi-thread access support:

>>> mydict = SqliteDict('some.db', autocommit=True) # the mapping will be persisted to file `some.db`
>>> mydict['some_key'] = any_picklable_object
>>> print mydict['some_key']
>>> print len(mydict) # etc... all dict functions work

Pickle is used internally to serialize the values. Keys are strings.

If you don’t use autocommit (default is no autocommit for performance), then don’t forget to call mydict.commit() when done with a transaction.

Features

  • Values can be any picklable objects (uses cPickle with the highest protocol).

  • Support for multiple tables (=dicts) living in the same database file.

  • Support for access from multiple threads to the same connection (needed by e.g. Pyro). Vanilla sqlite3 gives you ProgrammingError: SQLite objects created in a thread can only be used in that same thread.

Concurrent requests are still serialized internally, so this “multithreaded support” doesn’t give you any performance benefits. It is a work-around for sqlite limitations in Python.

Installation

The module has no dependencies beyond 2.5 <= Python < 3.0. Install or upgrade with:

sudo easy_install -U sqlitedict

or from the source tar.gz

python sqlitedict.py # run some tests
sudo python setup.py install

Documentation

Standard Python document strings are inside the module:

>>> import sqlitedict
>>> help(sqlitedict)

(but it’s just dict with a commit, really).

Beware: because of Python semantics, sqlitedict cannot know when a mutable persistent-dictionary entry was modified. For example, mydict.setdefault(‘new_key’, []).append(1) will leave mydict[‘new_key’] equal to empty list, not [1]. You’ll need to explicitly assign the mutated object back to achieve the same effect:

>>> val = mydict.get('new_key', [])
>>> val.append(1)
>>> mydict['new_key'] = val

For developers

Install:

# pip install nose
# pip install coverage

To perform all tests:

# make test-all

To perform all tests with coverage:

# make test-all-with-coverage

Comments, bug reports

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


sqlitedict is released as public domain, you may do with it as you please. Hack away.

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

sqlitedict-1.1.0.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file sqlitedict-1.1.0.tar.gz.

File metadata

  • Download URL: sqlitedict-1.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sqlitedict-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b80887023b8a411e40a8f231d3649e376fc7dd0663a1bdaa17a568a97907d5f0
MD5 85a269160fb6d49e08f09b75f2ae0d74
BLAKE2b-256 44582f1e1771cbaec51d77d55da57f1c58045e7541fdb749293d59f3cad019a0

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