Save/Store whole class (or another object) in a file

Discussion in 'Python' started by alexLIGO@googlemail.com, Oct 17, 2006.

  1. Guest

    Hi,

    is it possible in python (with/without matplotlib, numpy etc) to store
    a whole class with its data into a file, instead it to reconstruct
    every time again? So is there an analogous to the matlab functions
    load/save available?

    Thanks
    Alex
    , Oct 17, 2006
    #1
    1. Advertising

  2. enlightened us with:
    > is it possible in python (with/without matplotlib, numpy etc) to
    > store a whole class with its data into a file


    Check out the pickle module.

    Sybren
    --
    Sybren Stüvel
    Stüvel IT - http://www.stuvel.eu/
    Sybren Stuvel, Oct 17, 2006
    #2
    1. Advertising

  3. Matimus Guest

    > is it possible in python (with/without matplotlib, numpy etc) to store
    > a whole class with its data into a file, instead it to reconstruct
    > every time again? So is there an analogous to the matlab functions
    > load/save available?


    look up the pickle module.
    Matimus, Oct 17, 2006
    #3
  4. Guest

    Hi,

    thanks for the reply,but unfortunately this does not work with the type
    of classes I am dealing with. When trying to pickle the class I get the
    following error:

    File "/usr/lib/python2.4/copy_reg.py", line 76, in _reduce_ex
    raise TypeError("a class that defines __slots__ without "
    TypeError: a class that defines __slots__ without defining __getstate__
    cannot be pickled

    So there is something missing in this class? Or any other idea how to
    do this?

    Alex


    Sybren Stuvel wrote:
    > enlightened us with:
    > > is it possible in python (with/without matplotlib, numpy etc) to
    > > store a whole class with its data into a file

    >
    > Check out the pickle module.
    >
    > Sybren
    > --
    > Sybren Stüvel
    > Stüvel IT - http://www.stuvel.eu/
    , Oct 18, 2006
    #4
  5. wrote:

    > thanks for the reply,but unfortunately this does not work with the type
    > of classes I am dealing with. When trying to pickle the class I get the
    > following error:
    >
    > File "/usr/lib/python2.4/copy_reg.py", line 76, in _reduce_ex
    > raise TypeError("a class that defines __slots__ without "
    > TypeError: a class that defines __slots__ without defining __getstate__
    > cannot be pickled
    >
    > So there is something missing in this class?


    did you remember to read the error message before posting ?

    </F>
    Fredrik Lundh, Oct 18, 2006
    #5
  6. wrote:
    > Hi,
    >
    > thanks for the reply,but unfortunately this does not work with the type
    > of classes I am dealing with. When trying to pickle the class I get the
    > following error:
    >
    > File "/usr/lib/python2.4/copy_reg.py", line 76, in _reduce_ex
    > raise TypeError("a class that defines __slots__ without "
    > TypeError: a class that defines __slots__ without defining __getstate__
    > cannot be pickled
    >
    > So there is something missing in this class? Or any other idea how to
    > do this?
    >
    > Alex


    yes, read the documentation:
    http://docs.python.org/dev/lib/pickle-inst.html

    Michele Simionato
    Michele Simionato, Oct 18, 2006
    #6
  7. Guest

    Hi,

    Fredrik Lundh wrote:

    > wrote:
    >
    > > thanks for the reply,but unfortunately this does not work with the type
    > > of classes I am dealing with. When trying to pickle the class I get the
    > > following error:
    > >
    > > File "/usr/lib/python2.4/copy_reg.py", line 76, in _reduce_ex
    > > raise TypeError("a class that defines __slots__ without "
    > > TypeError: a class that defines __slots__ without defining __getstate__
    > > cannot be pickled
    > >
    > > So there is something missing in this class?

    >
    > did you remember to read the error message before posting ?
    >


    of course I read the error-message, I understand the error-message, BUT
    this message is not telling me how anyway to save the data of such a
    class in a file! It does not tell me, that there is absolutely no way
    to do that. Maybe there is, maybe there is not.
    Thats why I am posting it here!

    Alex
    , Oct 25, 2006
    #7
  8. At Wednesday 25/10/2006 11:32, wrote:

    >Fredrik Lundh wrote:
    > > wrote:
    > > > thanks for the reply,but unfortunately this does not work with the type
    > > > of classes I am dealing with. When trying to pickle the class I get the
    > > > following error:
    > > >
    > > > File "/usr/lib/python2.4/copy_reg.py", line 76, in _reduce_ex
    > > > raise TypeError("a class that defines __slots__ without "
    > > > TypeError: a class that defines __slots__ without defining __getstate__
    > > > cannot be pickled
    > > >
    > > > So there is something missing in this class?

    > >
    > > did you remember to read the error message before posting ?

    >
    >of course I read the error-message, I understand the error-message, BUT
    >this message is not telling me how anyway to save the data of such a
    >class in a file! It does not tell me, that there is absolutely no way
    >to do that. Maybe there is, maybe there is not.
    >Thats why I am posting it here!


    "A man who wears a hat without buying a ticket cannot enter the theatre"
    You wear a hat, you want to enter: either buy your ticket or forget
    about your hat.
    In this case, just define a __getstate__ method (read the pickle
    docs), or forget about __slots__.


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
    Gabriel Genellina, Oct 25, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rambo88
    Replies:
    3
    Views:
    425
    lordy
    Jul 16, 2004
  2. Guest
    Replies:
    111
    Views:
    11,611
    Mike Schilling
    Sep 15, 2004
  3. \A_Michigan_User\
    Replies:
    2
    Views:
    874
    \A_Michigan_User\
    Aug 21, 2006
  4. Victor
    Replies:
    1
    Views:
    323
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 4, 2007
  5. Herta
    Replies:
    2
    Views:
    308
    Lawrence D'Oliveiro
    Aug 12, 2010
Loading...

Share This Page