Tuesday, August 03, 2004

co_pickle , a module to serialize code objects

Title: co_pickle , a module to serialize code objects
Submitter: Andres Tremols (other recipes) Last Updated: 2003/07/27 Version no: 1.0
Category: Shortcuts
Description: Compiles a string of python code and returns a printable representation of the byte codes, the resulting string can then be restored back to a code object.
Source: Text Source
I found several posts in the newsgroups complaining of the inability of the pickle modules to achieve this, the key is to store the code object properties in a tuple. Known Issues: The pickle module is infamous for its speed(or lack of), so its recommended you replace pickle with cpickle, you may also want to add some error checking. Finally,Dealing with code objects is somewhat a esoteric hack so care must be taken.