[Zodb-checkins] SVN: ZODB/trunk/src/ZODB/ Optimization: use
iterators where possible
Dmitry Vasiliev
dima at hlabs.spb.ru
Thu Feb 3 05:14:29 EST 2005
Log message for revision 29025:
Optimization: use iterators where possible
Changed:
U ZODB/trunk/src/ZODB/fsIndex.py
U ZODB/trunk/src/ZODB/tests/testfsIndex.py
-=-
Modified: ZODB/trunk/src/ZODB/fsIndex.py
===================================================================
--- ZODB/trunk/src/ZODB/fsIndex.py 2005-02-03 10:11:20 UTC (rev 29024)
+++ ZODB/trunk/src/ZODB/fsIndex.py 2005-02-03 10:14:28 UTC (rev 29025)
@@ -48,7 +48,7 @@
def str2num(s):
return struct.unpack(">Q", "\000\000" + s)[0]
-class fsIndex:
+class fsIndex(object):
def __init__(self):
self._data = {}
@@ -76,7 +76,7 @@
def __len__(self):
r = 0
- for tree in self._data.values():
+ for tree in self._data.itervalues():
r += len(tree)
return r
@@ -85,7 +85,7 @@
self[k] = v
def has_key(self, key):
- v=self.get(key, self)
+ v = self.get(key, self)
return v is not self
def __contains__(self, key):
@@ -101,27 +101,27 @@
self._data.clear()
def __iter__(self):
- for prefix, tree in self._data.items():
+ for prefix, tree in self._data.iteritems():
for suffix in tree:
yield prefix + suffix
+ iterkeys = __iter__
+
def keys(self):
- r = []
- for prefix, tree in self._data.items():
- for suffix in tree.keys():
- r.append(prefix + suffix)
- return r
+ return list(self.iterkeys())
+ def iteritems(self):
+ for prefix, tree in self._data.iteritems():
+ for suffix, value in tree.iteritems():
+ yield (prefix + suffix, str2num(value))
+
def items(self):
- r = []
- for prefix, tree in self._data.items():
- for suffix, v in tree.items():
- r.append(((prefix + suffix), str2num(v)))
- return r
+ return list(self.iteritems())
+ def itervalues(self):
+ for tree in self._data.itervalues():
+ for value in tree.itervalues():
+ yield str2num(value)
+
def values(self):
- r = []
- for prefix, tree in self._data.items():
- for v in tree.values():
- r.append(str2num(v))
- return r
+ return list(self.itervalues())
Modified: ZODB/trunk/src/ZODB/tests/testfsIndex.py
===================================================================
--- ZODB/trunk/src/ZODB/tests/testfsIndex.py 2005-02-03 10:11:20 UTC (rev 29024)
+++ ZODB/trunk/src/ZODB/tests/testfsIndex.py 2005-02-03 10:14:28 UTC (rev 29025)
@@ -18,12 +18,15 @@
class Test(unittest.TestCase):
- def testInserts(self):
- index=fsIndex()
+ def setUp(self):
+ self.index = fsIndex()
for i in range(200):
- index[p64(i*1000)]=(i*1000L+1)
+ self.index[p64(i * 1000)] = (i * 1000L + 1)
+ def testInserts(self):
+ index = self.index
+
for i in range(0,200):
self.assertEqual((i,index[p64(i*1000)]), (i,(i*1000L+1)))
@@ -40,7 +43,7 @@
# self.failUnless(len(index._data) > 1)
def testUpdate(self):
- index=fsIndex()
+ index = self.index
d={}
for i in range(200):
@@ -63,7 +66,52 @@
self.assertEqual(index.get(p64(399000)), 399002)
self.assertEqual(len(index), 600)
+ def testKeys(self):
+ keys = list(iter(self.index))
+ keys.sort()
+ for i, k in enumerate(keys):
+ self.assertEqual(k, p64(i * 1000))
+
+ keys = list(self.index.iterkeys())
+ keys.sort()
+
+ for i, k in enumerate(keys):
+ self.assertEqual(k, p64(i * 1000))
+
+ keys = self.index.keys()
+ keys.sort()
+
+ for i, k in enumerate(keys):
+ self.assertEqual(k, p64(i * 1000))
+
+ def testValues(self):
+ values = list(self.index.itervalues())
+ values.sort()
+
+ for i, v in enumerate(values):
+ self.assertEqual(v, (i * 1000L + 1))
+
+ values = self.index.values()
+ values.sort()
+
+ for i, v in enumerate(values):
+ self.assertEqual(v, (i * 1000L + 1))
+
+ def testItems(self):
+ items = list(self.index.iteritems())
+ items.sort()
+
+ for i, item in enumerate(items):
+ self.assertEqual(item, (p64(i * 1000), (i * 1000L + 1)))
+
+ items = self.index.items()
+ items.sort()
+
+ for i, item in enumerate(items):
+ self.assertEqual(item, (p64(i * 1000), (i * 1000L + 1)))
+
+
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
More information about the Zodb-checkins
mailing list