Reputation: 185
I want to save state of poll class data to file and if my script restarts load it back. I popped out part of my program to replicate the problem. here are my files.
pickleclass.py
#POLL RECORD
class POLL:
title = ''
votes = {}
duration = 0
secenekler = 0
sure = ""
polltype = -1 # -1 initial, 0 = %, 1 = Sayi
chat_id = None
message_id = None
def reset():
POLL.title = ''
POLL.votes.clear()
POLL.duration = 0
POLL.secenekler = 0
POLL.sure = ""
POLL.polltype = -1
POLL.chat_id = None
POLL.message_id = None
fxns.py
def save_to_file(obj, filename):
"""
Saves obj to file named with fn as pickly object.
"""
import pickle
with open(filename, 'wb') as output: # Overwrites any existing file.
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def load_from_file(fn):
import pickle
"""
reads from file given in fn and returns object of dump pickle file
"""
return pickle.load( open( fn, "rb" ) )
save.py
import pickleclass as pk
import fxns as fxn
Poll_file = "p.dump"
poll = pk.POLL
poll.title = "TEST TITLE"
poll.votes['VOTE 1'] = 1
poll.votes['VOTE 2'] = 2
poll.votes['VOTE 3'] = 3
poll.duration = 0.4
poll.secenekler = 1
poll.sure = "23:55"
poll.polltype = 1
poll.chat_id = 112431
poll.message_id = 324
print("-"*55)
print("-"*55)
bozo = vars(poll)
for key in bozo.keys():
print(key, "=", bozo[key])
print("-"*55)
fxn.save_to_file(poll, Poll_file)
First i call save.py to create the class and then save it. its successfully ends. after the save.py script i call below load.py to load what is saved. but it loads empty class data. as it was newly created. output of the save.py file is as follows:
('reset', '=', <function reset at 0x7f4485277758>)
('__module__', '=', 'pickleclass')
('sure', '=', '23:55')
('secenekler', '=', 1)
('title', '=', 'TEST TITLE')
('__doc__', '=', None)
('votes', '=', {'VOTE 2': 2, 'VOTE 3': 3, 'VOTE 1': 1})
('polltype', '=', 1)
('chat_id', '=', 112431)
('duration', '=', 0.4)
('message_id', '=', 324)
load.py
import pickleclass as pk
import fxns as fxn
Poll_file = "p.dump"
zozo = fxn.load_from_file(Poll_file)
zozo = vars(zozo)
for key in zozo.keys():
print(key, "=", zozo[key])
print("-"*55)
print("-"*55)
and when i load the file and show the output, its empty like below.
('reset', '=', <function reset at 0x7f8e99907758>)
('__module__', '=', 'pickleclass')
('sure', '=', '')
('secenekler', '=', 0)
('title', '=', '')
('__doc__', '=', None)
('votes', '=', {})
('polltype', '=', -1)
('chat_id', '=', None)
('duration', '=', 0)
('message_id', '=', None)
I couldn't find out the problem. it loads the class but not the data.
Upvotes: 1
Views: 1013
Reputation: 1981
The load was performed correctly: the problem comes from your class which is not implemented properly.
Have a look at some material on classes and instances (or some posts on SO), especially the __init__
function, self
, and notions like instance and class members (see also a closely related problem in that post).
Then look at how pickle
handles classes and you should be good to go.
Edit apparently with dill
this should actually be possible, see this SO post
Upvotes: 1