|
From: Samuele P. <pe...@in...> - 2001-07-12 15:21:32
|
I'm forwarding this, in case someone has time to play with it. A polished version would be a great thing for jython. Thanks to Magnus. regards, Samuele Pedroni. PS: I think there should be a way to have the module called jdbm too. ------------- Begin Forwarded Message ------------- From: "Magnus Lie Hetland" <ml...@id...> To: "Samuele Pedroni" <pe...@in...> Cc: "Amund Tveit" <am...@el...> Subject: A start... Date: Tue, 10 Jul 2001 18:38:03 +0200 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 X-Keywords: Hi! I just threw together something which may at least be a starting point for a jdbm module. It is (AFAIK) fully functional, but can probably be improved in many ways. I had to call it something other than jdbm since that is the name of the Java module, so I called it jdbpy. It should probably be imported through anydbm anyway, so... :) Here is the code: ------------ begin jdbpy ------------ """A thin wrapper around jdbm. The open function returns a dictionary object which is a thin wrapper around a jdbm.BTree object. The jdbm database can be found at jdbm.sourceforge.net. """ _os = __import__('os') import __builtin__ _open = __builtin__.open import jdbm error = IOError # For anydbm (?) class _Database: def __init__(self, file): self._manager = jdbm.JDBMRecordManager(file) self._hash = self._manager.getHashtable('jdbpy') def __getitem__(self, key): try: val = self._hash.get(key) except: val = None if not val: raise KeyError(key) return val def __setitem__(self, key, value): self._hash.put(key, value) def __delitem__(self, key): self._hash.remove(key) def __len__(self): return len(self.keys()) # Horribly inefficient def has_key(self, key): try: result = self._hash.get(key) if result: return 1 except: pass return 0 def keys(self): e = self._hash.keys() result = [] while e.hasMoreElements(): result.append(e.nextElement()) return result def close(self): self._manager.close() del self._manager del self._hash def __del__(self): self.close() def open(file, flag = None, mode = None): # flag and mode arguments are currently ignored return _Database(file) ------------- end jdbpy ------------- Here is a simple patch for anydbm, so it works with jdbm (jdbpy): ------------- begin diff ------------- *** /home/idi/f/mlh/python/Python-2.1/Lib/anydbm.py Sun Feb 18 04:30:53 2001 --- anydbm.py Tue Jul 10 18:23:12 2001 *************** *** 10,16 **** import anydbm d = anydbm.open(file, 'w') ! The returned object is a dbhash, gdbm, dbm or dumbdbm object, dependent on the type of database being opened (determined by whichdb module) in the case of an existing dbm. If the dbm does not exist and the create or new flag ('c' or 'n') was specified, the dbm type will --- 10,16 ---- import anydbm d = anydbm.open(file, 'w') ! The returned object is a dbhash, gdbm, dbm, jdbm or dumbdbm object, dependent on the type of database being opened (determined by whichdb module) in the case of an existing dbm. If the dbm does not exist and the create or new flag ('c' or 'n') was specified, the dbm type will *************** *** 48,54 **** except: error = "anydbm.error" ! _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm'] _errors = [error] _defaultmod = None --- 48,54 ---- except: error = "anydbm.error" ! _names = ['dbhash', 'gdbm', 'dbm', 'jdbpy', 'dumbdbm'] _errors = [error] _defaultmod = None -------------- end diff -------------- Adding a check at the beginning of whichdb.py would simply entail checking for foobar.db and foobar.lg if the database is called foobar. (Or something like that - some simple analysis of the contents may be necessary; I don't know. But I would guess that it would take 10-15 minutes if one just looked at it :) It may be that using the jdbm module is not the best choice - that jdbm.btree would be more scalable... I don't know. And... If jdbm is added to Jython's capabilities, it could certainly be added to the anydbm.py of the standard distribution. The only difference is that you would have to check _one_ more possibility before falling back on dumbdm... And if you are going to fall back on that, performance is probably not an issue anyway ;) That way, the same module could be used for CPython and Jython - I assume that is a goal? Well - that's it for now. - M -- Magnus Lie Hetland http://www.hetland.org "Reality is that which, when you stop believing in it, doesn't go away." -- Philip K. Dick ------------- End Forwarded Message ------------- |