Persistent dict is a context manager that loads and stores a dict in a file automatically. Inspired by my python_json_database_manager repo. This is essentially a simpler version of that.
from persistentdict import persistentdict
with persistentdict("test") as d:
if "test" not in d:
d["test"] = "test"
print("Added 'test' key!")
else:
print("'Test' is already there!")
# The second time you run this you will get `'Test' is already there!`
"""
A dictionary that persists to a file on disk.
:param filename: The file to save the dictionary to.
:param format=json: The format to save the dictionary in. Should be a class that has a dump and load method and __name__.
:param using_locks: If True, the dictionary will be locked when it is being read or written to.
This is useful if you are using multiple processes to access the dictionary.
:param dump_kwargs: Any keyword arguments to pass to the dump method of the format.
"""Tip
Do partial application of the function arguments. If you only want to read toml files and always use locks you could use partial from functools to do this:
from persistentdict import persistentdict
from functools import partial
import toml
persistentdict = partial(persistentdict, format=toml, using_locks=True)Note
For proper toml integration, you need to install the toml python package. The standard library tomllib module (added in 3.11) is not enough as it can only load toml files but not create them.
The persistantdict works with anything that implements my Format abstract base class.
That means any object with a dump and load method and a __name__ attribute will work as a format.
wget https://raw.githubusercontent.com/tintin10q/persistentdict/refs/heads/production/persistentdict.py