0

Iv'e only just learnt about the pickle library, so I have been trying to learn how to use it. But I've run into a problem. What I'm trying to do is to save (pickle) a dictionary which has a object that holds a 2d list (grid). But pickle doesn't seem to save when I change one of the values of the seats. Here's some of the code

#Save movie data
def saveData():
    pickle.dump(movieInfo, open("save.p", "wb"))

#load movie data
def loadData():
    movieInfo = pickle.load(open("save.p", "rb"))

class seats:
    def __init__(self):
        self.seating = []
        for i in range(0,10):
            self.seating.append(
                [False,False,False,False,False,False,False,False,False,False]
            )

loadData()
input("continue...")
chooseMovie()

Any help would be much appreciated! Thank you.

  • Side-note: `self.seating = [[False] * 10 for _ in range(10)]` would accomplish the same thing as your whole `__init__` method body rather more succinctly (and likely faster too). – ShadowRanger Mar 01 '17 at 05:44
  • @ShadowRanger Thank you so much! I knew there must of been a way to make it shorter, I just was not to sure how to. Cheers! –  Mar 01 '17 at 05:45

2 Answers2

2

You have to close your file when you are done with it. Modify your code like below:

def saveData():
    with open("save.p", "wb") as pickleFile:
        pickle.dump(movieInfo, pickleFile)

def loadData():
    with open("save.p", "rb") as pickleFile:
        movieInfo = pickle.load(pickleFile)
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
Dongeon Kim
  • 443
  • 4
  • 12
  • Doesn't seem to be the problem, Still running into the same problem :( –  Mar 01 '17 at 05:40
  • 1
    @NickLanglais: On CPython (the reference interpreter), the close will happen automatically in this case, though it's not something to rely on. In other interpreters, it can easily fail to close for a long time, or never close. This doesn't actually answer the question/solve the problem, but it's a good idea nonetheless. – ShadowRanger Mar 01 '17 at 05:42
1

You load the pickled file into movieInfo, which is a variable that is local to the function, and once the function loadData() exits you lose a the reference to it.

If it is a global, you should declare it:

#load movie data
def loadData():
    global movieInfo
    movieInfo = pickle.load(open("save.p", "rb"))

However, using globals is usually a bad practice, instead, it will be better to return the loaded object from the function and use it there:

#load movie data
def loadData():
    return pickle.load(open("save.p", "rb"))

# ...
moveInfo = loadData()
# ...
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Thank you so much! I'm quite new to programming so it's nice to learn all this stuff early on. Would you mind explaining with globals are a bad practice?? –  Mar 01 '17 at 05:43
  • @NickLanglais this is a bit too much for a comment - take a look at this answer - http://stackoverflow.com/a/19158418 – MByD Mar 01 '17 at 05:49
  • @NickLanglais: Short-version: What if you need two (or more) different save files, and must manipulate them independently? globals don't scale like that. You'd usually use either idempotent (no side-effect) functions which take arguments/return values with no global state, or implement a class with instance state, so each instance has it's own unique state separate from all other instances. – ShadowRanger Mar 01 '17 at 05:51
  • @ShadowRanger Interesting, Thank you very much. Damn you deserve a solid high five. You're helping me so much! –  Mar 01 '17 at 06:13
  • @ShadowRanger I just tried to integrate the version without the global variable. It seems that it runs into the same problem in the first place. This is what I changed. Sorry for bugging you. `def loadData(): return pickle.load(open("save.p", "rb"))` and `elif choice == 3: moveInfo = loadData()` –  Mar 01 '17 at 06:36
  • @NickLanglais: You're going to need to ask a new question; there's not enough there to give a useful response, and trying to put significant code in comments is doomed to failure, particularly for a whitespace sensitive language. – ShadowRanger Mar 01 '17 at 11:54