473,804 Members | 3,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.T ime_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_Lis t (L1)
TS2 = TIME_SERIES_7.T ime_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_lis t (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\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 320, in _reconstruct
y = callable(*args)
File "C:\PYTHON24\li b\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.__a dd__ () 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_contin uous () # Fills in missing days and values
Summand.make_co ntinuous ()

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

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

Oct 2 '06 #1
2 1188

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\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 320, in _reconstruct
y = callable(*args)
File "C:\PYTHON24\li b\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.__newo bj__, 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\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 335, in _reconstruct
state = deepcopy(state, memo)
File "C:\PYTHON24\li b\copy.py", line 161, in deepcopy
y = copier(x, memo)
File "C:\PYTHON24\li b\copy.py", line 252, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\PYTHON24\li b\copy.py", line 188, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\PYTHON24\li b\copy.py", line 320, in _reconstruct
y = callable(*args)
File "C:\PYTHON24\li b\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.__newo bj__, 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.__d eepcopy__ () 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
2804
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 ACC2K2 (SP-2) -------------- ---------------------------
6
2256
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 contains a pointer to int values as a filed. In the definition (implementation) of constructor I use this pointer to create table of int values with the new operator. The number of elements of the table is provided by the user during execution of the...
2
2394
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 allocate memory for a C++ class object, the object's constructor is called after the memory is allocated." So, I got a doubt and quickly executed below program. Its o/p is : base
5
2295
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
0
9585
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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
10082
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
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
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...
1
4301
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
2997
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.