Friday 15 July 2011

Save to file and load an instance of a python class with its attributes -


I am working with Python for data analysis and finding a good example of saving a class I am hoping that later it can be reloaded the values ​​of its properties. Specifically, an example of this class will load an object that is an example of another class, then the processors make intensive comparisons that take some time.

For example:

  Oil Attainable Classes MyCalus (): def __init __ (self): self.info = Other class (option = 1) DEF ork (self ): F = file ('test_file', 'wb') pickle.dump (self, f, pickleHHEEEEPETOCL) F.Clos () DEP Unpical (SF): F = file ('Test_file', 'RB'). Lokal Load (F) F.Clos () Class Other Class (): Df __int_ (Self, Options): Self.property = option * 2 mydata = myClass (option = 5) mydata.pickle ()  < / Pre> 

Then import this module and run mydata.unpickle () to run mydata back (I also have an option for which it looks like the file is spicy first and calculation and other class charge Is suspended on suspension). Right now, if I do this I can load mydata, but now mydata.info is not an object. My understanding is that this is a deliberate piece of pickle, but is there a way to completely save my data so that I can use it later?

You are not doing anything with the data you load. MyClass (): ... @staticmethod def unpickle (): with file ('test_file', 'rb') as f: return pickle.load (f) data = MyClass.unpickle ()

No comments:

Post a Comment