473,545 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(m d, open("test.pick le", 'w'))
mdp = pickle.load(ope n("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\li b\doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest __main__.MyDate[3]>", line 1, in <module>
mdp = pickle.load(ope n("test.pickle" ))
File "C:\Python25\li b\pickle.py", line 1370, in load
return Unpickler(file) .load()
File "C:\Python25\li b\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python25\li b\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 2511
On Aug 2, 11:02 am, Mike Rooney <m...@qvii.comw rote:
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(m d, open("test.pick le", 'w'))
>>mdp = pickle.load(ope n("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\li b\doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest __main__.MyDate[3]>", line 1, in <module>
mdp = pickle.load(ope n("test.pickle" ))
File "C:\Python25\li b\pickle.py", line 1370, in load
return Unpickler(file) .load()
File "C:\Python25\li b\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python25\li b\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&at id=105470&aid=9 52807&g...
-http://sourceforge.net/tracker/index.php?func= detail&aid=7209 08&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
3229
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 value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python...
8
2720
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 conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
4
2809
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 arrays of datetime.datetime instances. The following is the code I have written: import cPickle, datetime import Numeric
2
13912
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 ( weekstart datetime not null primary key, weekend datetime not null )
0
1591
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 correctly. If I change to HTML the generated html is <cc1:IT24DateTime id="IT24DateTime1" runat="server" Type="Date"></cc1:IT24DateTime> .......
2
1224
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 a string
1
1401
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
5557
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 Tools Test PC :- .Net Framework 2
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.