0

Alright so what I am trying to do is to get objects to save in list form when a user creates a NoteSet. It appends the objects to the list db properly when I input NoteSet('ex','example',True). I made a function called makeNewNoteSet() and it seems to be working correctly but it doesnt append to the db list. I can not figure out why.

import sys
import datetime
import pickle
notesets = []
db = []
def save():
    global db
    filename = "notesets.dat"
    file = open(filename, "wb")
    if file == None:
        print("There was an error creating your file")
        return
    pickle.dump(db, file)
    file.close()
    print("Saved words to ",filename)
def load():
    global db
    filename = "notesets.dat"
    file = open(filename, "rb")
    db = pickle.load(file)
    print("There are ",len(db)," Note Sets")
    file.close()

class NoteSet:    
    nextseqNum = len(db)+2
    def __init__(self,name,description,hidden):
        global db
        self.seqNum = NoteSet.nextseqNum
        self.name        = name
        self.description = description
        self.dateCreated = datetime.date.today()
        self.hidden      = hidden
        self.notes       = list()
        NoteSet.nextseqNum += 1
        print(self)
        notesets.append(self)
        notelist = [self.seqNum,self.name,self.description,self.dateCreated,self.hidden,self.notes]
        print(notelist)
        db.append(notelist)

        NoteSet.nextseqNum  += 1
    def __str__(self):
            printstr = str(self.seqNum),self.name,self.description,str(self.dateCreated)
            printstr = str(printstr)
            return printstr

class Note:
    nextseqNum = 0
    def __init__(self,text,dateCreated,description,category,priority,hidden):
        self.text        = text
        self.dateCreated = str
        self.dateRead    = str
        self.description = str
        self.category    = str
        self.priority    = int
        self.hidden      = bool
        self.seqNum      = Note.nextseqNum
        Note.nextseqNum  += 1

def main():
    while True:
        load()
        printMainMenu()
        selection = int(input("? "))

        if selection == 1:
            listNoteSets()
        elif selection == 2:
            listAllNoteSets()
        elif selection == 3:
            makeNewNoteSet()
        elif selection == 4:
            selectNoteSet()    # this makes the working note set
        elif selection == 5:
            deleteNoteSet()
        elif selection == 6:
            sys.exit()
        else:
            print("Invalid choice")

def printMainMenu():
    print("1.  List note sets")
    print("2.  List all note sets (including hidden sets)")
    print("3.  Make a new note set")
    print("4.  Select a working note set")
    print("5.  Delete a note set")
    print("6.  Quit")

def listNoteSets():
    num = 0
    for row in db:
        if db[num][4] == False:
            print('#',db[num][0],'   ',db[num][1],'----',db[num][2])
        num += 1
        input("[Press return to see main menu]")
        main()
def listAllNoteSets():
    num = 0
    for row in db:
        print('#',db[num][0],'   ',db[num][1],'----',db[num][2])
        num += 1
        input("[Press return to see main menu]")
        main()
def makeNewNoteSet():
    num = 0
    name = input("What would you like to name your note set? ---  ")
    for row in db:
        if name == db[num][1]:
            print("That note set has already been created")
            makeNewNoteSet()                
    description = input("Describe your note set briefly ---  ")
    hidden      = input("Would you like this note set to be hidden? ---  ")
    if hidden == 'y' or 'yes':
        hidden = True
    else:
        hidden = False
    NoteSet(name, description, hidden)
    print("noteset created you can now access it through the menu")
    input("[Press enter to return to menu]")
    main()
def selectNoteSet():
    num = 0
    for row in db:
        print('#',db[num][0],'   ',db[num][1],'----',db[num][2])
        num += 1
    response = input("Enter the number assosciated with the noteset you would like to access")
    print("Note set #",response," was selected")


main()
3
  • I don't see any code in MakeNewNoteSet that adds to db. Commented Oct 7, 2014 at 0:45
  • 1
    @alexis: It's hard to find, it's in the NoteSet.__init__ method. I'm still trying to figure out what might be wrong. This code would certainly not pass a code review! Commented Oct 7, 2014 at 0:46
  • db.append(notelist) in NoteSet Class Commented Oct 7, 2014 at 0:46

1 Answer 1

2

After you add a new note in makeNewNoteSet(), you call main() which calls load() which overwrites the in-memory copy of the database you just changed. You probably want to call save() somewhere in there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.