Error subclassing datetime.date and pickling

M

Mike Rooney

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.
"""
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&group_id=5470
-
http://sourceforge.net/tracker/index.php?func=detail&aid=720908&group_id=5470&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
 
K

kyosohma

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.
"""
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top