Java Doc for cPickle.java in  » Testing » Marathon » org » python » modules » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » Marathon » org.python.modules 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.python.modules.cPickle

cPickle
public class cPickle implements ClassDictInit(Code)
From the python documentation:

The cPickle.java module implements a basic but powerful algorithm for ``pickling'' (a.k.a. serializing, marshalling or flattening) nearly arbitrary Python objects. This is the act of converting objects to a stream of bytes (and back: ``unpickling''). This is a more primitive notion than persistency -- although cPickle.java reads and writes file objects, it does not handle the issue of naming persistent objects, nor the (even more complicated) area of concurrent access to persistent objects. The cPickle.java module can transform a complex object into a byte stream and it can transform the byte stream into an object with the same internal structure. The most obvious thing to do with these byte streams is to write them onto a file, but it is also conceivable to send them across a network or store them in a database. The module shelve provides a simple interface to pickle and unpickle objects on ``dbm''-style database files.

Note: The cPickle.java have the same interface as the standard module pickleexcept that Pickler and Unpickler are factory functions, not classes (so they cannot be used as base classes for inheritance). This limitation is similar for the original cPickle.c version.

Unlike the built-in module marshal, cPickle.java handles the following correctly:

  • recursive objects (objects containing references to themselves)

  • object sharing (references to the same object in different places)

  • user-defined classes and their instances

