473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1906
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 Stüvel
Stüvel 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 Palsgård | 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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.