Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python question 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

python question

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 create_book(title,fname,lname,genre,publisher,year,id):

"""Create a new book entry.

Args:

title (str): the book's title

fname (str): the author's first name

lname (str) : the author's last name

genre (str): the book's genre

publisher (str): the book's publisher

year (int): the year of publication

id (int): the book's unique id

Returns:

dict: a book entry

"""

pass

def delete_book(books,id):

"""Remove the book identified by the specified id.

Args:

books (list): a copy of the books db

id (int): the id of the book to delete

Returns:

list: the updated list of books

"""

# Add your book deletion code here

pass

def sort_books(books, key):

"""Sort a list of books based on the specified key.

The key should be one of the keys of the book dictionary.

Args:

books (list): a list of books

key (str): the sorting key

Returns:

list: a list of books sorted by key

"""

pass

def search(books, key, value):

"""Search a list of books based on the specified key and value.

Restricted to searching by title or author's name only.

If the key is 'title', the search matches all books whose title

contains any of the search terms.

If the key is 'name', the search term is a single word and

the search matches all books whose author's first name or

last name equals the search term.

Args:

books (list): a copy of the books db

key (str): one of ['title', 'name']

value (str): the search term

Returns:

list: a list of books matching the search criteria

"""

pass

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 key to keep the old value. "

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

What is meant by decentralisation of authority ?

Answered: 1 week ago

Question

Briefly explain the qualities of an able supervisor

Answered: 1 week ago

Question

Define policy making?

Answered: 1 week ago

Question

Define co-ordination?

Answered: 1 week ago

Question

What are the role of supervisors ?

Answered: 1 week ago

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago