Differences between revisions 1 and 9 (spanning 8 versions)
Revision 1 as of 2004-04-22 19:52:29
Size: 517
Editor: dsl254-010-130
Comment: Basic framework of a page.
Revision 9 as of 2004-07-24 20:42:21
Size: 3473
Editor: twgate
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
 * [http://www.python.org/doc/current/lib/node64.html Official Pickle Use Documentation] - as usual, no example code :(  * [http://www.python.org/doc/current/lib/node64.html Official Pickle Use Documentation]
* [http://www.python.org/doc/current/lib/pickle-example.html Official Pickle Example]
Line 8: Line 9:
== Code Example ==

''Need some code here, when it's figured out.''
== Pickle Example ==
Line 14: Line 13:
# Relevant Python code
class Something():
  pass
# Save a dictionary into a pickle file.
import pickle
Line 18: Line 16:
x=Something()
print x
favorite_color = { "lion": "yellow", "kitty": "red" }

pickle.dump( favorite_color, open( "save.p", "w" ) )
Line 21: Line 20:

{{{
#!python
# Load the dictionary back from the pickle file.
import pickle

favorite_color = pickle.load( open( "save.p" ) )
# favorite_color is now { "lion": "yellow", "kitty": "red" }
}}}

For a more complex example, see [http://www.python.org/doc/current/lib/pickle-example.html the official Pickle example,] and for API details, see the [http://www.python.org/doc/current/lib/node64.html official Pickle use documentation.]

== cPickle ==

You can speed up your pickle access with ''cPickle.''

The only difference is that you write...

{{{
#!python
import cPickle as pickle
}}}

...at the top of your file, and now all your pickle calls are ''much'' faster. [http://www.python.org/doc/current/lib/module-cPickle.html ("...up to 1000 times faster.")]

Why would you ever use native pickle, rather than cPickle? I don't know.
 ''Because pickle handles unicode objects.''

== Flying Pickle Alert! ==

Pickle files can be hacked. If you receive a raw pickle file over the network, ''don't trust it!'' It could have malicious code in it, that would run arbitrary python when you try to de-pickle it.

However, if you are doing your own pickle writing and reading, you're safe. (Provided no one else has access to the pickle file, of course.)

You might also check out http://trustedpickle.sourceforge.net/ if you want to generate pickles, and later verify that you were the one that generated the pickle (e.g., if you are putting a pickle in a hidden field).

== What can you Pickle? ==

Generally you can pickle any object if you can pickle every attribute of that object. Classes, functions, and methods cannot be pickled -- if you pickle an object, the object's class is not pickled, just a string that identifies what class it belongs to. This works fine for most pickles (but note the discussion about long-term storage of pickles).

You ''cannot'' pickle open file objects, network connections, or database connections. When you think about it, it makes sense -- pickle cannot will the connection for file object to exist when you unpickle your object, and the process of creating that connection goes beyond what pickle can automatically do for you. If you really want to pickle something that has an attribute that is causing problems, look at the pickle documentation for {{{__getstate__}}}, {{{__setstate__}}}, and {{{__getinitargs__}}} -- using these you can exclude problematic attributes.
Line 24: Line 64:
LionKimbro LionKimbro, IanBicking
Line 28: Line 68:
  (none yet) Pickles can cause problems if you save a pickle, then update your code and read the pickle in. Attribute added to your {{{__init__}}} may not be present in the unpickled object; also, if pickle can't find your class and module (e.g., if you renamed the module) you will get errors.

For this reason, you should be wary of using pickles for long-term storage where the underlying code is not highly stable.

Using Pickle

Pickle Example

   1 # Save a dictionary into a pickle file.
   2 import pickle
   3 
   4 favorite_color = { "lion": "yellow", "kitty": "red" }
   5 
   6 pickle.dump( favorite_color, open( "save.p", "w" ) )

   1 # Load the dictionary back from the pickle file.
   2 import pickle
   3 
   4 favorite_color = pickle.load( open( "save.p" ) )
   5 # favorite_color is now { "lion": "yellow", "kitty": "red" }

For a more complex example, see [http://www.python.org/doc/current/lib/pickle-example.html the official Pickle example,] and for API details, see the [http://www.python.org/doc/current/lib/node64.html official Pickle use documentation.]

cPickle

You can speed up your pickle access with cPickle.

The only difference is that you write...

   1 import cPickle as pickle

...at the top of your file, and now all your pickle calls are much faster. [http://www.python.org/doc/current/lib/module-cPickle.html ("...up to 1000 times faster.")]

Why would you ever use native pickle, rather than cPickle? I don't know.

  • Because pickle handles unicode objects.

Flying Pickle Alert!

Pickle files can be hacked. If you receive a raw pickle file over the network, don't trust it! It could have malicious code in it, that would run arbitrary python when you try to de-pickle it.

However, if you are doing your own pickle writing and reading, you're safe. (Provided no one else has access to the pickle file, of course.)

You might also check out http://trustedpickle.sourceforge.net/ if you want to generate pickles, and later verify that you were the one that generated the pickle (e.g., if you are putting a pickle in a hidden field).

What can you Pickle?

Generally you can pickle any object if you can pickle every attribute of that object. Classes, functions, and methods cannot be pickled -- if you pickle an object, the object's class is not pickled, just a string that identifies what class it belongs to. This works fine for most pickles (but note the discussion about long-term storage of pickles).

You cannot pickle open file objects, network connections, or database connections. When you think about it, it makes sense -- pickle cannot will the connection for file object to exist when you unpickle your object, and the process of creating that connection goes beyond what pickle can automatically do for you. If you really want to pickle something that has an attribute that is causing problems, look at the pickle documentation for __getstate__, __setstate__, and __getinitargs__ -- using these you can exclude problematic attributes.

Contributors

LionKimbro, IanBicking

Discussion

Pickles can cause problems if you save a pickle, then update your code and read the pickle in. Attribute added to your __init__ may not be present in the unpickled object; also, if pickle can't find your class and module (e.g., if you renamed the module) you will get errors.

For this reason, you should be wary of using pickles for long-term storage where the underlying code is not highly stable.

UsingPickle (last edited 2019-12-15 07:34:41 by FrancesHocutt)

Unable to edit the page? See the FrontPage for instructions.