pythonpickleenaml

How to serialize Atom api objects without losing 'connections'


I'm using an Atom based Enaml gui and I would like to save/load the atom objects to/from disk.

I have tried using pickle, but I lose the connections to the gui.

from atom.api import Atom, Unicode
import pickle

class test(Atom):
     name = Unicode

     def save(self)
          f = open('tester.p','w')
          pickle.dump(self.name,f)
          f.close()

     def load(self):
          f = open('tester.p','r')
          self.name = pickle(f)  # gui is not updated
          #self.name = 'this one works' #gui is updated
          f.close()

How can I serialize Atom objects without losing the gui connections?

Edit: I have found out that if i do self.name = str(pickle(f)) I can get the string without losing the connections. How can I extend this idea to something more generalizable, like a dictionary with arbitrary Atom objects?


Solution

  • See this stackoverflow question and answer to see how to use setattr to keep the Atom update mechanism working.