-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook_list.py
More file actions
87 lines (72 loc) · 2.2 KB
/
Copy pathbook_list.py
File metadata and controls
87 lines (72 loc) · 2.2 KB
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
#
# Book list
#
import book
import string
class Book_List:
def __init__ (self):
self.contents = []
def cmp(a, b):
return (a > b) - (a < b)
def make_from_file (self, file):
#
# Read the file and create a book list
#
lines = file.readlines ()
self.contents = []
#
# Parse each line and create a list of Book objects
#
for one_line in lines:
# It's not a comment or empty line
if (len(one_line) > 0) and (one_line[0] != "#"):
# Split into tokens
tokens = one_line[:-1].split(":", 1)
if len (tokens) > 0:
if tokens[0] == "title":
current_book = book.Book (tokens[1].strip())
self.contents.append (current_book)
elif tokens[0] == "author":
current_book.set_author (tokens[1])
elif tokens[0] == "subject":
current_book.set_subject (tokens[1].strip())
elif tokens[0] == "url" and len(tokens) > 1:
current_book.set_url (tokens[1].strip())
def sort_by_author (self):
#
# Sort book list by author
#
self.contents.sort (key = lambda x : x.last_name)
def sort_by_title (self):
#
# Sort book list by title
#
self.contents.sort (key = lambda x : x.title)
def sort_by_subject (self):
#
# Sort by subject
#
self.contents.sort (key = lambda x : x.subject)
def display (self):
#
# Print the contents of the list
#
for b in self.contents:
print("-----------------")
b.display ()
print("-----------------")
#
# Code to test this class
#
if (__name__ == "__main__"):
print ("*** testing book_file_parser ****")
f = open ("books.txt", "r")
book_list = Book_List ()
book_list.make_from_file (f)
book_list.display ()
book_list.sort_by_author ()
book_list.display ()
book_list.sort_by_title ()
book_list.display ()
book_list.sort_by_subject ()
book_list.display ()