List Info

Thread: Explanation of Pickle




Explanation of Pickle
user name
2007-01-30 01:11:16
can any one explain about pickle i read in the book but they have not provided any example for that so please explain with a simple example

--
         ;           ;           ;   
      ;           ;           ;           ;    Vanam
Re: Explanation of Pickle
user name
2007-01-30 04:24:24
"vanam" <vgvr620034gmail.com> wrote

> can any one explain about pickle i read in the book but
they have 
> not
> provided any example for that so please explain with a
simple 
> example

Conceptually it is very simple. Pickle takes a python
object
and serialises it to a sequence of bytes which it stores in
a
text file. The text file can be unpickled by reading it
back
and de-serialising it. The object you wind up with should
be
exactly the same as the object you started with. The idea
being that you can store (or persist) your objects even
while
your program is not running. The pickle module provides the
functions to do all of that.

Are there  any more specific problems you are having?

Alan G. 


_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: Explanation of Pickle
user name
2007-01-30 05:02:31
vanam wrote:
> can any one explain about pickle i read in the book but
they have not 
> provided any example for that so please explain with a
simple example

There are examples in the docs:
http:/
/docs.python.org/lib/pickle-example.html

Kent

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: Explanation of Pickle
user name
2007-01-30 10:34:31
Hello Vanam!

On Tuesday 30 January 2007 08:11, vanam wrote:
> can any one explain about pickle i read in the book but
they have
> not provided any example for that so please explain
with a simple
> example
A class can pickle itself like this:


class foo(object):
    #lots of code here

    def _loadPickle(self, fileName):
        '''Load data from a file in pickle format.'''
        f = open(fileName, 'rb')
        newStore = cPickle.load(f) 
        self.__dict__ = newStore.__dict__ #copy the data
attributes
        f.close()

    def _savePickle(self, fileName):
        '''Dump the data into a binary pickle file'''
        f = open(fileName, 'wb')
        cPickle.dump(self, f, 2)
        f.close()


Kind regards,
Eike.

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )