(10 ratings)   
By: Patrick O'Brien
Python pickling support comes from the pickle module, and its cousin, the cPickle module. The latter was coded in C to provide better performance and is the recommended choice for most applications. We'll continue to talk about pickle, but our...
Added: 02 June 2008    Views: 242  
PathComputers    Programming    Python
Keywords: computers   python   programming   code   coder   language   coding  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 
Python pickling support comes from the pickle module, and its cousin, the cPickle module. The latter was coded in C to provide better performance and is the recommended choice for most applications. We'll continue to talk about pickle, but our examples will actually make use of cPickle. Since most of our examples will be shown from the Python shell, let's start by showing how to import cPickle while being able to refer to it as pickle:

>>> import cPickle as pickle
Now that we've imported the module, let's take a look at the pickle interface. The pickle module provides the following function pairs: dumps(object) returns a string containing an object in pickle format; loads(string) returns the object contained in the pickle string; dump(object, file) writes the object to the file, which may be an actual physical file, but could also be any file-like object having a write() method that accepts a single string argument; load(file) returns the object contained in the pickle file.

By default, dumps() and dump() create pickles using a printable ASCII representation. Both functions have a final, optional argument that, if True, specifies that pickles will be created using a faster and smaller binary representation. The loads() and load() functions automatically detect whether a pickle is in the binary or text format.

Listing 1 shows an interactive session using the dumps() and loads() functions just described:

Listing 1. Illustration of dumps() and loads()

Welcome To PyCrust 0.7.2 - The Flakiest Python Shell

Sponsored by Orbtech - Your source for Python programming expertise.

Python 2.2.1 (#1, Aug 27 2002, 10:22:32)

[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386

Type "copyright", "credits" or "license" for more information.

>>> import cPickle as pickle

>>> t1 = ('this is a string', 42, [1, 2, 3], None)

>>> t1

('this is a string', 42, [1, 2, 3], None)

>>> p1 = pickle.dumps(t1)

>>> p1

"(S'this is a string'\nI42\n(lp1\nI1\naI2\naI3\naNtp2\n."

>>> print p1

(S'this is a string'

I42

(lp1

I1

aI2

aI3

aNtp2

.

>>> t2 = pickle.loads(p1)

>>> t2

('this is a string', 42, [1, 2, 3], None)

>>> p2 = pickle.dumps(t1, True)

>>> p2

'(U\x10this is a stringK*]q\x01(K\x01K\x02K\x03eNtq\x02.'

>>> t3 = pickle.loads(p2)

>>> t3

('this is a string', 42, [1, 2, 3], None)
Notice that the text pickle format isn't too difficult to decipher. In fact, the conventions used are all documented in the pickle module. We should also point out that with the simple objects used in our example, there wasn't much space efficiency gained by using the binary pickle format. However, in a real system with complex objects, you will see a noticable size and speed improvement with the binary format.

Next we'll look at some examples using dump() and load(), which work with files and file-like objects. These functions operate much like the dumps() and loads() that we just looked at, with one additional capability -- the dump() function allows you to dump several objects to the same file, one after the other. Subsequent calls to load() will retrieve the objects in the same order. Listing 2 shows this capability in action:

Listing 2. Example of dump() and load()

>>> a1 = 'apple'

>>> b1 = {1: 'One', 2: 'Two', 3: 'Three'}

>>> c1 = ['fee', 'fie', 'foe', 'fum']

>>> f1 = file('temp.pkl', 'wb')

>>> pickle.dump(a1, f1, True)

>>> pickle.dump(b1, f1, True)

>>> pickle.dump(c1, f1, True)

>>> f1.close()

>>> f2 = file('temp.pkl', 'rb')

>>> a2 = pickle.load(f2)

>>> a2

'apple'

>>> b2 = pickle.load(f2)

>>> b2

{1: 'One', 2: 'Two', 3: 'Three'}

>>> c2 = pickle.load(f2)

>>> c2

['fee', 'fie', 'foe', 'fum']

>>> f2.close()
About the Author :
Patrick O\'Brien is a Python programmer, consultant, and trainer. He is the author of PyCrust and a developer on the PythonCard project. He most recently lead the PyPerSyst team that ported Prevayler to Python, and continues to lead that project into interesting new territory.
A peck of pickled Python
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments