So in a further attempt to learn some Python, I've taken the little
Library program
(http://groups.google.com/group/comp....a9ccf1bc136f84)
I wrote and added several features to it. Readers now quit when they've
read all the books in the Library. Books know how many times they've
been read. Best of all, you can now create your own list of books to
read!
Again, the point of all this is to get used to programming in Python.
So although the program is trivial, any feedback on style, structure,
etc. would be much appreciated. I'm a convert from Java, so I've
probably got some unconscious Javanese in there somewhere. Help me get
rid of it!
Here's the new, improved program:
And here's a handy text file of books for you, so you don't have to
make your own:
Library program
(http://groups.google.com/group/comp....a9ccf1bc136f84)
I wrote and added several features to it. Readers now quit when they've
read all the books in the Library. Books know how many times they've
been read. Best of all, you can now create your own list of books to
read!
Again, the point of all this is to get used to programming in Python.
So although the program is trivial, any feedback on style, structure,
etc. would be much appreciated. I'm a convert from Java, so I've
probably got some unconscious Javanese in there somewhere. Help me get
rid of it!
Here's the new, improved program:
Code:
#!/usr/bin/python
# Filename: Library.py
# author: mwt
# Feb, 2006
import thread
import time
import threading
import random
class Library2:
def __init__(self, listOfBooks, totalBooks):
self.stacks = listOfBooks
self.cv = threading.Condition()
self.totalBooks = totalBooks
def checkOutBook(self, readerName):
"'Remove book from the front of the list, block if no books are
available'"
self.cv.acquire()
while len(self.stacks) == 0:
self.cv.wait()
print "%s waiting for a book..." %readerName
book = self.stacks.pop(0)
self.cv.release()
return book
def returnBook(self, returnedBook):
"'put book at the end of the list, notify that a book is
available'"
returnedBook.wasRead()
self.cv.acquire()
self.stacks.append(returnedBook)
self.cv.notify()
self.cv.release()
class Reader(threading.Thread):
def __init__(self, library, name, readingSpeed, timeBetweenBooks):
threading.Thread.__init__(self)
self.library = library
self.name = name
self.readingSpeed = readingSpeed
self.timeBetweenBooks = timeBetweenBooks
self.book = ""
self.numberOfBooksRead = 0
def run(self):
"'Keep checking out and reading books until you've read all in
the Library'"
while self.numberOfBooksRead < self.library.totalBooks:
self.book = self.library.checkOutBook(self.name)
print "%s reading %s" %(self.name, self.book.title),
time.sleep(self.readingSpeed)
self.numberOfBooksRead += 1
self.library.returnBook(self.book)
print "%s done reading %s" %(self.name, self.book.title),
print"Number of books %s has read: %d" %(self.name,
self.numberOfBooksRead)
self.bookName = ""
time.sleep(self.timeBetweenBooks)
print "%s done reading." %self.name
class Book:
def __init__(self, author, title):
self.author = author
self.title = title
self.numberOfTimesRead = 0
#print "%s,%s" % (self.author, self.title),#print as books are
loaded in
def wasRead(self):
self.numberOfTimesRead += 1
print "Number of times %s has been read: %d" %(self.title,
self.numberOfTimesRead)
if __name__=="__main__":
print "\nWELCOME TO THE THURMOND STREET PUBLIC LIBRARY"
print "Checking which books are avialable...\n"
try:
theBookFile = open("books.txt", "r")#Create your own list of
books!
stacks = []#a place to put the books
for line in theBookFile.readlines():
L = line.split (",") # a comma-delimited list
author = L[0]
bookName = L[1]
newBook = Book(author, bookName)
stacks.append(newBook)
theBookFile.close()
except IOError:
print "File not found!"
#string = "How many books would you like in the Library?[1-" +
str(len(stacks)) + "]"
totalBooks = input("How many books would you like in the
Library?[1-" + str(len(stacks)) + "]")
stacks[totalBooks: len(stacks)] = []
print "Number of books in the Library is:", len(stacks)
library = Library2(stacks, totalBooks)
readers = input("\nHow many readers would you like?")
print "Number of readers is:", readers, "\n"
for i in range(0,readers):
newReader = Reader(library, "Reader" + str (i),
random.randint(1,7), random.randint(1,7))
newReader.start()
make your own:
Code:
Conrad, Heart of Darkness Kafka, Die Verwandlung Hemingway, For Whom the Bell Tolls James Joyce, Dubliners Moliere, Cyrano de Bergerac William Golding, Lord of the Flies Dostoevski, Crime and Punishment Cervantes, Don Quixote Camus, L'Etranger Tolstoy, War and Peace Poe, Tales Faulkner, The Sound and the Fury Orwell, 1984 Fitzgerald, The Great Gatsby Steinbeck, The Grapes of Wrath Huxley, Brave New World Twain, The Adventures of Huckleberry Finn Mann, Der Tod in Venedig Kesey, Sometimes a Great Notion Pynchon, Gravity's Rainbow McEwan, The Cement Garden Márquez, Cien Años de Soledad Salinger, The Catcher in the Rye Miltion, Paradise Lost Chapman et al , The Pythons
Comment