1
2
3
4
5
6
7
8
20
22 """ An in memory index that allows rapid random access into a file.
23
24 The class can be used to turn a file into a read-only
25 database.
26 """
27 - def __init__(self, filename, iterator_gen, key_gen ) :
28 """
29 Arguments:
30
31 filename -- The file to index
32
33 iterator_gen -- A function that eats a file handle, and returns
34 a file iterator. The iterator has a method next()
35 that returns the next item to be indexed from the file.
36
37 key_gen -- A function that generates an index key from the items
38 created by the iterator.
39 """
40 dict.__init__(self)
41
42 self.filename = filename
43 self.iterator_gen = iterator_gen
44
45 f = open(self.filename)
46 try:
47 loc = 0
48 i = self.iterator_gen(f)
49 while 1 :
50 next_thing = i.next()
51 if next_thing is None : break
52 key = key_gen(next_thing)
53 if key != None :
54 self[key]=loc
55 loc = f.tell()
56 finally :
57 f.close()
58
60 """ Return an item from the indexed file. """
61 loc = dict.__getitem__(self,key)
62
63 f = open(self.filename)
64 try:
65 f.seek(loc)
66 thing = self.iterator_gen(f).next()
67 finally:
68 f.close()
69 return thing
70