Jun-14-2019, 06:22 PM
(This post was last modified: Jun-15-2019, 07:55 PM by Beginner_coder123.)
Hello everyone, I'm trying to fix some methods such as "restore a backup" in my public library system.. But I've been stuck for weeks. Can someone help me out? The code is below, but it's a lot. Not that complicated to read, I hope. I basically expect to be able to make a book, and put it in my JSON file. I already have an existing JSON file, but it doesn't append it to file. But instead it keeps it in the dictionary I made it in, and when I run the "make backup" method, it appends it in my backed up file. How can I make it so that you can add a book straight into the JSON file that's being loaded? That is booklist.json
"""TODO: Done - filling the system with books from a file of books in JSON format (file with books). You may add a fake ISBN number to book items and generate the number of book items per book. Done - filling the system with customers from a file of people ( file). Done - adding a book item. Done - adding a customer. Done - searching a book. Done - making a book loan. Done - making a backup of the system in JSON format. Not Done - restoring the system from a backup. """ import json import csv """ Created class 'Book' for adding a new book, Returning the new dict() back to the class 'bookItems' where it will be added to the bookcollection """ class Book: @staticmethod def createBook(FileBookcollection): #parameters is for the inputUser. So it shows the right text for the user parameters = ["author","country","imageLink","language","link","pages","title","year"] newBook = dict() for item in parameters: inputUser = input("Fill in the " + item + " of the book: ") if inputUser == "": newBook[item] = None else: #adding the parameters as key and inputUser as value to the dict newBook newBook[item] = inputUser #Adding ISBN to the Dict with a counter that counts the Len(Dict) + 1 newBook["ISBN"] = str(len(FileBookcollection) + 1) return newBook def restoreBooksBackup(this): with open(r".\bookCollectionBackup.json") as jsonFile: bookList = json.load(jsonFile) this.JSONFileBookCollection = bookList def makeBackup(this): with open(r".\backupFakeNames.csv","w",newline="") as output: new_csv = csv.writer(output) new_csv.writerows(this.allPersonsInfo) with open(r".\bookCollectionBackup.json","w") as jsonFile: json.dump(this.bookCollection.getAllBooks(),jsonFile) with open(r".\loanbookListBackup.json","w") as jsonFile: json.dump(this.loanBookList,jsonFile) def restoreBackUp(this): pass """ with open(r".\bookCollectionBackup.json") as jsonFile: bookList = json.load(jsonFile) this.bookCollection.dumbJSON.JSONFileBookCollection = bookList with open(r".\backupFakeNames.csv","r") as output: new_csv = csv.reader(output) totalPersonListBackup = [] for line in new_csv: totalPersonListBackup.append(line) this.AllCustomers.allPersonsInfo.personsList = totalPersonListBackup #this.bookCollection.restoreBooksBackup() with open(r".\loanbookListBackup.json") as jsonFile: fileLoanBookBackup = json.load(jsonFile) this.loanBookList = fileLoanBookBackup """