Archive
pickle
“The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy.” It’s a.k.a. serialization. (source)
For writing this entry, I also used this post on PyMOTW.
By default, the pickled byte stream contains ASCII characters only. But it’s fine, it makes debugging easier.
The cpickle module is a C implementation, which is a lot faster than the pure Python pickle module.
The pickle format is specific to Python, so you can use it only between two Python programs.
Warning! The pickle module is not intended to be secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.
#!/usr/bin/env python
try:
import cPickle as pickle
except:
import pickle
def main():
data1 = [ { 'a':'one', 'b':2, 'c':3.0 } ]
print 'DATA: ',
print(data1)
data1_string = pickle.dumps(data1) # here: pickling
print 'PICKLE:', data1_string
data2 = pickle.loads(data1_string) # here: unpickling
print 'UNPICKLED:',
print(data2)
print 'SAME?:', (data1 is data2)
print 'EQUAL?:', (data1 == data2)
####################
if __name__ == "__main__":
main()
Output:
DATA: [{'a': 'one', 'c': 3.0, 'b': 2}]
PICKLE: (lp1
(dp2
S'a'
S'one'
p3
sS'c'
F3
sS'b'
I2
sa.
UNPICKLED: [{'a': 'one', 'c': 3.0, 'b': 2}]
SAME?: False
EQUAL?: True
“When working with your own classes, you must ensure that the class being pickled appears in the namespace of the process reading the pickle. Only the data for the instance is pickled, not the class definition. The class name is used to find the constructor to create the new object when unpickling.” (source)
That is, when you want to unpickle instances of a class, don’t forget to import the definition of this class!
