Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Pickling Objects Into Files - Page 13

June 7, 2001

Pickling is a major benefit in Python. Use this ability!


Write an arbitrary Python object with cPickle.dump.


Python can write any data structure into a file, and read that data structure back out of a file and re-create it, with just a few commands. This is an unusual ability, but one that is highly useful. It can save the programmer many pages of code which do nothing but dump the state of a program into a file (and can save a similar amount of code which does nothing but read that state back in).

Python provides this ability via the cPickle module. cPickle is actually a C language rewrite of the original pickle module. We are using it in our examples here as it is a thousand times faster than the pickle module. Pickling is very powerful but very simple to use. For example, assume that the entire state of a program is held in three variables, a, b, and c. We can save this state to a file called "state" as follows:

import cPickle
.
.
.
file = open("state", 'w')
cPickle. dump(a, file)
cPickle. dump(b, file)
cPickle. dump(c, file)
file. close()

It doesn't matter what was stored in a, b, and c. It might be as simple as numbers, or as complex as a list of dictionaries containing instances of user-defined classes. cPickle.dump will save everything.

Now, to read that data back in on a later run of the program, just say

import cPickle
file = open("state", 'r')
a = cPickle.load(file)
b = cPickle.load(file)
c = cPickle.load(file)
file. close()

Retrieve an arbitrary Python object with cPickle.load.

Any data that was previously in the variables a, b, or c will have been restored to them by cPickle.load.


cPickle can handle just about any Python object.


The cPickle module can store almost anything in this manner. The cPickle module can handle lists, tuples, numbers, strings, dictionaries, and just about anything made up of these types of objects, which includes all class instances. It also handles shared objects, cyclic references, and other complex memory structures correctly, storing shared objects only once, and restoring them as shared objects, not as identical copies. However, code objects (what Python stores byte compiled code in) and system resources like files or sockets can not be pickled.


A convenient way of using cPickle is to save your state variables into a dictionary, and then cPickle the dictionary.


More often than not, you won't want to save your entire program state with cPickle. For example, most applications can have multiple documents open at one time. If you saved the entire state of the program, you would effectively save all open documents in one file. An easy and effective way of saving and restoring only data of interest is to write a save function which stores all data you wish to save into a dictionary, and then uses cPickle to save the dictionary. Then, a complementary restore function can be used to read the dictionary back in (again using cPickle), and to assign the values in the dictionary to the appropriate program variables. This also has the advantage that there is no possibility of reading values back in an incorrect order, that is, an order different from the order in which they were stored. Using this approach with the above example, we would get code looking something like this:

import cPickle
  .
  .
  .
def saveData():
  global a, b, c
  file = open(" state", 'w')
  data = {'a' : a, 'b' : b, 'c' : c}
  cPickle.dump( data, file)
  file.close()

def restoreData():
  global a, b, c
  file = open(" state", 'r')
  data = cPickle.load( file)
  file.close()
  a = data[ 'a']
  b = data[ 'b']
  c = data[ 'c']
  .
  .

Now this is a somewhat contrived example. You probably won't be saving the state of the top-level variables of your interactive mode very often.

Output and Redirection - Page 12
The Quick Python Book
Pickling the Cache - Page 14


Up to => Home / Authoring / Languages / Python / Quick




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers