Python has a builtin, persistent, key-value store

by manu

I'm currently working on a small Python project for myself and wanted to store some data locally. I still knew about pickle from back in the day, but I haven't really used it much. Other than that I could have used sqlite or store everything in plain json files. But as someone who is quite lazy, I didn't really want to come up with something on my own. For my upcoming exam in non-relational databases, I was thinking about a simple key-value store such as Redis for a second, before I finally got to know about shelve.

A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings.

And it looks like I'm not the only one who has got excited about it.