Question: python import csv import json import pathlib from operator import itemgetter def load_db(path): Load books from the database csv file. Args: path (pathlib.Path): path to
python
import csv
import json
import pathlib
from operator import itemgetter
def load_db(path):
"""Load books from the database csv file.
Args:
path (pathlib.Path): path to the csv file
Returns:
list: a list of books where each book is a dictionary
"""
pass
def save_db(books, path):
"""Write a list of books to csv file.
Args:
books (list): a list of books where each book is a dictionary
path (pathlib.Path): the path to the csv file
Returns:
None
"""
pass
def fullname(fname, lname):
"""Join arguments into a full name"""
return f"{fname}, {lname}"
def id_generator(books):
"""Generate the id of the next book to be added to
the database.
Ids are assigned incrementally, so we take the current
largest id in the database and add 1 to get the next id.
This is useful when creating new books, so that we don't
have to guess what the next id should be.
"""
return max(int(book['id']) for book in books) + 1
def display_books(books):
if books:
for book in books:
print(format_book(book))
else:
print('There are no books to display')
def format_book(book):
"""Return a formatted representation of the book"""
pass
def get_book(books, id):
"""Retrieve a single book from the database.
Args:
books (str): a copy of the books db
id (int): the book's unique id
Returns:
dict: the book with the specified id if found
None: if the book was not found
"""
for book in books:
if int(book['id']) == id:
return book
return None
def filter_books(books, key, value):
"""Filter books by the specified key.
Args:
books (list): a copy of the books db
key (str): the filter key, one of the keys
of the book dictionary
value (any): the filter criterion
Returns:
list : a list of books matching the filter criteria
"""
pass
def update_book(books, id):
"""Edit the details of the book matching the specified id.
Args:
books (list): a copy of the books db
id (int): the unique id of the book to update
Returns:
list: a list of books containing the updated book
"""
book = get_book(books, id)
if not book:
print(f"The book with id: {id} was not found")
return False
for key in book.keys():
new_value = input("Press
f"Enter {key!r} new value: ").strip()
if new_value:
book[key] = new_value
return books
def display_main_menu():
print('Please choose an action to perform: ')
print('1. Show all books')
print('2. Search books')
print('3. Add books')
print('4. Update book')
print('5. Delete book')
print('6. Quit')
def perform_action(action):
db_path = 'books_db.csv'
books = load_db(db_path)
if action == 1:
# implement the following display options:
# 1. Display all books unsorted
# 2. Filter books according to some criteria
# 3. Display books sorted according to some criteria
display_books(books)
if action == 2:
# perform search action
display_books(books)
if action == 3:
# Perform add books action
# Add the new books to the book database
# by calling save_db()
pass
if action == 4:
id = int(input('Enter book id: ').strip())
books = update_book(books, id)
if books:
save_db(books, db_path)
if action == 5:
# Perform delete book action
# If deletion is successful,
# save the updated list of books to the database
pass
def main():
while True:
display_main_menu()
action = int(input('> ').strip())
if action == 6:
break
else:
perform_action(action)
if __name__ == '__main__':
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
