1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
<!--startcut ==========================================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<head><title>Book Class</title></head>
<BODY BGCOLOR="#EEE1CC" TEXT="#000000" LINK="#0000FF" VLINK="#0020F0"
ALINK="#FF0000">
<pre>
#
# Instances of this class represent books. A description of a book includes
#
# title, author, subject, and optional URL of the book's description
#
from string import split, strip
class Book:
def __init__ (self, t="", a="", s="", u=""):
#
# Create an instance of Book
#
self.title = t
self.last_name = []
self.first_name = []
self.set_author (a)
self.subject = s
self.url = u
def set_title (self, new_title):
self.title = new_title
def set_author (self, new_author):
#
# Author's name is in "last_name, first_name" format
#
if new_author:
names = split (new_author, ",")
self.last_name.append (strip (names[0]))
self.first_name.append (strip (names[1]))
else:
self.last_name = []
self.first_name = []
def set_subject (self, new_subject):
self.subject = new_subject
def set_url (self, new_url):
self.url = new_url
def display (self):
print "Title : " + self.title
i = 0
while i > len (self.first_name):
print "Author : " + self.first_name[i] + " " + self.last_name[i]
i = i + 1
print "Subject: " + self.subject
print "URL : " + self.url
#
# Code to test this class
#
if __name__ == '__main__':
print "**** Test 1 ****"
b = Book()
b.set_author ("Gann, Ernest")
b.set_title ("Fate is the Hunter")
b.set_subject ("General Aviation")
b.display ()
print "*** Test 2 ****"
b = Book ("Fate is the Hunter", "Gann, Ernest")
b.display ()
print "*** Test 3 ****"
b = Book ("Some book", "First, Author")
b.set_author ("Seconf, Author")
b.display ()
print "*** Finish ***"
</pre>
<!--===================================================================-->
<P> <hr> <P>
<center><H5>Copyright © 1997, Richie Bielak<BR>
Published in Issue 19 of the Linux Gazette, July 1997</H5></center>
<!--startcut ==========================================================-->
</BODY>
</HTML>
<!--endcut ============================================================-->
|