def __init__(self, **kwargs): """ main application build """ super().__init__(**kwargs) self.list_books = BookList() self.list_books.load_file()
def build(self): # Create the main widget for Kivy program self.booklist = BookList() self.booklist.load_books() self.title = "Reading List 2.0" # Kivy title self.root = Builder.load_file("app.kv") # load Kivy self.loading() # load the books return self.root
def __init__(self, **kwargs): kwargs['cols'] = 1 super(BookListPanel, self).__init__(**kwargs) self.book_list = BookList('books.csv') self.book_list_adapter = ListAdapter(data=[], args_converter=self.book_convert, cls=ListItemButton, selection_mode='single', allow_empty_selection=False) self.refresh_books(completed=False)
def searchBook(self, bookName): # 本を探す book_list = BookList() location = book_list.searchBook(bookName) # 本の場所がnullではない(所蔵してる)とき if location is not None: # 貸出中かチェックする lending_list = LendingList() if lending_list.check(bookName): # 貸出中のとき return "貸出中です" else: # 貸出中ではないとき return location # 所蔵してないとき else: return "その本は所蔵していません"
class BookReadList(App): def build(self): # Create the main widget for Kivy program self.booklist = BookList() self.booklist.load_books() self.title = "Reading List 2.0" # Kivy title self.root = Builder.load_file("app.kv") # load Kivy self.loading() # load the books return self.root def loading(self): count = 0 total = 0 self.root.ids.entriesBox.clear_widgets() # clear all the widgets self.root.ids.status_label.text = "Click books to mark them as completed" for item in self.booklist.requirelist: temp_button = Button(text=item.title, background_color=[ count - 0.5, 0, 1, 1 ]) # setting background colour temp_button.book = item temp_button.bind( on_release=self.handle_mark) # if click display the list self.root.ids.entriesBox.add_widget(temp_button) count += 1 total = self.booklist.getRequirePage() if count == 0: # Situation when no book there self.root.ids.top_label.text = "No required items" # Show the prompt at the top label self.root.ids.entriesBox.clear_widgets() # Clear the list widgets else: # Else print total price at the top label self.root.ids.top_label.text = "Total pages to read: {}".format( total) # Else show the total price of required items at top babel def handle_mark( self, book ): # Function when user click the button of the required item (Each required button) name = book.text self.booklist.complete_book( book.book ) # Using "c" instead of "r" in the list when user chooses the item self.root.ids.status_label.text = ( "{} marked as completed".format(name) ) # Show the marked item at the status label def completed(self): # Function for the completed item (Completed button) count = 0 total = 0 self.root.ids.entriesBox.clear_widgets() # Clear the list widgets for book in self.booklist.completelist: # using for loop to separate to sorted file temp_button = Button(text=book.title) # Setting the button text temp_button.book = book temp_button.bind(on_release=self.handle_completed ) # Setting when user click the button self.root.ids.entriesBox.add_widget( temp_button) # Add widgets for each completed item count += 1 total = self.booklist.getCompletePage() if count == 0: # If total count = 0, then means no completed item self.root.ids.top_label.text = "No completed items" # Show the prompt at the top label self.root.ids.entriesBox.clear_widgets() # Clear the list widgets else: self.root.ids.top_label.text = "Total pages completed: {}".format( total) # Else show the total price of completed items at top babel def handle_completed( self, book ): # Function when user click the button of the completed item (Each conpleted button) self.root.ids.status_label.text = "{},write by {}, pages:{}, (completed)".format( book.book.title, book.book.author, book.book.pages) # Show the detail of the completed item at the status label def handle_add(self): # Function for adding new item (Add item button) title = self.root.ids.input_name.text # Let user enter the item name author = self.root.ids.input_author.text # Let user enter the pages of item pages = self.root.ids.input_pages.text # Let user enter the priority of item if title == "" or pages == "" or author == "": # If any field is blank, show error prompt self.root.ids.status_label.text = "All fields must be completed" else: try: # Using exception let user enter valid number author = self.root.ids.input_author.text pages = int(self.root.ids.input_pages.text) except ValueError: self.root.ids.status_label.text = "Please enter a valid number" else: if pages <= 0: self.root.ids.status_label.text = "pages must be >= 0" else: book = Book(title, author, pages, flag='r') self.booklist.add_book(book) self.root.ids.status_label.text = "book \'{}\' wrote by {} in {} pages has been add to book list".format( title, author, pages) # show the added item at the status label self.root.ids.input_name.text = "" self.root.ids.input_author.text = "" self.root.ids.input_pages.text = "" # Clear whole input box after the new item add to the list # Error check def handle_clear( self): # Function for clear whole input box (Clear button) self.root.ids.input_name.text = "" self.root.ids.input_author.text = "" self.root.ids.input_pages.text = "" def save_item( self ): # Function for saving completed item to the file (Save item button) file_writer = open("books.csv", "w") # Open the file with the correct format count = 0 writer = csv.writer(file_writer) for book in self.booklist.requirelist: writer.writerow([book.title, book.author, book.pages, book.flag]) count += 1 for book in self.booklist.completelist: writer.writerow([book.title, book.author, book.pages, book.flag]) count += 1 self.root.ids.status_label.text = "{} items saved to items.csv".format( count) # Display how many items which add to the file at the status label file_writer.close()
from booklist import BookList from book import Book # test empty BookList book_list = BookList() print(book_list) assert len(book_list.books) == 0 # test loading books book_list.load_books('books.csv') print(book_list) assert len(book_list.books) > 0 # assuming CSV file is not empty # test sorting books # test adding a new Book # test saving books (check CSV file manually to see results)
def main(): my_books = BookList() print(my_books)
""" (incomplete) Tests for BookList class """ from booklist import BookList from book import Book books = BookList() # test empty BookList print(books) assert len(books.book_list) == 0 # test loading books books.load_books() print(books) assert len(books.book_list) > 0 # assuming CSV file is not empty # test adding a new Book books.add_book('In Search of Lost Time', 'Marcel Proust', 365) print(books) assert len(books.book_list) >= 0 # test saving books (check CSV file manually to see results) books.save_books() assert len(books.book_list) > 0 # test get book by title function book = books.get_book_title('The Practice of Computing Using Python') print(book) # test total number of pages for required
class ReadingListApp(App): """ This is main class for this entire programm """ top_label = StringProperty() bottom_label = StringProperty() def on_start(self): # initializing the program print("Programm is called.") self.book_button("r") def build(self): """ set Kivy for GUI :return: root kivy widget """ self.status_text = "Reading List App 2.0" self.title = "Reading list 2.0" self.root = Builder.load_file("app.kv") Window.size = (1000, 650) # window size return self.root def __init__(self, **kwargs): """ main application build """ super().__init__(**kwargs) self.list_books = BookList() self.list_books.load_file() def display_book_list(self, required_or_complited): """ This function create button for required book and completed book and put th book in to the interface to display Also, display the book with different colour depend on the number of pages :param required_or_complited: "True" is required book and "False" is completed book :return: None """ self.root.ids.displayBookList.clear_widgets() if required_or_complited == True: for book in self.list_books.booklists: if book.status == 'r': button = Button(text=book.title) button.bind(on_release=self.required_book) if book.longBook( ): # check ths number of pages of book whether the page is over 500 button.background_color = over_500_pages_book_colour # put the colour which is set on the first part of programm for over 500 pages Book else: button.background_color = under_500_pages_book_colour # put the colour which is set on the first part of programm for under 500 pages Book self.root.ids.displayBookList.add_widget(button) self.bottom_label = "Click books to mark them as completed" #display the message on botton bar self.top_label = "Total pages to read: {}".format( self.list_books.get_total_pages("r")) elif required_or_complited == False: for book in self.list_books.booklists: if book.status == 'c': button = Button(text=book.title) button.bind(on_release=self.completed_book) button.background_color = complited_book_colour # put the colour which is set on the first part of programm for completed book self.root.ids.displayBookList.add_widget(button) self.bottom_label = 'Click the book to see the details' #display the message on botton bar self.top_label = "Total pages completed: {}".format( self.list_books.get_total_pages('c')) def required_book(self, sample): """ This function shows how many pages to read and have a button for moving required book to completed book :param sample: kivy button :return: None """ title = sample.text getBook = self.list_books.get_book_by_title(title) getBook.mark_book_completed() # move required book to completed book self.top_label = "Total pages to read: {}".format( self.list_books.get_total_pages('r')) self.root.ids.displayBookList.remove_widget(widget=sample) def completed_book(self, sample): """ This function displaying message for completed book :param sample: kivy button :return: None """ title = sample.text completed_book = self.list_books.get_book_by_title(title) self.bottom_label = '{},(completed)'.format( completed_book) # displaying message for completed book def book_button(self, mode): """ This function highlights the button pressed by the clicks by users :param mode: "r" is required book and "c" is completed book. This shows whether user clicked required book or completed book :return: None """ if mode == "r": self.display_book_list(True) # shows required books self.root.ids.listRequired_button.state = 'down' # state of button when required book is displayed self.root.ids.listCompleted_button.state = 'normal' # state of button when completed book is displayed elif mode == "c": self.display_book_list(False) # shows completed books self.root.ids.listRequired_button.state = 'normal' # state of button when required book is displayed self.root.ids.listCompleted_button.state = 'down' # state of button when completed book is displayed def clear_button(self): """ This function delete the user input in "title", "author", and "page" :return: None """ self.root.ids.title_of_book.text = '' self.root.ids.book_author.text = '' self.root.ids.number_of_page.text = '' def add_new_book(self, name_of_book, name_of_author, number_of_pages): """ This function is for "add item" button for add a new book. Also, displays suitable message for each situation :param name_of_book: user input the "title" of new book :param name_of_author: user input the "author" :param number_of_pages: user input the number of "pages" of new book :return: None """ try: # Error checking count = 0 for each in str( name_of_author ): # check whether there is no word in author name section if each not in (string.ascii_letters + ' '): count += 1 if count > 0: self.bottom_label = "Please enter valid author name, Author name cannot be a number" elif name_of_book == '' or name_of_book.isspace( ) or name_of_author == '' or name_of_author.isspace( ) or number_of_pages == '': self.bottom_label = "All filed must be completed" elif int( number_of_pages ) <= 0: # check whether there is no number in page section self.bottom_label = "Please enter a valid number" else: added_book = Book(name_of_book, name_of_author, int(number_of_pages), 'r') self.list_books.add_book(added_book) self.list_books.save_file( ) # save a new book when a new book is added self.book_button("r") self.clear_button() except ValueError: # check whether there is invalid entry on page section self.bottom_label = "Please enter a valid number" def on_stop(self): print('Stop') self.list_books.save_file( ) # save changed or added books to the "books.csv" when the program finish
class ReadingListApp(App): BOOK_FILE = "books.csv" # file of books LONG_COLOUR = (0, 1, 1, 1) # colour for books 500 pages or over SHORT_COLOUR = (1, 1, 0, 1) # colour for books under 500 pages def __init__(self): super(ReadingListApp, self).__init__() self.book_list = BookList() self.top_status_bar = Label(id="top_status_bar", text="total pages", size_hint_y=None, height=50) self.bottom_status_bar = Label(id="bottom_status_bar", text="click books to mark them as complete", size_hint_y=None, height=50) def build(self): self.title = "Reading List 2.0" self.root = Builder.load_file("app.kv") self.book_list.load_books(self.BOOK_FILE) self.create_buttons("r") # creates buttons for required books return self.root def create_buttons(self, book_type): self.get_list_type(book_type) # changes what menu widget is highlighted and what the status bars say depending on the type of book chosen self.clear_buttons() self.book_list.sort_books() self.root.ids.books.add_widget(self.top_status_bar) if self.book_list.is_empty(book_type) == 0: empty_label = Label() self.root.ids.books.add_widget(empty_label) # creates a filler label if there aren't any books that match the chosen type self.create_book_buttons(book_type) self.root.ids.books.add_widget(self.bottom_status_bar) def add_book(self, title, author, pages): valid = 0 while valid == 0: if self.root.ids.title_input.text == "" or self.root.ids.author_input.text == "" \ or self.root.ids.pages_input.text == "": self.bottom_status_bar.text = "All fields must be complete" break try: book = Book(title, author, int(pages)) except ValueError: self.bottom_status_bar.text = "Please enter a valid number" break if book.pages < 0: self.bottom_status_bar.text = "Pages must be >= 0" break else: self.bottom_status_bar.text = "{} {}".format(str(book), "was added") self.book_list.add_book(Book(self.root.ids.title_input.text, self.root.ids.author_input.text, self.root.ids.pages_input.text)) valid = 1 self.clear_buttons() self.create_buttons("r") self.clear_text() def clear_buttons(self): self.root.ids.books.clear_widgets() def clear_text(self): self.root.ids.title_input.text = "" self.root.ids.author_input.text = "" self.root.ids.pages_input.text = "" def completed_button_information(self, instance): book = self.book_list.get_book_from_title(instance.text) self.bottom_status_bar.text = "{} {}".format(book, "(completed)") def create_book_buttons(self, book_type): for book in self.book_list.books: if book.book_type == book_type: # splits up required and completed books temp_button = Button(text=str(book.title)) if book.book_type == "r": temp_button.bind(on_release=self.mark_completed) if book.is_long(): temp_button.background_color = self.LONG_COLOUR else: temp_button.background_color = self.SHORT_COLOUR # buttons for required books else: temp_button.bind(on_release=self.completed_button_information) self.root.ids.books.add_widget(temp_button) # buttons for completed books def get_list_type(self, book_type): if book_type == "r": self.top_status_bar.text = "{}{}".format("Total pages to read: ", self.book_list.get_required_pages()) self.bottom_status_bar.text = "Click books to mark them as completed" self.root.ids.list_required.state = "down" self.root.ids.list_complete.state = "normal" else: self.top_status_bar.text = "{}{}".format("Total pages completed: ", self.book_list.get_completed_pages()) self.bottom_status_bar.text = "Click books to view details" self.root.ids.list_complete.state = "down" self.root.ids.list_required.state = "normal" def mark_completed(self, button): book = self.book_list.get_book_from_title(button.text) Book.mark_complete(book) self.create_buttons("r") self.bottom_status_bar.text = "{}{}".format("Completed: ", str(book)) def on_stop(self): self.book_list.save_books(self.BOOK_FILE)
def __init__(self): super(ReadingListApp, self).__init__() self.book_list = BookList() self.top_status_bar = Label(id="top_status_bar", text="total pages", size_hint_y=None, height=50) self.bottom_status_bar = Label(id="bottom_status_bar", text="click books to mark them as complete", size_hint_y=None, height=50)
class BookListPanel(GridLayout): total_pages_label = ObjectProperty(None) book_list_view = ObjectProperty(None) status_bar = ObjectProperty(None) total_pages_label_text = StringProperty() status_bar_text = StringProperty() def __init__(self, **kwargs): kwargs['cols'] = 1 super(BookListPanel, self).__init__(**kwargs) self.book_list = BookList('books.csv') self.book_list_adapter = ListAdapter(data=[], args_converter=self.book_convert, cls=ListItemButton, selection_mode='single', allow_empty_selection=False) self.refresh_books(completed=False) # self.add_widget(self.book_list_view) def refresh_books(self, completed: bool): book_list_data = [ book for book in self.book_list.sorted() if book.completed == completed ] self.book_list_adapter.data = book_list_data total_pages = self.book_list.get_total_pages(completed) if completed: self.status_bar_text = 'Select a book...' self.total_pages_label_text = ( 'Total pages completed: {}'.format(total_pages)) else: self.status_bar_text = 'Click books to mark them as completed' self.total_pages_label_text = ( 'Total pages to read: {}'.format(total_pages)) def book_convert(self, row_index, book): if book.is_long(): book_color = (0.5, 0, 0, 1) # red else: book_color = (0, 0.5, 0, 1) # green return { 'text': book.title, 'on_press': self.book_click, 'size_hint_y': None, 'height': 25, 'deselected_color': book_color, 'selected_color': book_color } def book_click(self, button): selected_book = self.book_list.get_by_title(button.text) if selected_book.completed: self.status_bar_text = str(selected_book) else: selected_book.mark_completed() self.refresh_books(False) self.book_list.save()
def __init__(self, books_filename, authors_filename, books_authors_link_filename): self.all_books = BookList(books_filename) self.all_authors = AuthorList(authors_filename) self.all_book_author_links = BookAuthorLinkList(books_authors_link_filename) pass
class BooksDataSource: def __init__(self, books_filename, authors_filename, books_authors_link_filename): self.all_books = BookList(books_filename) self.all_authors = AuthorList(authors_filename) self.all_book_author_links = BookAuthorLinkList(books_authors_link_filename) pass def book(self, book_id): # if book_id isn't an integer, raise TypeError if type(book_id) != int: raise TypeError book = self.all_books.get_book_from_id(book_id) # if book_id doesn't return a book, raise ValueError if book == {}: raise ValueError return book def books(self, *, author_id=None, search_text=None, start_year=None, end_year=None, sort_by='title'): # create master book_list of all books book_list = [] book_obj_list = self.all_books.book_list for book_obj in book_obj_list: book_list.append(book_obj.get_book()) # filter_list is a list of books to filter from master book_list filter_list = [] # if search parameter is specified, gets those books # filters other books from master list if author_id != None: filter_list = self.all_books.get_book_from_author_id(author_id, self.all_book_author_links) book_list = self.all_books.filter_book_list(book_list, filter_list) if search_text != None: filter_list = self.all_books.get_book_from_search_text(search_text) book_list = self.all_books.filter_book_list(book_list, filter_list) if start_year != None: filter_list = self.all_books.get_book_from_start_year(start_year) book_list = self.all_books.filter_book_list(book_list, filter_list) if end_year != None: filter_list = self.all_books.get_book_from_end_year(end_year) book_list = self.all_books.filter_book_list(book_list, filter_list) # sorts book_list by sort_by book_list = self.all_books.sort_book_list(sort_by, book_list=book_list) return book_list def author(self, author_id): # if author_id not an integer, raise TypeError if type(author_id) != int: raise TypeError author = self.all_authors.get_author_from_id(author_id) # if author_id didn't return an author, raise ValueError if author == {}: raise ValueError return author def authors(self, *, book_id=None, search_text=None, start_year=None, end_year=None, sort_by='birth_year'): # creates a master author_list of all authors author_list = [] author_obj_list = self.all_authors.author_list for author_obj in author_obj_list: author_list.append(author_obj.get_author()) # filter_list is a list of authors to filter from master list filter_list = [] # if search parameter is specified, gets those authors # filters other authors from master list if book_id != None: filter_list = self.all_authors.get_author_from_book_id(book_id, self.all_book_author_links) author_list = self.all_authors.filter_author_list(author_list, filter_list) if search_text != None: filter_list = self.all_authors.get_author_from_search_text(search_text) author_list = self.all_authors.filter_author_list(author_list, filter_list) if start_year != None: filter_list = self.all_authors.get_author_from_start_year(start_year) author_list = self.all_authors.filter_author_list(author_list, filter_list) if end_year != None: filter_list = self.all_authors.get_author_from_end_year(end_year) author_list = self.all_authors.filter_author_list(author_list, filter_list) # sorts author_list by sort_by author_list = self.all_authors.sort_author_list(sort_by, author_list=author_list) return author_list
from book import Book from booklist import BookList # test empty book (defaults) book = Book("", "", 0, 'r') print(book) assert book.author == "" assert book.title == "" assert book.pages == 0 # test initial-value book book2 = Book("Fish fingers", "Dory", 2, 'r') # test mark_completed() book2.mark() assert book2.flag == 'c' bl = BookList() bl.add_book(book2) assert len(bl.completelist) == 1 book3 = Book("Simple", 'Hegns', 100, 'r') bl.add_book(book3) assert len(bl.requirelist) == 1 bl.complete_book(book3) assert len(bl.completelist) == 2 print("all tests passed!")