#!/usr/bin/python author_keys = [ 'name', 'student', 'inst', 'advisor' ] record_keys = [ 'title', 'issue', 'date', 'pages', 'andree' ] key_names = { 'issue':'Issue', 'date':'Date',\ 'title':'Title', 'pages':'Pages', 'andree':'Andree Award',\ 'name':'Author', 'student':'Student', 'inst':'School',\ 'advisor':'Advisor' } class Records: def __init__(self): self.file = open("data") self.known = None self.issue = "Unknown" self.date = "Unknown" self.count = 0 def hasNext(self): if self.known is None: self.readNext() return self.known def next(self): if self.known is None: self.readNext() ret = self.known self.known = None return ret def readNext(self): if self.file is None: return rec = { 'authors':[], 'id':self.count, \ 'issue':self.issue, 'date':self.date } self.count = self.count + 1 author = None while 1==1: linePos = self.file.tell() line = self.file.readline() if line == "": self.file.close() self.file = None if 'title' in rec: self.known = None else: self.known = rec self.issue = rec['issue'] self.date = rec['date'] return if line[0:6] in [ 'issue ', 'title '] and 'title' in rec: self.file.seek(linePos) self.known = rec self.issue = rec['issue'] self.date = rec['date'] return if line[0] <> '#': index = line.find(' ') if index >= 0: key = line[0:index] val = line[index + 1:].strip() if key == 'name': author = { 'name':val } rec['authors'].append(author) elif key in author_keys: if author is None: print "Error: Author has attribute before name" else: author[key] = val else: rec[key] = val def search(key, value): recs = Records() result = [] value = value.upper() if key in author_keys: while recs.hasNext(): rec = recs.next() add = 0==1 for author in rec['authors']: if key in author and author[key].upper().find(value) >= 0: add = 1==1 if add: result.append(rec) else: while recs.hasNext(): rec = recs.next() if key in rec and rec[key].upper().find(value) >= 0: result.append(rec) return result