将对象保存到列表python;列表因某种原因没有更新?

时间:2014-10-07 00:40:10

标签: python list class object

好吧我正在尝试做的是在用户创建NoteSet时让对象以列表形式保存。当我输入NoteSet('ex','example',True)时,它会正确地将对象附加到列表db。我创建了一个名为makeNewNoteSet()的函数,它似乎工作正常,但它不会附加到db列表。我无法弄清楚原因。

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()

1 个答案:

答案 0 :(得分:2)

makeNewNoteSet()中添加新笔记后,调用main()调用load(),这会覆盖您刚更改的数据库的内存副本。你可能想在那里的某处调用save()