[Python-checkins] r69266 - sandbox/trunk/dbm_sqlite/alt/dbdict.py
raymond.hettinger
python-checkins at python.org
Wed Feb 4 09:28:45 CET 2009
Author: raymond.hettinger
Date: Wed Feb 4 09:28:44 2009
New Revision: 69266
Log:
Add dbdict, a dbm based on a dict subclass.
On open, loads full file into memory.
On close, writes full dict to disk.
Supported file formats: JSON, pickle, or csv
Added:
sandbox/trunk/dbm_sqlite/alt/dbdict.py (contents, props changed)
Added: sandbox/trunk/dbm_sqlite/alt/dbdict.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/dbm_sqlite/alt/dbdict.py Wed Feb 4 09:28:44 2009
@@ -0,0 +1,78 @@
+'''Alternate DB based on a dict subclass
+
+Runs like gdbm's fast mode (all writes all delayed until close).
+While open, the whole dict is kept in memory. Start-up and
+close time's are potentially long because the whole dict must be
+read or written to disk.
+
+File format is selectable between pickle, json, and csv.
+All three are backed by fast C implementations.
+
+'''
+
+import os, pickle, json, csv
+
+class DictDB(dict):
+
+ def __init__(self, filename, flag='c', mode=None, *args, **kwds):
+ # r=readonly c=read_write_create_if_needed n=new_overwrite_previous
+ self.flag = flag
+ self.mode = mode
+ self.filename = filename
+ if flag != 'n' and os.access(filename, os.R_OK):
+ file = __builtins__.open(filename, 'rb')
+ try:
+ self.update(load(file))
+ finally:
+ file.close()
+ self.update(*args, **kwds)
+
+ def sync(self):
+ if self.flag != 'r':
+ file = __builtins__.open(self.filename, 'wb')
+ try:
+ dump(self, file)
+ finally:
+ file.close()
+ if self.mode:
+ os.chmod(self.filename, self.mode)
+
+ def close(self):
+ self.sync()
+
+def myopen(filename, flag='c', mode=0o666):
+ return DictDB(filename, flag, mode)
+
+def set_csv():
+ global load, dump
+ def load(f):
+ ans = list(csv.reader(f))
+ return ans
+ def dump(self, f):
+ csv.writer(f).writerows(self.items())
+
+def set_json():
+ global load, dump
+ load = json.load
+ def dump(self, f):
+ json.dump(self, f, separators=(',',':'))
+
+def set_pickle():
+ global load, dump
+ load = pickle.load
+ def dump(self, f):
+ pickle.dump(self, f, -1)
+
+set_csv()
+
+if __name__ == '__main__':
+ os.chdir('/dbm_sqlite/alt')
+ print(os.getcwd())
+ s = myopen('tmp.shl', 'c')
+ print(s, 'start')
+ s['xyz'] = 'pdq'
+ s['abc'] = '123'
+ s.close()
+ f = __builtins__.open('tmp.shl', 'rb')
+ print (f.read())
+ f.close()
More information about the Python-checkins
mailing list