library backend, initial version

Nick Guenther 15 years ago
parent 671f0d95a8
commit 9ac0c98ea6
  1. 99

@ -0,0 +1,99 @@
""" The backend for the library-tracking system
This uses shelve which is pretty simplistic, but which should be sufficient for our (extremely minimal) use of the library.
There is a booklist (book=(Author, Title, Year, Signout)) where signout is None for "Checked In" or a (userid, date) to give who and when that book was signed out.
import shelve
import time
import re
LIBRARY_DB = "./csc_library.db"
class Book:
def __init__(self, author, title, year):
"""Any of these may be None to indicate 'unknown'.""" =author
self.title = title
self.year = year
self.signout = None
def sign_out(self, username):
if self.signout is None:
self.signout = Signout(username)
raise Exception("Book already signed out to %s" %, b)
def sign_in(self):
if self.signout is not None:
self.signout = None
raise Exception("Book was not signed out, no need to sign it in")
def __str__(self):
return "Book(author=%r, title=%r, year=%r, signout=%r)" % (, self.title, self.year, self.signout)
class Signout:
"""Represents a sign-out of a book to someone. Automatically records when the signout occured"""
def __init__(self, name):
#in theory we could check that the name given to us is in LDAP = str(name) = time.time()
def __repr__(self):
return "Signout(%r, %s)" % (, time.ctime(
def reset():
"""make a fresh database""",'n').close()
def add(author, title, year):
db =,'w') #use w here (not c) to ensure a crash if the DB file got erased (is this a good idea?)
i = len(db)
db[str(i)] = Book(author, title, year)
def search(author=None, title=None, year=None):
"""search for a title
author and title are regular expressions
year is a single number or a list of numbers (so use range() to search the DB)
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
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
db =, 'w', 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 author is not None:
all = [book for book in all if and re.match(author,] #should factor this out
if title is not None:
all = [book for book in all if book.title and re.match(title, book.title)] #should factor this out
if year is not None:
if type(year) == int:
year = [year]
#now assume year is a list
all = [book for book in all if book.year and book.year in year]
return all
#def delete(....):
# """must think about how to do this one; it'll have to be tied to the DB ID somehow"""
# pass
if __name__ == '__main__':
#print "Making database"
#print "Filling database"
#add("Bob McBob", "My Life Of Crime", None)
print "Listing database"
for b in search():
print b