dcs

view library.py @ 26:84d626fd32d4

mass add
author Dave St.Germain <dcs@adullmoment.com>
date Sun, 12 Jun 2011 19:08:36 -0400
parents 9cfdbf76639f
children db67204582de
line source
1 """
2 This is a starting point for our library management system
3 """
4 import pickle
5 import os.path
6 import isbndb
8 ISBNDB_KEY = 'W8NQQ89H'
10 class Book:
11 def __init__(self):
12 self.title = ''
13 self.author = ''
14 self.isbn = ''
17 class MultipleResultsException(Exception):
18 def __init__(self, results):
19 Exception.__init__(self)
20 self.results = results
22 def __iter__(self):
23 return iter(self.results)
26 class Library:
27 """
28 represents a collection of books/things
29 """
30 def __init__(self, filename):
31 self.books = {}
32 self.filename = filename
34 def add_book(self, book):
35 """
36 add a book to the library
37 """
38 self.books[book.title] = book
40 def remove_book(self, book):
41 """
42 remove a book from the library
43 """
44 del self.books[book.title]
46 def find_book(self, title):
47 """
48 return the book with the given title
49 """
50 return self.books[title]
52 def get_all_books(self):
53 """
54 return a list of all the books
55 """
56 return self.books.values()
58 def add_by_isbn(self, isbn):
59 """
60 add a book by isbn
61 """
62 try:
63 book_info = isbndb.by_isbn(ISBNDB_KEY, isbn)[0]
64 except isbndb.NotFoundException:
65 if isbn.isdigit():
66 if len(isbn) in (9, 10, 13):
67 print("no book with that isbn number was found in isbndb.com")
68 else:
69 print("that's not a properly formatted isbn number...")
70 return
71 else:
72 #the input was letters
73 try:
74 #if the input is text and a proper title
75 results = isbndb.by_title(ISBNDB_KEY, '"%s"' % isbn)
76 first = results[0]
77 if str(first['title_long'] or first['title']).lower() != isbn.lower():
78 # can't be sure which this is
79 raise MultipleResultsException(results[:25])
80 else:
81 book_info = first
82 except isbndb.NotFoundException:
83 #if the input is text but not a proper title
84 print("duuuuude! that's not an isbn number OR a title! what's your deal?!")
85 return
87 new_book = Book()
89 new_book.title = book_info['title']
90 new_book.title_long = book_info['title_long']
91 new_book.isbn = book_info['isbn']
92 new_book.author = book_info['author']
93 self.add_book(new_book)
94 self.save()
95 return new_book
97 def mass_add(self, fname):
98 open_file = open(fname, 'r')
99 for book in open_file:
100 book_info = book.strip().split('\t')
101 new_book = Book()
102 new_book.title = book_info[1]
103 new_book.isbn = book_info[0]
104 new_book.author = book_info[2]
105 self.add_book(new_book)
107 def save(self):
108 """
109 saves the library
110 """
111 with open(self.filename, 'wb') as fp:
112 pickle.dump(self, fp)
114 def load(filename):
115 if os.path.exists(filename):
116 with open(filename, 'rb') as fp:
117 return pickle.load(fp)
118 else:
119 # if it doesn't exists, create a new instance of the library
120 return Library(filename)
123 def main():
124 my_library = Library.load('lib.pckl')
126 print(my_library.get_all_books())
127 while True:
128 newbook = Book()
129 newbook.title = input('enter title: ').strip()
130 newbook.author = input('enter author: ').strip()
132 my_library.add_book(newbook)
133 my_library.save()
134 if input('continue? [y/n]').strip() == 'n':
135 break
138 if __name__ == '__main__':
139 main()