本文整理匯總了Python中book.Book.book_by_record方法的典型用法代碼示例。如果您正苦於以下問題:Python Book.book_by_record方法的具體用法?Python Book.book_by_record怎麽用?Python Book.book_by_record使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類book.Book
的用法示例。
在下文中一共展示了Book.book_by_record方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: add_record
# 需要導入模塊: from book import Book [as 別名]
# 或者: from book.Book import book_by_record [as 別名]
def add_record(self, book):
"""
Adds a new record of a book to the file.If there is a book record
with the same title,author and year of publication then the book
is recorded and we only change the number of copies of the
recorded book.Otherwise, we add the new book.
"""
with open(self.file, 'r') as file:
data = file.readlines()
try:
_file = self.file
enum = enumerate(data)
I = next(i for i, v in enum if Book.book_by_record(v) == book)
os.rename(os.path.realpath(_file), os.path.realpath(_file)+".bak")
copies = int(data[I].rstrip('\n').split('+')[-1])
book_update = book.special_record().rstrip('\n').split('+')
book_update[-1] = str(int(book_update[-1]) + copies)
data[I] = '+'.join(book_update) + '\n'
with open(_file, 'w') as file:
file.writelines(data)
os.remove(os.path.realpath(self.file)+".bak")
except StopIteration:
data.append(book.special_record())
with open(self.file, 'a') as file:
file.write(book.special_record())
示例2: test_book_by_record
# 需要導入模塊: from book import Book [as 別名]
# 或者: from book.Book import book_by_record [as 別名]
def test_book_by_record(self):
book = Book.book_by_record("Fall+Carl Jon+1999+Crime+3+2")
self.assertEqual(
"<Book Information>\nTitle:Fall\nAuthor:Carl "
+ "Jon\nPublished in:1999\nGenre:Crime\n"
+ "Rating:3.0\nNumber of copies:2\n",
str(book),
)
示例3: booklist
# 需要導入模塊: from book import Book [as 別名]
# 或者: from book.Book import book_by_record [as 別名]
def booklist(self):
"""
Returns a list of the books that are recorded in the file.
"""
with open(self.file, 'r') as file:
return [Book.book_by_record(book) for book in file.readlines()]