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

Error subclassing datetime.date and pickling

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
1 2501
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
8
by: Zibi | last post by:
I have some problem with datatime. SELECT * FROM stat WHERE data > '2005-05-24 14:07:28' ORDER BY id Asc I got the error: Microsoft OLE DB Provider for SQL Server error '80040e07' The...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
0
by: pabloazorin | last post by:
I developed a Date Picker web control using C# and .net framework 1.1 I added my control to Visual Studio 2003 IDE toolbar. When I drag and drop my control to design web page, the control renders...
2
by: Paulo da Silva | last post by:
Hi! What's wrong with this way of subclassing? from datetime import date class MyDate(date): def __init__(self,year,month=None,day=None): if type(year) is str: # The whole date is here as...
1
by: Christian Heimes | last post by:
Rick King schrieb: datetime.date is a C extension class. Subclassing of extension classes may not always work as you'd expect it. Christian
11
by: xenoix | last post by:
hey there, im reasonably new to C# and im currently writing a backup application which im using as a learning resource. My PC :- Visual Studio 2005 .NET Framework 2 Component Factory Krypton...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.