472,371 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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

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 '06 #1
7 1851
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.

Oct 17 '06 #2
al******@googlemail.com 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 17 '06 #3
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:
al******@googlemail.com 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 Stvel
Stvel IT - http://www.stuvel.eu/
Oct 18 '06 #4
al******@googlemail.com 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>

Oct 18 '06 #5
al******@googlemail.com 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

Oct 18 '06 #6
Hi,

Fredrik Lundh wrote:
al******@googlemail.com 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 '06 #7
At Wednesday 25/10/2006 11:32, al******@googlemail.com wrote:
>Fredrik Lundh wrote:
al******@googlemail.com 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
Oct 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
6
by: Andreas Palsgrd | last post by:
Hey.. i want to write a class object containing both strings and integers, to a file on disk, and be able to read it properly again at another time. I made the code below... it doesn't work, but...
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
8
by: Les Coover | last post by:
The following is an example of input: 1 Les L 01/02/04 2300 06:00AM SW Nashville R 01/06/04 2400 10:00PM SW Kansas City L 01/15/04 2440 10:00AM NW Minneapolis R 01/15/04 3440 05:00PM NW...
2
by: monsieur.marco | last post by:
Hi all, Does anybody know how to save localization in an excel workbook (C# 1.1)? I'm not sure this is possible, but I'm trying to do this so that numberColumns keep their format no matter what...
2
by: wanwan | last post by:
when I program with Java, I'm able to open a file to open a file stream and an object stream, then save a class object into a file. I don't see any class in vb.net that is equivalent to object...
1
by: ganeshshegde | last post by:
Hi.. Can we store whole file in database? ie it should store in database in same format. .pdf should store in .pdf fomat and .doc should store in .doc fomat.
8
by: hanna88 | last post by:
hello.. is there a way of storing data to other class object. here's the problem. class Repository is where it reads through the sentences and tokenize it into words . and what i did is by using...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.