Commit dd54f185 authored by Dmitry Vasiliev's avatar Dmitry Vasiliev

Optimization: use iterators where possible

parent ca2ed1cf
......@@ -48,7 +48,7 @@ def num2str(n):
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 @@ class fsIndex:
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 @@ class fsIndex:
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 @@ class fsIndex:
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())
......@@ -18,11 +18,14 @@ from ZODB.utils import p64
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 @@ class Test(unittest.TestCase):
# self.failUnless(len(index._data) > 1)
def testUpdate(self):
index=fsIndex()
index = self.index
d={}
for i in range(200):
......@@ -63,6 +66,51 @@ class Test(unittest.TestCase):
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()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment