Search works whoooo

This commit is contained in:
Nick Guenther 2008-06-02 20:40:25 -04:00
parent 943079fb68
commit 0241e3b0eb
2 changed files with 34 additions and 18 deletions

View File

@ -9,9 +9,11 @@ import shelve
import time import time
import re import re
LIBRARY_DB = "./csc_library.db" #LIBRARY_DB = "/users/ceo/library/books.db"
LIBRARY_DB = "./csc_library.db" #testing location
def format_maybe(v): def format_maybe(v):
"""little hack to make printing things that may come out as None nicer"""
if v is None: if v is None:
return "unknown" return "unknown"
else: else:
@ -78,26 +80,40 @@ def search(author=None, title=None, year=None, signedout=None):
whichever ones passed in that aren't None are the restrictions used in the search whichever ones passed in that aren't None are the restrictions used in the search
possibly-useful side effect of this design is that search() just gets the list of everything possibly-useful side effect of this design is that search() just gets the list of everything
this is extraordinarily inefficient, but whatever (I don't think that without having an indexer run inthe background we can improve this any?) this is extraordinarily inefficient, but whatever (I don't think that without having an indexer run inthe background we can improve this any?)
returns: a list of Book objects returns: a sequence of Book objects
""" """
db = shelve.open(LIBRARY_DB, 'c', writeback=True) #open it for writing so that changes to books get saved db = shelve.open(LIBRARY_DB, 'c', writeback=True) #open it for writing so that changes to books get saved
all = db.values() #this should pull out the ID numbers somehow too.. bah if type(year) == int:
if author is not None: year = [year]
all = [book for book in all if book.author and re.search(author, book.author)] #should factor this out def filter(book):
if title is not None: """filter by the given params, but only apply those that are non-None"""
all = [book for book in all if book.title and re.search(title, book.title)] #should factor this out #this code is SOOO bad, someone who has a clear head please fix this
if year is not None: #we need to apply:
if type(year) == int: b_auth = b_title = b_year = b_signedout = True #default to true (in case of None i.e. this doesn't apply)
year = [year] if author is not None:
#now assume year is a list if re.search(author, book.author):
all = [book for book in all if book.year and book.year in year] b_auth = True
if signedout is not None: else:
if signedout: b_auth = False
all = [book for book in all if book.signout is not None] if title is not None:
else: if re.search(title, book.title): #should factor this out
all = [book for book in all if book.signout is None] #aaah copypaste b_title = True
else:
b_title = False
if year is not None: #assume year is a list
if book.year in year:
b_year = True
else:
b_year = False
if signedout is not None:
b_signedout = signedout == (book.signout is not None)
return b_auth and b_title and b_year and b_signedout
for i in db:
book = db[i]
if(filter(book)):
yield i,book
db.close() db.close()
return all
#def delete(....): #def delete(....):

BIN
ceo/library.pyc Normal file

Binary file not shown.