Python Forum

Full Version: I'm having trouble with an OOP version of Pickle functionality
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Well I'm pretty novice at OOP so I thought I'd take one of my working non-OOP version of a Pickle program and create this object-oriented version:

import pickle

'''An object-oriented example of file pickling.'''


class Pickles:

    def __init__(self, count, coomo, f):
        self.count = count
        self.coomo = coomo
        self.f = f

    def to_pickle(self):
        pickle.dump(self.count, self.f)
        pickle.dump(self.coomo, self.f)

    def __del__(self):
        f.close()


f = open('pickles1.dat', 'wb')
count = ['vun', 'two', 'three']
coomo = ['om', 'nom', 'nom']
The problem is that it doesn't work. It doesn't throw any errors, but when I use an un-pickling program to open the pickled file, I get an error stating the following:

Error:
Traceback (most recent call last): File "picklingun_ex.py", line 7, in <module> count = pickle.load(f) EOFError: Ran out of input
I took a look at the location of the pickles1.dat file and it wasn't saved as a binary file, but a plaintext file. So it seems that I've done something wrong with my object-oriented Pickling program, but I'm not sure what.

Any suggestions? I'm really stumped at the moment. Confused
Is that all of your code? You create a class, but then never instance it? The file "pickles1.dat" should be completely empty, since you never use it, right?
(Dec-19-2018, 05:34 PM)nilamo Wrote: [ -> ]Is that all of your code? You create a class, but then never instance it? The file "pickles1.dat" should be completely empty, since you never use it, right?

And there's my problem. For those reading this post my solution was to append the following lines to the end of the script:

pickled = Pickles(count, coomo, f)
pickled.to_pickle()
del(pickled)
Good grief, such an amateur mistake on my part. Wall

Thank you for the help! Big Grin