Created
June 2, 2011 00:52
-
-
Save ianlivingstone/1003706 to your computer and use it in GitHub Desktop.
Python 2.7 GDBM Wrapper
This file contains hidden or 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 json | |
import gdbm | |
import zlib | |
class GDBMWrapper (object): | |
""" | |
Wrapper around gdbm that provides a better interface and allows you to | |
store more complex data while maintaining cross language support | |
through json. | |
Usage Example: | |
# Open our database and store blog posts | |
# The key will be a slug that is a url friendly verison | |
# of the blog title. | |
import datetime | |
from GDBMWrapper import GDBMWrapper | |
stories = GDBMWrapper('db/stories.dbm') | |
stories[u'how_to_run'] = { | |
u'date':datetime.utcnow(), | |
u'title':u'How To: Run!', | |
u'body':u'You can run in many ways...' | |
} | |
stories[u'how_to_jump'] = { | |
u'date':datetme.utcnow(), | |
u'title':u'How To: Jump!', | |
u'text':u'..' | |
} | |
# Now we can loop over the stories | |
for slug, story in stories: | |
print slug, story | |
# Also, single get based on key | |
if 'how_to_jump' in stories: | |
story = stories['how_to_jump'] | |
# Finally, delete it. | |
del stories['how_to_jump'] | |
# When we're done, close it! | |
stories.close() | |
""" | |
def __init__ (self, path, flag = 'c'): | |
""" | |
Opens the database at the given path. | |
By default uses creation flag. For more information about what | |
this means please view the gdbm documentation at: | |
http://docs.python.org/library/gdbm.html | |
""" | |
self.flag = flag | |
self.db = gdbm.open(path, flag) | |
def __getitem__ (self, name): | |
return json.loads(zlib.decompress(self.db[name])) | |
def __setitem__ (self, name, value): | |
self.db[name] = zlib.compress(json.dumps(value,separators=(',',':'))) | |
def __delitem__ (self, name): | |
del self.db[name] | |
def __iter__ (self): | |
data = [] | |
k = self.db.firstkey() | |
while k != None: | |
yield (k, json.loads(zlib.decompress(self.db[k]))) | |
k = self.db.nextkey(k) | |
def __len__ (self): | |
return len(self.db) | |
def __contains__ (self, name): | |
try: | |
self.db[name] | |
except KeyError, e: | |
return False | |
else: | |
return True | |
def reorganize (self): | |
""" | |
If you have performed a lot of delete operations you can shrink | |
the size of the binary file by calling this method. | |
""" | |
return self.db.reorganize() | |
def close (self): | |
""" | |
Will sync the file (for fast mode), and close the file handler. | |
""" | |
self.db.sync() | |
self.db.close() | |
if __name__ == '__main__': | |
gdbm = GDBMWrapper('test.gdbm') | |
for i in range(50): | |
gdbm[str(i)] = 'hello' | |
for k,v in gdbm: | |
print k,v | |
for i in range(50): | |
print gdbm[str(i)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment