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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
"""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 """ |