473,406 Members | 2,371 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,406 software developers and data experts.

Operator += works once, fails if called again

Hi all,

I have a class Time_Series derived from list. It lists days and
contains a dictionary of various Lists also derived from list which
contain values related to said days. (e.g. Stock quotes, volumes traded,
etc.)
I defined an operator += which works just fine, but only once. If I
repeat the operation, it fails and leaves me utterly mystified and crushed.

Craving to be uncrushed by a superior intelligence.

Frederic
---------------------------------------------------------------

A test:
>>TS1 = TIME_SERIES_7.Time_Series (range (10), 'TS1') # Some days
L1 = LIST_7.Floats ((22,44,323,55,344,55,66,77,-1,0), 'Numbers')
# Some List with values
>>TS1.add_List (L1)
TS2 = TIME_SERIES_7.Time_Series ((3,4,5,7,8), 'TS2') # Other days
(subset)
>>L2 = LIST_7.Floats ((7,-2,-5,0,2), 'Numbers') # Another List with
values
>>TS2.add_list (L2)
TS1 += TS2 # First call
TS1.write ()
TS1 | Date | Numbers |

0.00 | 1900.00.00 | 22.00 |
1.00 | 1900.01.01 | 44.00 |
2.00 | 1900.01.02 | 323.00 |
3.00 | 1900.01.03 | 62.00 |
4.00 | 1900.01.04 | 342.00 |
5.00 | 1900.01.05 | 50.00 |
6.00 | 1900.01.06 | 63.00 |
7.00 | 1900.01.07 | 77.00 |
8.00 | 1900.01.08 | -1.00 |
9.00 | 1900.01.09 | 0.00 |

Perfect!
>>TS1 += TS2 # Second call
Traceback (most recent call last):
File "<pyshell#311>", line 1, in -toplevel-
TS += TS2
File "c:\i\sony\fre\src\python\TIME_SERIES_7.py", line 1314, in __iadd__
return self.__add__ (other)
File "c:\i\sony\fre\src\python\TIME_SERIES_7.py", line 1273, in __add__
Sum = copy.deepcopy (self)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 320, in _reconstruct
y = callable(*args)
File "C:\PYTHON24\lib\copy_reg.py", line 92, in __newobj__
return cls.__new__(cls, *args)
TypeError: instancemethod expected at least 2 arguments, got 0
It seems to crash on the second call to deepcopy. Why? The type of the
deepcopied object is still 'Time_Series'.

Here's Time_Series.__add__ () but I don't think there's anything wrong
with that.

def __add__ (self, other):

if self [0] <= other [0]: # One or the other ...
Sum = copy.deepcopy (self)
Summand = copy.deepcopy (other)
else: # depending on which starts earlier
Sum = copy.deepcopy (other)
Summand = copy.deepcopy (self)

if Sum [0] != Summand [0]:
Summand.insert (0, Sum [0])
for list_name in Summand.Lists:
Summand.Lists[list_name].insert (0, None)
if Sum [-1] < Summand [-1]:
Sum.append (Summand [-1])
elif Sum [-1] Summand [-1]:
Summand.append (Sum [-1])

Sum.make_continuous () # Fills in missing days and values
Summand.make_continuous ()

for list_name in Summand.Lists:
if Sum.Lists.has_key (list_name):
Sum.Lists[list_name] += Summand.Lists[list_name]
# List operators work fine

return Sum
---------------------------------------------------------------------------

Oct 2 '06 #1
2 1174

Frederic Rentsch wrote:
Hi all,

I have a class Time_Series derived from list. It lists days and
contains a dictionary of various Lists also derived from list which
contain values related to said days. (e.g. Stock quotes, volumes traded,
etc.)
I defined an operator += which works just fine, but only once. If I
repeat the operation, it fails and leaves me utterly mystified and crushed.

Craving to be uncrushed by a superior intelligence.

Frederic
---------------------------------------------------------------

A test:
[perfection snipped]
>
Perfect!
>>TS1 += TS2 # Second call

