Created
June 18, 2014 17:16
-
-
Save jboynyc/d8dcae5650230ed910d0 to your computer and use it in GitHub Desktop.
simple persistent key-value store backed by... shelve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shelve | |
from contextlib import closing | |
class Store: | |
'''Simple persistent key-value store.''' | |
def __init__(self, shelf='data.shelf'): | |
self.shelf = shelf | |
def store(self, key, value): | |
with closing(shelve.open(self.shelf)) as db: | |
db[key] = value | |
def get_value(self, key): | |
with closing(shelve.open(self.shelf)) as db: | |
return(db[key]) | |
def has_key(self, key): | |
with closing(shelve.open(self.shelf)) as db: | |
return(key in db) | |
def keys(self): | |
with closing(shelve.open(self.shelf)) as db: | |
return(db.keys()) | |
def __contains__(self, key): | |
return(self.has_key(key)) | |
def __getitem__(self, key): | |
return(self.get_value(key)) | |
def __setitem__(self, key, value): | |
self.store(key, value) | |
def __delitem__(self, key): | |
with closing(shelve.open(self.shelf)) as db: | |
del db[key] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment