Python Forum
I'm having trouble with an OOP version of Pickle functionality
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm having trouble with an OOP version of Pickle functionality
#1
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
Reply
#2
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?
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 378 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  Priority Queue with update functionality PythonNewbee 4 1,913 Dec-29-2022, 12:13 PM
Last Post: Yoriz
  pickle problem DPaul 13 4,748 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 2,648 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 15,985 Aug-30-2021, 02:49 PM
Last Post: law
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 1,983 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  Save/Loading using pickle Scordomaniac 4 3,029 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  computing entropy using pickle files baran01 2 2,424 Dec-30-2019, 09:45 PM
Last Post: micseydel
  Validating the functionality of the application rpalakodety 1 1,767 Dec-30-2019, 07:58 PM
Last Post: ndc85430
  Tkinter don't get ver from file via pickle storzo 2 2,559 Jul-31-2019, 03:50 PM
Last Post: storzo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020