[Python-checkins] r69265 - sandbox/trunk/dbm_sqlite/alt/dbsqlite.py
raymond.hettinger
python-checkins at python.org
Wed Feb 4 00:26:55 CET 2009
Author: raymond.hettinger
Date: Wed Feb 4 00:26:54 2009
New Revision: 69265
Log:
Clean-ups.
Modified:
sandbox/trunk/dbm_sqlite/alt/dbsqlite.py
Modified: sandbox/trunk/dbm_sqlite/alt/dbsqlite.py
==============================================================================
--- sandbox/trunk/dbm_sqlite/alt/dbsqlite.py (original)
+++ sandbox/trunk/dbm_sqlite/alt/dbsqlite.py Wed Feb 4 00:26:54 2009
@@ -42,7 +42,7 @@
return self.conn.execute(GET_LEN).fetchone()[0]
def __bool__(self):
- GET_BOOL = 'SELECT MAX(ROWID) FROM shelf' # returns None if count is zero
+ GET_BOOL = 'SELECT MAX(ROWID) FROM shelf'
return self.conn.execute(GET_BOOL).fetchone()[0] is not None
def keys(self):
@@ -68,17 +68,15 @@
raise KeyError(key)
return item[0]
- def __setitem__(self, key, value):
+ def __setitem__(self, key, value):
ADD_ITEM = 'REPLACE INTO shelf (key, value) VALUES (?,?)'
self.conn.execute(ADD_ITEM, (key, value))
- #self.conn.commit()
def __delitem__(self, key):
if key not in self:
raise KeyError(key)
- DEL_ITEM = 'DELETE FROM shelf WHERE key = ?'
+ DEL_ITEM = 'DELETE FROM shelf WHERE key = ?'
self.conn.execute(DEL_ITEM, (key,))
- #self.conn.commit()
def update(self, items=(), **kwds):
if isinstance(items, collections.Mapping):
@@ -89,13 +87,13 @@
if kwds:
self.update(kwds)
- def clear(self):
- CLEAR_ALL = 'DELETE FROM shelf; VACUUM;'
+ def clear(self):
+ CLEAR_ALL = 'DELETE FROM shelf; VACUUM;'
self.conn.executescript(CLEAR_ALL)
self.conn.commit()
def sync(self):
- if self.conn is not None:
+ if self.conn is not None:
self.conn.commit()
def close(self):
@@ -105,27 +103,27 @@
self.conn = None
def __del__(self):
- self.close()
+ self.close()
class ListRepr:
def __repr__(self):
- return repr(list(self))
+ return repr(list(self))
class SQLhashKeysView(collections.KeysView, ListRepr):
-
+
def __iter__(self):
GET_KEYS = 'SELECT key FROM shelf ORDER BY ROWID'
return map(itemgetter(0), self._mapping.conn.cursor().execute(GET_KEYS))
class SQLhashValuesView(collections.ValuesView, ListRepr):
-
+
def __iter__(self):
GET_VALUES = 'SELECT value FROM shelf ORDER BY ROWID'
return map(itemgetter(0), self._mapping.conn.cursor().execute(GET_VALUES))
class SQLhashItemsView(collections.ValuesView, ListRepr):
-
+
def __iter__(self):
GET_ITEMS = 'SELECT key, value FROM shelf ORDER BY ROWID'
return iter(self._mapping.conn.cursor().execute(GET_ITEMS))
@@ -136,12 +134,14 @@
return SQLhash()
+
+
if __name__ in '__main___':
for d in SQLhash(), SQLhash('example'):
list(d)
print(list(d), "start")
d['abc'] = 'lmno'
- print(d['abc'])
+ print(d['abc'])
d['abc'] = 'rsvp'
d['xyz'] = 'pdq'
print(d.items())
@@ -150,7 +150,7 @@
print(list(d), 'list')
d.update(p='x', q='y', r='z')
print(d.items())
-
+
del d['abc']
try:
print(d['abc'])
@@ -158,7 +158,7 @@
pass
else:
raise Exception('oh noooo!')
-
+
try:
del d['abc']
except KeyError:
@@ -167,7 +167,7 @@
raise Exception('drat!')
print(list(d))
- print(bool(d), True)
+ print(bool(d), True)
d.clear()
print(bool(d), False)
print(list(d))
More information about the Python-checkins
mailing list