FAQ
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Þtail&atid5470&aid•2807&group_idT70
-
http://sourceforge.net/tracker/index.php?funcÞtail&aidr0908&group_idT70&atid5470

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

Search Discussions

  • Kyosohma at Aug 2, 2007 at 7:50 pm

    On Aug 2, 11:02 am, Mike Rooney wrote:
    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Þtail&atid5470&aid•2807&g...
    -http://sourceforge.net/tracker/index.php?funcÞtail&aidr0908&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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 2, '07 at 4:02p
activeAug 2, '07 at 7:50p
posts2
users2
websitepython.org

2 users in discussion

Kyosohma: 1 post Mike Rooney: 1 post

People

Translate

site design / logo © 2022 Grokbase