1

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.

GurhanCagin
  • 185
  • 2
  • 13

1 Answers1

1

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

Silmathoron
  • 1,781
  • 1
  • 15
  • 32
  • What i read once and try to do was like "A class can itself be used as a container or namespace. There's no need to instantiate it, and it'll save you a bit of typing if you just want a bundle of attributes." i have some classes with self and init, but in this case i only needed to hold some parameters under one section, so i did it this way. you say that to able to save with pickle of dill, i need to have full class definition with self and init inside. I will try and check. Thanks – GurhanCagin Dec 02 '18 at 10:29
  • Yep, unfortunately that does not work well for pickle, see [jdi's answer](https://stackoverflow.com/a/10842615/5962321) – Silmathoron Dec 02 '18 at 10:32
  • Apparently Dill allows that, sorry for the mistake, see edit – Silmathoron Dec 02 '18 at 10:37
  • I tried with dill also, it was same. didn't work. i try with __init__ now, and now i able to load data properly. so seems without *instantiating class*, its not possible to _save_ and _load_ properly. – GurhanCagin Dec 02 '18 at 10:41