Package Bio :: Package SCOP :: Module FileIndex
[hide private]
[frames] | no frames]

Source Code for Module Bio.SCOP.FileIndex

 1  # Copyright 2001 by Gavin E. Crooks.  All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  # This functionality may be of general use, in which case this module should 
 7  # be moved out of the SCOP package. 
 8   
9 -class defaultdict(dict):
10
11 - def __init__(self, default=None):
12 dict.__init__(self) 13 self.default = default
14
15 - def __getitem__(self, key):
16 try: 17 return dict.__getitem__(self, key) 18 except KeyError: 19 return self.default
20
21 -class FileIndex(dict) :
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
59 - def __getitem__(self, key) :
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