473,797 Members | 3,204 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 2526
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
3248
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 direction I've created to remove the pre-1970 date objects? Ideally, I would be able to iterate...
8
2729
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
2834
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
13929
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
1607
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> .... and that works correctly.
2
1235
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
1411
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
5597
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
9685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10209
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9066
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2934
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.