By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Error subclassing datetime.date and pickling

P: n/a
Hi everyone, this is my first post to this list. I am trying to create a
subclass of datetime.date and pickle it, but I get errors on loading it
back. I have created a VERY simple demo of this:

import datetime

class MyDate(datetime.date):
"""
This should be pickleable.
>>md = MyDate(2007, 1, 6)
import pickle
pickle.dump(md, open("test.pickle", 'w'))
mdp = pickle.load(open("test.pickle"))
import os; os.remove("test.pickle")
"""
def __init__(self, y, m, d):
datetime.date.__init__(self, y, m, d)

if __name__ == "__main__":
import doctest
doctest.testmod()
The traceback that occurs is:

Traceback (most recent call last):
File "C:\Python25\lib\doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest __main__.MyDate[3]>", line 1, in <module>
mdp = pickle.load(open("test.pickle"))
File "C:\Python25\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python25\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python25\lib\pickle.py", line 1133, in load_red
value = func(*args)
TypeError: __init__() takes exactly 4 arguments (2 given)
I have found a few relating issues:
-
https://sourceforge.net/tracker/?fun...&group_id=5470
-
http://sourceforge.net/tracker/index...70&atid=105470

But these are both rather old and are marked as fixed. I am running
Python 2.5.1 on Windows XP SP2. Any help here would be greatly appreciated!

- Mike
Aug 2 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Aug 2, 11:02 am, Mike Rooney <m...@qvii.comwrote:
Hi everyone, this is my first post to this list. I am trying to create a
subclass of datetime.date and pickle it, but I get errors on loading it
back. I have created a VERY simple demo of this:

import datetime

class MyDate(datetime.date):
"""
This should be pickleable.
>>md = MyDate(2007, 1, 6)
>>import pickle
>>pickle.dump(md, open("test.pickle", 'w'))
>>mdp = pickle.load(open("test.pickle"))
>>import os; os.remove("test.pickle")
"""
def __init__(self, y, m, d):
datetime.date.__init__(self, y, m, d)

if __name__ == "__main__":
import doctest
doctest.testmod()

The traceback that occurs is:

Traceback (most recent call last):
File "C:\Python25\lib\doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest __main__.MyDate[3]>", line 1, in <module>
mdp = pickle.load(open("test.pickle"))
File "C:\Python25\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python25\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python25\lib\pickle.py", line 1133, in load_red
value = func(*args)
TypeError: __init__() takes exactly 4 arguments (2 given)

I have found a few relating issues:
-https://sourceforge.net/tracker/?func=detail&atid=105470&aid=952807&g...
-http://sourceforge.net/tracker/index.php?func=detail&aid=720908&group...

But these are both rather old and are marked as fixed. I am running
Python 2.5.1 on Windows XP SP2. Any help here would be greatly appreciated!

- Mike
I've never used pickle or shelve in my applications...yet. I tried
separating out the class into its own module and importing it, as
pickling classes is supposed to be tricky. I looked through all my
Python books and what few examples I found looked just like your code.

I did find this article which might be helpful to you more than it was
to me: http://www.ibm.com/developerworks/library/l-pypers.html
>From what I can tell, when your object gets unpickled it only passes 2
arguments to the class instead of the 4 that were supposed to be
pickled. If you change the number of arguments that the class accepts
and re-pickle it, you'll see that the unpickle object traceback
corresponds.

It's very weird. Sorry I didn't have some slam-bang awesome idea.

Mike

Aug 2 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.