473,382 Members | 1,692 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,382 software developers and data experts.

How to pickle a subclass of tuple?

__getstate__ is easy:

def __getstate__(self):
return tuple(self)

but even

def __getstate__(self):
return self

seems to work, as far as Pickle.dump is concerned. The problem is, how
one writes a __setstate__ for an immutable class?
--
TZOTZIOY, I speak England very best,
"I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
Jul 18 '05 #1
4 2616
Christos TZOTZIOY Georgiou wrote:
__getstate__ is easy:

def __getstate__(self):
return tuple(self)

but even

def __getstate__(self):
return self

seems to work, as far as Pickle.dump is concerned. The problem is, how
one writes a __setstate__ for an immutable class?


Why do you need to do this i.e. redefine __getstate__() and __setstate__()?

Doesn't just pickling instances of your class work?
class T(tuple):pass .... t = T((1,2))
t.a = 33
import pickle
x = pickle.loads(pickle.dumps(t))
x (1, 2) x.a 33


--
Shalabh
Jul 18 '05 #2
On Thu, 03 Jun 2004 17:52:45 -0700, rumours say that Shalabh Chaturvedi
<sh*****@cafepy.com> might have written:
Christos TZOTZIOY Georgiou wrote:
__getstate__ is easy:

def __getstate__(self):
return tuple(self)

but even

def __getstate__(self):
return self

seems to work, as far as Pickle.dump is concerned. The problem is, how
one writes a __setstate__ for an immutable class?


Why do you need to do this i.e. redefine __getstate__() and __setstate__()?

Doesn't just pickling instances of your class work?
class T(tuple):pass... t = T((1,2))
t.a = 33
import pickle
x = pickle.loads(pickle.dumps(t))
x(1, 2) x.a

33


Oh, yeah, I forgot to mention that I also define __slots__ to avoid
having a dictionary; I do that because I need to create *really* lots of
instances, and the memory usage became prohibiting for the specific
computer I want this to run (a firewall).

It's data about various of ip addresses related to an anti-spam program.
I want to cache results to avoid running DNS requests every time.

So, apart from advice on how to circumvent this for my specific problem
(eg, run on another computer, don't base on tuple etc) since I already
have circumvented it, I would still like to find out if it is possible
or not to pickle a tuple-based class with __slots__ defined.
--
TZOTZIOY, I speak England very best,
"I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
Jul 18 '05 #3
On Thu, 03 Jun 2004 19:26:37 +0300, Christos "TZOTZIOY" Georgiou
<tz**@sil-tec.gr> wrote:
__getstate__ is easy:

def __getstate__(self):
return tuple(self)

but even

def __getstate__(self):
return self

seems to work, as far as Pickle.dump is concerned. The problem is, how
one writes a __setstate__ for an immutable class?


You'll probably need to use some of the extended pickle protocol hooks
documented here:

http://www.python.org/peps/pep-0307.html

In particular, you can give your subclass a __reduce__ implementation, which
pretty much gives you complete control over how it is pickled and unpickled.

---
Greg Chapman

Jul 18 '05 #4
On Fri, 04 Jun 2004 16:09:25 GMT, rumours say that Greg Chapman
<gl*@well.com> might have written:
You'll probably need to use some of the extended pickle protocol hooks
documented here:

http://www.python.org/peps/pep-0307.html


Thanks, Greg, I'm currently reading the PEP.
--
TZOTZIOY, I speak England very best,
"I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
Jul 18 '05 #5

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

Similar topics

2
by: Bram Stolk | last post by:
Hello, I am using python-2.2.2 on linux-ARM. Python itself works OK. However, importing pickle gives me *very* strange results: The first 'import pickle' fails with "ImportError: No module...
2
by: Jane Austine | last post by:
Hi. class A: def __init__(self,tick): if tick: self.foo=self.bar else: self.foo=self.bur def bar(self): print 'bar'
3
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from...
2
by: GMane Python | last post by:
Hey all. I have a ( list | tuple | dictionary ) with several graphics in it. Trying to cPickle it to a file, I get errors. For instance, UnpickleableError: Cannon pickle <type 'ImagingCore'>...
0
by: Edward C. Jones | last post by:
Suppose I want to define the class MyClass so I can create an instance by MyClass(arg0, arg1, kwarg0=None, kwarg1=0, reuse=None, save=None) If reuse is not None, it is the name of a pickle...
18
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
3
by: dgdev | last post by:
I would like to pickle an extension type (written in pyrex). I have it working thus far by defining three methods: class C: # for pickling __getstate__(self): ... # make 'state_obj' return...
5
by: brainstaurm | last post by:
I am trying to make some of the in-memory dictionaries in my code persistent using bsddb 4.3. Here is an example dictionary in my code for which I would like to create a persistent version: #...
3
by: JL | last post by:
Hello, I am trying to pickle an object, and I get: TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled I didn't find __slots__ in the object or the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.