本文整理汇总了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()]