Troll_Hunter
Troll_Hunter

Reputation: 515

How does Python pickle save objects?

I want to know if I can save an object with multiple values/properties into one pickle file or do I have to save each value independently? Here is the code I have now:

def __init__(self, id, targets, binaries):
    self.id = id
    self.targets = targets
    self.binaries = binaries

with open('PI0_Electron_data.pickle', 'wb') as output:
    pickle.dump(PiElectron, output)

For better understanding id is an integer and both targets and binaries are numpy arrays. Will I be able to get the id, targets, and binaries of the object from this single pickle file or must I create three pickle files? Also how would I extract the data from the pickle file?

Upvotes: 0

Views: 854

Answers (1)

Mike McKerns
Mike McKerns

Reputation: 35217

Your code should look something like this:

>>> class Thing(object):
...   def __init__(self, id, targets, binaries):
...     self.id = id
...     self.targets = targets
...     self.binaries = binaries
... 
>>> import numpy as np
>>> t = Thing(1, np.arange(3), np.arange(3,9,2))
>>> 
>>> import dill
>>> with open('electron_data.pkl', 'w') as f:
...   dill.dump(t, f)
... 
>>>

Where I've used dill to give you better serialization… essentially, dill can pickle class instances easily -- as well as most python objects. Then when you want the object back you need to load.

Python 2.7.10 (default, May 25 2015, 13:16:30) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> with open('electron_data.pkl', 'r') as f:
...   t = dill.load(f)
... 
>>> t
<__main__.Thing object at 0x100394410>
>>> t.id
1
>>> t.targets
array([0, 1, 2])
>>> t.binaries
array([3, 5, 7])
>>>
>>> print dill.source.getsource(t.__class__)
class Thing(object):
  def __init__(self, id, targets, binaries):
    self.id = id
    self.targets = targets
    self.binaries = binaries

>>> 

I hope that helps answer your question… if your actual code is more complex (I'm assuming it is), it should still work unless there's an unpicklable object.

Upvotes: 2

Related Questions