A trivial, persistent, dictionary-like object, backed by SQLite.
$ python setup.py install
Or just drop the permadict.py
file into your package.
Basic usage:
>>> from permadict import Permadict
>>> d = Permadict("db.sqlite")
>>> d["key"] = "value"
>>> print(d["key"])
value
As a context manager:
>>> with Permadict("db.sqlite") as d:
... d["something"] = 1.2345
...
>>> with Permadict("db.sqlite") as d:
... print(d["something"])
...
1.2345
Iterating:
>>> d = Permadict("db.sqlite")
>>> for k, v in d.items():
... print(k, v)
...
something 1.2345
>>> for key in d.keys():
... print(key)
...
something
Deleting an item:
>>> del d["something"]
Clearing all items:
>>> d.clear()
Keys must be strings. Values are stored as BLOB
type after being
pickled, so your Python objects must be picklable. Reading and writing speeds
for large binary blobs are likely to be considerably slower than reading/writing
directly to the filesystem, so Permadict
shouldn't be used to store large
Numpy arrays, for example. See here for more details.
Permadict
doesn't act entirely like a dict
: some methods are
missing, whether that be on purpose (as with dict.copy
) or simply
due to negligence.
I needed a way to share small amounts of data between processes. SQLite provides a safe way to do so. Also, why not?