diff options
author | Jay Hutchinson <jlhutch@gmail.com> | 2010-08-24 16:53:48 -0500 |
---|---|---|
committer | Jay Hutchinson <jlhutch@gmail.com> | 2010-08-24 16:53:48 -0500 |
commit | 73dc3f76b8a27648c4d715a65d3789c24613a25d (patch) | |
tree | 0495af1ae1dc93179167d64e29da5003a811bd60 | |
parent | 1c6476dadcf1b1dd1ab9d87513326d354608a2e8 (diff) | |
download | pylru-73dc3f76b8a27648c4d715a65d3789c24613a25d.tar.gz |
Fixed indention for test.py
-rw-r--r-- | test.py | 171 |
1 files changed, 87 insertions, 84 deletions
@@ -1,78 +1,81 @@ - - def _selftest(): + + +from lru import lrucache + +def _selftest(): class simplelrucache: + + def __init__(self, size): + self.size = size + self.length = 0 + self.items = [] + + def __contains__(self, key): + for x in self.items: + if x[0] == key: + return True - def __init__(self, size): - self.size = size - self.length = 0 - self.items = [] - - def __contains__(self, key): - for x in self.items: - if x[0] == key: - return True - - return False - + return False + class simplelrucache: - def __init__(self, size): + def __init__(self, size): - # Initialize the cache as empty. - self.cache = [] - self.size = size + # Initialize the cache as empty. + self.cache = [] + self.size = size - def __contains__(self, key): + def __contains__(self, key): - for x in self.cache: - if x[0] == key: - return True + for x in self.cache: + if x[0] == key: + return True - return False + return False - def __getitem__(self, key): + def __getitem__(self, key): - for i in range(len(self.cache)): - x = self.cache[i] - if x[0] == key: - del self.cache[i] - self.cache.append(x) - return x[1] + for i in range(len(self.cache)): + x = self.cache[i] + if x[0] == key: + del self.cache[i] + self.cache.append(x) + return x[1] - assert False + assert False - def __setitem__(self, key, obj): + def __setitem__(self, key, obj): - for i in range(len(self.cache)): - x = self.cache[i] - if x[0] == key: - x[1] = obj - del self.cache[i] - self.cache.append(x) - return + for i in range(len(self.cache)): + x = self.cache[i] + if x[0] == key: + x[1] = obj + del self.cache[i] + self.cache.append(x) + return - if len(self.cache) == self.size: - self.cache = self.cache[1:] + if len(self.cache) == self.size: + self.cache = self.cache[1:] - self.cache.append([key, obj]) + self.cache.append([key, obj]) - return + return - def __delitem__(self, key): + def __delitem__(self, key): - for i in range(len(self.cache)): - if self.cache[i][0] == key: - del self.cache[i] - return + for i in range(len(self.cache)): + if self.cache[i][0] == key: + del self.cache[i] + return - return + return @@ -80,60 +83,60 @@ class simplelrucache: def testa(): - a = lrucache(16) + a = lrucache(16) - for i in range(len(vect)): - a[vect[i]] = 0 + for i in range(len(vect)): + a[vect[i]] = 0 def testb(): - a = simplelrucache(16) + a = simplelrucache(16) - for i in range(len(vect)): - a[vect[i]] = 0 + for i in range(len(vect)): + a[vect[i]] = 0 if __name__ == '__main__': - import random + import random - a = lrucache(20) - b = simplelrucache(20) + a = lrucache(20) + b = simplelrucache(20) - for i in range(256): - x = random.randint(0, 256) - y = random.randint(0, 256) + for i in range(256): + x = random.randint(0, 256) + y = random.randint(0, 256) - a[x] = y - b[x] = y + a[x] = y + b[x] = y - q = [] - z = a.head - for j in range(len(a.table)): - q.append([z.key, z.obj]) - z = z.next + q = [] + z = a.head + for j in range(len(a.table)): + q.append([z.key, z.obj]) + z = z.next - if q != b.cache[::-1]: - print i - print b.cache[::-1] - print q - print a.table.keys() - assert False + if q != b.cache[::-1]: + print i + print b.cache[::-1] + print q + print a.table.keys() + assert False - from timeit import Timer - import random + from timeit import Timer + import random - global vect + global vect - vect = [] - for i in range(1000000): - vect.append(random.randint(0, 1000)) + vect = [] + for i in range(1000000): + vect.append(random.randint(0, 1000)) - t = Timer("testa()", "from __main__ import testa") - print t.timeit(1) + t = Timer("testa()", "from __main__ import testa") + print t.timeit(1) - t = Timer("testb()", "from __main__ import testb") - print t.timeit(1) + t = Timer("testb()", "from __main__ import testb") + print t.timeit(1) |