The data format used by cPickle.java is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as XDR (which can't represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.

By default, the cPickle.java data format uses a printable ASCII representation. This is slightly more voluminous than a binary representation. The big advantage of using printable ASCII (and of some other characteristics of cPickle.java's representation) is that for debugging or recovery purposes it is possible for a human to read the pickled file with a standard text editor.

A binary format, which is slightly more efficient, can be chosen by specifying a nonzero (true) value for the bin argument to the Pickler constructor or the dump() and dumps() functions. The binary format is not the default because of backwards compatibility with the Python 1.4 pickle module. In a future version, the default may change to binary.

The cPickle.java module doesn't handle code objects.

For the benefit of persistency modules written using cPickle.java, it supports the notion of a reference to an object outside the pickled data stream. Such objects are referenced by a name, which is an arbitrary string of printable ASCII characters. The resolution of such names is not defined by the cPickle.java module -- the persistent object module will have to implement a method persistent_load(). To write references to persistent objects, the persistent module must define a method persistent_id() which returns either None or the persistent ID of the object.

There are some restrictions on the pickling of class instances.

First of all, the class must be defined at the top level in a module. Furthermore, all its instance variables must be picklable.

When a pickled class instance is unpickled, its __init__() method is normally not invoked. Note: This is a deviation from previous versions of this module; the change was introduced in Python 1.5b2. The reason for the change is that in many cases it is desirable to have a constructor that requires arguments; it is a (minor) nuisance to have to provide a __getinitargs__() method.

If it is desirable that the __init__() method be called on unpickling, a class can define a method __getinitargs__(), which should return a tuple containing the arguments to be passed to the class constructor (__init__()). This method is called at pickle time; the tuple it returns is incorporated in the pickle for the instance.

Classes can further influence how their instances are pickled -- if the class defines the method __getstate__(), it is called and the return state is pickled as the contents for the instance, and if the class defines the method __setstate__(), it is called with the unpickled state. (Note that these methods can also be used to implement copying class instances.) If there is no __getstate__() method, the instance's __dict__ is pickled. If there is no __setstate__() method, the pickled object must be a dictionary and its items are assigned to the new instance's dictionary. (If a class defines both __getstate__() and __setstate__(), the state object needn't be a dictionary -- these methods can do what they want.) This protocol is also used by the shallow and deep copying operations defined in the copy module.

Note that when class instances are pickled, their class's code and data are not pickled along with them. Only the instance data are pickled. This is done on purpose, so you can fix bugs in a class or add methods and still load objects that were created with an earlier version of the class. If you plan to have long-lived objects that will see many versions of a class, it may be worthwhile to put a version number in the objects so that suitable conversions can be made by the class's __setstate__() method.

When a class itself is pickled, only its name is pickled -- the class definition is not pickled, but re-imported by the unpickling process. Therefore, the restriction that the class must be defined at the top level in a module applies to pickled classes as well.

The interface can be summarized as follows.

To pickle an object x onto a file f, open for writing:

 p = pickle.Pickler(f)
 p.dump(x)
 

A shorthand for this is:

 pickle.dump(x, f)
 

To unpickle an object x from a file f, open for reading:

 u = pickle.Unpickler(f)
 x = u.load()
 

A shorthand is:

 x = pickle.load(f)
 

The Pickler class only calls the method f.write() with a string argument. The Unpickler calls the methods f.read() (with an integer argument) and f.readline() (without argument), both returning a string. It is explicitly allowed to pass non-file objects here, as long as they have the right methods.

The constructor for the Pickler class has an optional second argument, bin. If this is present and nonzero, the binary pickle format is used; if it is zero or absent, the (less efficient, but backwards compatible) text pickle format is used. The Unpickler class does not have an argument to distinguish between binary and text pickle formats; it accepts either format.

The following types can be pickled:

  • None

  • integers, long integers, floating point numbers

  • strings

  • tuples, lists and dictionaries containing only picklable objects

  • classes that are defined at the top level in a module

  • instances of such classes whose __dict__ or __setstate__() is picklable

Attempts to pickle unpicklable objects will raise the PicklingError exception; when this happens, an unspecified number of bytes may have been written to the file.

It is possible to make multiple calls to the dump() method of the same Pickler instance. These must then be matched to the same number of calls to the load() method of the corresponding Unpickler instance. If the same object is pickled by multiple dump() calls, the load() will all yield references to the same object. Warning: this is intended for pickling multiple objects without intervening modifications to the objects or their parts. If you modify an object and then pickle it again using the same Pickler instance, the object is not pickled again -- a reference to it is pickled and the Unpickler will return the old value, not the modified one. (There are two problems here: (a) detecting changes, and (b) marshalling a minimal set of changes. I have no answers. Garbage Collection may also become a problem here.)

Apart from the Pickler and Unpickler classes, the module defines the following functions, and an exception:

dump (object, file[, bin])
Write a pickled representation of obect to the open file object file. This is equivalent to "Pickler(file, bin).dump(object)". If the optional bin argument is present and nonzero, the binary pickle format is used; if it is zero or absent, the (less efficient) text pickle format is used.

load (file)
Read a pickled object from the open file object file. This is equivalent to "Unpickler(file).load()".

dumps (object[, bin])
Return the pickled representation of the object as a string, instead of writing it to a file. If the optional bin argument is present and nonzero, the binary pickle format is used; if it is zero or absent, the (less efficient) text pickle format is used.

loads (string)
Read a pickled object from a string instead of a file. Characters in the string past the pickled object's representation are ignored.

PicklingError
This exception is raised when an unpicklable object is passed to Pickler.dump().

For the complete documentation on the pickle module, please see the "Python Library Reference"


The module is based on both original pickle.py and the cPickle.c version, except that all mistakes and errors are my own.


author:
   Finn Bock, bckfnn@pipmail.dknet.dk
version:
   cPickle.java,v 1.30 1999/05/15 17:40:12 fb Exp


Inner Class :interface IOFile
Inner Class :static class cStringIOFile implements IOFile
Inner Class :static class FileIOFile implements IOFile
Inner Class :static class ObjectIOFile implements IOFile
Inner Class :public static class Pickler
Inner Class :public static class Unpickler

Field Summary
final static  charAPPEND
    
final static  charAPPENDS
    
final static  charBINFLOAT
    
final static  charBINGET
    
final static  charBININT
    
final static  charBININT1
    
final static  charBININT2
    
final static  charBINPERSID
    
final static  charBINPUT
    
final static  charBINSTRING
    
final static  charBINUNICODE
    
final static  charBUILD
    
final public static  PyStringBadPickleGet
    
final static  charDICT
    
final static  charDUP
    
final static  charEMPTY_DICT
    
final static  charEMPTY_LIST
    
final static  charEMPTY_TUPLE
    
final static  charFLOAT
    
final static  charGET
    
final static  charGLOBAL
    
final static  charINST
    
final static  charINT
    
final static  charLIST
    
final static  charLONG
    
final static  charLONG_BINGET
    
final static  charLONG_BINPUT
    
final static  charMARK
    
final static  charNONE
    
final static  charOBJ
    
final static  charPERSID
    
final static  charPOP
    
final static  charPOP_MARK
    
final static  charPUT
    
public static  PyObjectPickleError
    
public static  PyObjectPicklingError
    
final static  charREDUCE
    
final static  charSETITEM
    
final static  charSETITEMS
    
final static  charSHORT_BINSTRING
    
final static  charSTOP
    
final static  charSTRING
    
final static  charTUPLE
    
final static  charUNICODE
    
public static  PyObjectUnpickleableError
    
public static  PyObjectUnpicklingError
    
public static  String[]__depends__
    
public static  String__doc__
    
public static  String__version__
     The program version.
final public static  String[]compatible_formats
     Old format versions we can read.
final public static  Stringformat_version
     File format version we write.

Constructor Summary
public  cPickle()
    

Method Summary
public static  PicklerPickler(PyObject file)
     Returns a pickler instance.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements awrite method.
public static  PicklerPickler(PyObject file, boolean bin)
     Returns a pickler instance.
public static  UnpicklerUnpickler(PyObject file)
     Returns a unpickler instance.
public static  PyObject_PickleError(PyObject[] arg, String[] kws)
    
public static  void_PickleError__init__(PyObject[] arg, String[] kws)
    
public static  PyString_PickleError__str__(PyObject[] arg, String[] kws)
    
public static  PyObject_UnpickleableError(PyObject[] arg, String[] kws)
    
public static  void_UnpickleableError__init__(PyObject[] arg, String[] kws)
    
public static  PyString_UnpickleableError__str__(PyObject[] arg, String[] kws)
    
public static  PyObject_empty__init__(PyObject[] arg, String[] kws)
    
public static  voidclassDictInit(PyObject dict)
     Initialization when module is imported.
public static  voiddump(PyObject object, PyObject file)
     Shorthand function which pickles the object on the file.
Parameters:
  object - a data object which should be pickled.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements awrite method.
public static  voiddump(PyObject object, PyObject file, boolean bin)
     Shorthand function which pickles the object on the file.
public static  Stringdumps(PyObject object)
     Shorthand function which pickles and returns the string representation.
public static  Stringdumps(PyObject object, boolean bin)
     Shorthand function which pickles and returns the string representation.
public static  Objectload(PyObject file)
     Shorthand function which unpickles a object from the file and returns the new object.
public static  Objectloads(PyObject str)
     Shorthand function which unpickles a object from the string and returns the new object.

Field Detail
APPEND
final static char APPEND(Code)



APPENDS
final static char APPENDS(Code)



BINFLOAT
final static char BINFLOAT(Code)



BINGET
final static char BINGET(Code)



BININT
final static char BININT(Code)



BININT1
final static char BININT1(Code)



BININT2
final static char BININT2(Code)



BINPERSID
final static char BINPERSID(Code)



BINPUT
final static char BINPUT(Code)



BINSTRING
final static char BINSTRING(Code)



BINUNICODE
final static char BINUNICODE(Code)



BUILD
final static char BUILD(Code)



BadPickleGet
final public static PyString BadPickleGet(Code)



DICT
final static char DICT(Code)



DUP
final static char DUP(Code)



EMPTY_DICT
final static char EMPTY_DICT(Code)



EMPTY_LIST
final static char EMPTY_LIST(Code)



EMPTY_TUPLE
final static char EMPTY_TUPLE(Code)



FLOAT
final static char FLOAT(Code)



GET
final static char GET(Code)



GLOBAL
final static char GLOBAL(Code)



INST
final static char INST(Code)



INT
final static char INT(Code)



LIST
final static char LIST(Code)



LONG
final static char LONG(Code)



LONG_BINGET
final static char LONG_BINGET(Code)



LONG_BINPUT
final static char LONG_BINPUT(Code)



MARK
final static char MARK(Code)



NONE
final static char NONE(Code)



OBJ
final static char OBJ(Code)



PERSID
final static char PERSID(Code)



POP
final static char POP(Code)



POP_MARK
final static char POP_MARK(Code)



PUT
final static char PUT(Code)



PickleError
public static PyObject PickleError(Code)



PicklingError
public static PyObject PicklingError(Code)



REDUCE
final static char REDUCE(Code)



SETITEM
final static char SETITEM(Code)



SETITEMS
final static char SETITEMS(Code)



SHORT_BINSTRING
final static char SHORT_BINSTRING(Code)



STOP
final static char STOP(Code)



STRING
final static char STRING(Code)



TUPLE
final static char TUPLE(Code)



UNICODE
final static char UNICODE(Code)



UnpickleableError
public static PyObject UnpickleableError(Code)



UnpicklingError
public static PyObject UnpicklingError(Code)



__depends__
public static String[] __depends__(Code)



__doc__
public static String __doc__(Code)
The doc string



__version__
public static String __version__(Code)
The program version.



compatible_formats
final public static String[] compatible_formats(Code)
Old format versions we can read.



format_version
final public static String format_version(Code)
File format version we write.




Constructor Detail
cPickle
public cPickle()(Code)




Method Detail
Pickler
public static Pickler Pickler(PyObject file)(Code)
Returns a pickler instance.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements awrite method. The data will be written as text.



Pickler
public static Pickler Pickler(PyObject file, boolean bin)(Code)
Returns a pickler instance.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements awrite method.
Parameters:
  bin - when true, the output will be written as binary data.



Unpickler
public static Unpickler Unpickler(PyObject file)(Code)
Returns a unpickler instance.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements aread and readline method.



_PickleError
public static PyObject _PickleError(PyObject[] arg, String[] kws)(Code)



_PickleError__init__
public static void _PickleError__init__(PyObject[] arg, String[] kws)(Code)



_PickleError__str__
public static PyString _PickleError__str__(PyObject[] arg, String[] kws)(Code)



_UnpickleableError
public static PyObject _UnpickleableError(PyObject[] arg, String[] kws)(Code)



_UnpickleableError__init__
public static void _UnpickleableError__init__(PyObject[] arg, String[] kws)(Code)



_UnpickleableError__str__
public static PyString _UnpickleableError__str__(PyObject[] arg, String[] kws)(Code)



_empty__init__
public static PyObject _empty__init__(PyObject[] arg, String[] kws)(Code)



classDictInit
public static void classDictInit(PyObject dict)(Code)
Initialization when module is imported.



dump
public static void dump(PyObject object, PyObject file)(Code)
Shorthand function which pickles the object on the file.
Parameters:
  object - a data object which should be pickled.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements awrite method. The data will be written astext.



dump
public static void dump(PyObject object, PyObject file, boolean bin)(Code)
Shorthand function which pickles the object on the file.
Parameters:
  object - a data object which should be pickled.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements awrite method.
Parameters:
  bin - when true, the output will be written as binary data.



dumps
public static String dumps(PyObject object)(Code)
Shorthand function which pickles and returns the string representation.
Parameters:
  object - a data object which should be pickled.



dumps
public static String dumps(PyObject object, boolean bin)(Code)
Shorthand function which pickles and returns the string representation.
Parameters:
  object - a data object which should be pickled.
Parameters:
  bin - when true, the output will be written as binary data.



load
public static Object load(PyObject file)(Code)
Shorthand function which unpickles a object from the file and returns the new object.
Parameters:
  file - a file-like object, can be a cStringIO.StringIO,a PyFile or any python object which implements aread and readline method.



loads
public static Object loads(PyObject str)(Code)
Shorthand function which unpickles a object from the string and returns the new object.
Parameters:
  str - a strings which must contain a pickled objectrepresentation.



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.