Traceback (most recent call last):
File "<pyshell#311>", line 1, in -toplevel-
TS += TS2
File "c:\i\sony\fre\src\python\TIME_SERIES_7.py", line 1314, in __iadd__
return self.__add__ (other)
File "c:\i\sony\fre\src\python\TIME_SERIES_7.py", line 1273, in __add__
Sum = copy.deepcopy (self)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 320, in _reconstruct
y = callable(*args)
File "C:\PYTHON24\lib\copy_reg.py", line 92, in __newobj__
Oho! What's it doing there, in copy_reg.py? Something has stashed away
a reference to copy_reg.__newobj__, and the stasher is *not* the copy
module ....
return cls.__new__(cls, *args)
TypeError: instancemethod expected at least 2 arguments, got 0
>From a quick browse through the code for copy.py and copy_reg.py [Isn't
open source a wonderful thing?], I'm guessing:
(1) The "cls" is *your* class. You can confirm that with a debugger
(or by hacking in a print statement!).
(2) You may need to read this in the copy docs:
"""
Classes can use the same interfaces to control copying that they use to
control pickling. See the description of module pickle for information
on these methods. The copy module does not use the copy_reg
registration module.
"""
(3) You may need to read this in the pickle docs:
"""
New-style types can provide a __getnewargs__() method that is used for
protocol 2. Implementing this method is needed if the type establishes
some internal invariants when the instance is created, or if the memory
allocation is affected by the values passed to the __new__() method for
the type (as it is for tuples and strings). Instances of a new-style
type C are created using
obj = C.__new__(C, *args)
where args is the result of calling __getnewargs__() on the original
object; if there is no __getnewargs__(), an empty tuple is assumed.
"""
(4) You may need to debug your pickling/unpickling before you debug
your deepcopying.
(5) Look at function _copy_inst at line 134 in copy.py -- work out what
it will do with your class instance, depending on what __magic__
method(s) you have implemented.

Hope some of this helps,
John

Oct 2 '06 #2
John Machin wrote:
Frederic Rentsch wrote:
>Hi all,

I have a class Time_Series derived from list. It lists days and
contains a dictionary of various Lists also derived from list which
contain values related to said days. (e.g. Stock quotes, volumes traded,
etc.)
I defined an operator += which works just fine, but only once. If I
repeat the operation, it fails and leaves me utterly mystified and crushed.

Craving to be uncrushed by a superior intelligence.

Frederic
---------------------------------------------------------------

A test:

[perfection snipped]

>Perfect!
> >>TS1 += TS2 # Second call

Traceback (most recent call last):
File "<pyshell#311>", line 1, in -toplevel-
TS += TS2
File "c:\i\sony\fre\src\python\TIME_SERIES_7.py", line 1314, in __iadd__
return self.__add__ (other)
File "c:\i\sony\fre\src\python\TIME_SERIES_7.py", line 1273, in __add__
Sum = copy.deepcopy (self)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\lib\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\lib\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\lib\copy.py", line 320, in _reconstruct
y = callable(*args)
File "C:\PYTHON24\lib\copy_reg.py", line 92, in __newobj__

Oho! What's it doing there, in copy_reg.py? Something has stashed away
a reference to copy_reg.__newobj__, and the stasher is *not* the copy
module ....

> return cls.__new__(cls, *args)
TypeError: instancemethod expected at least 2 arguments, got 0
From a quick browse through the code for copy.py and copy_reg.py [Isn't
open source a wonderful thing?], I'm guessing:
(1) The "cls" is *your* class. You can confirm that with a debugger
(or by hacking in a print statement!).
(2) You may need to read this in the copy docs:
"""
Classes can use the same interfaces to control copying that they use to
control pickling. See the description of module pickle for information
on these methods. The copy module does not use the copy_reg
registration module.
"""
(3) You may need to read this in the pickle docs:
"""
New-style types can provide a __getnewargs__() method that is used for
protocol 2. Implementing this method is needed if the type establishes
some internal invariants when the instance is created, or if the memory
allocation is affected by the values passed to the __new__() method for
the type (as it is for tuples and strings). Instances of a new-style
type C are created using
obj = C.__new__(C, *args)
where args is the result of calling __getnewargs__() on the original
object; if there is no __getnewargs__(), an empty tuple is assumed.
"""
(4) You may need to debug your pickling/unpickling before you debug
your deepcopying.
(5) Look at function _copy_inst at line 134 in copy.py -- work out what
it will do with your class instance, depending on what __magic__
method(s) you have implemented.

Hope some of this helps,
John

John,

Thank you very much for your suggestions. I followed them one by one and
in the end found the cause of the problem to be a circular reference.
Some species of contained Lists need a reference to the containing
Time_Series and that circular reference trips up deepcopy. Supposedly I
can define my own __deepcopy__, but didn't understand whose method it is
supposed to be. Time_Series.__deepcopy__ () doesn't make sens, nor does
it work. Time_Series should be the argument of the method not the owner.
Anyway, I managed without deepcopy making a new instance and loading it
as I would, if it were the first one.

Thanks again

Frederic

Oct 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MGFoster | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've converted an ACC97 .mdb file to an ACC2K2 .adp. A report that worked in ACC97 doesn't work in ACC2K2. Report setup: ACC97 ...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
2
by: bharath.donnipad | last post by:
Hi All, I was going through a c++ manual in msdn site "http://msdn2.microsoft.com/en-us/library/kewsb8ba.aspx". There there was a small note on usage of new operator : "When new is used to...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.