Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > More On - deepcopy, Tkinter

Reply
Thread Tools

More On - deepcopy, Tkinter

 
 
phil
Guest
Posts: n/a
 
      07-05-2005
I posted the following yesterday and got no response
and did some testing simplifying the circumstances
and it appears that deepcopy fails when the object
to be copied contains a reference to a Canvas Object.
Perhaps any Tkinter object, didn't get that far.

The problem arises because I have a geometry tutorial
with a progression of drawings and want the students to
be able to go "back". Creating "snapshots" of points
in time in the tutorial makes a clean and elegant solution
possible. Other solutions I am trying to come up with are
very messy.

It is frustrating to think that in a language like python
there might be things which you can't make a copy of.
That is bizarre enough to wonder about a deep flaw or
hopefully I'm just doing something very wrong.

Any ideas appreciated.

phil wrote:

> I wrote the following to prove to myself that
> deepcopy would copy an entire dictionary
> which contains an instance of a class to
> one key of another dictionary.
> Note that after copying adict to ndict['x']
> I delete adict.
> Then ndict['x'] contains a good copy of adict.
> works great.
>
>
> class aclass:
> def __init__(s):
> s.anint = 123
> aninstance = aclass()
> adict = {}
> adict['y'] = aninstance
>
> ndict = {}
> ndict['x'] = copy.deepcopy(adict)
> del adict
> print ndict
> print ndict['x']['y']
> print ndict['x']['y'].anint # this line prints 123
> print
>
> Then in the following code when I try to deepcopy
> s.glob.objs I get following error
> Note that s.glob.objs is a dictionary and I am
> attempting to copy to a key of s.save, another dict,
> just like above.
> ??????????
> s.glob.objs may have several keys, the data for each
> will be instance of classes like line and circle.
> Those instances will have tkinter canvas objects in them.
>
> class Graph:
> def __init__(s):
> class DummyClass: pass
> s.glob = DummyClass()
> s.glob.objs = {}
> .. # here add some instance of objects like
> .. # circles and lines to s.glob.objs
> # instantiate dialog
> s.DI = dialog(s.glob)
>
> class dialog:
> def __init__(s,glob):
> s.glob = glob
> s.save = {}
>
> def proc(s):
> cur = someint
> s.save[cur] = copy.deepcopy(s.glob.objs)
>
> Exception in Tkinter callback
> Traceback (most recent call last):
> File "/usr/local/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
> return self.func(*args)
> File "/home/phil/geo/g.py", line 303, in enter
> else:s.proc()
> File "/home/phil/geo/g.py", line 245, in proc
> s.save[cur][k] = copy.deepcopy(s.glob.objs[k][0])
> File "/usr/local/lib/python2.3/copy.py", line 179, in deepcopy
> y = copier(x, memo)
> File "/usr/local/lib/python2.3/copy.py", line 307, in _deepcopy_inst
> state = deepcopy(state, memo)
> File "/usr/local/lib/python2.3/copy.py", line 179, in deepcopy
> y = copier(x, memo)
> File "/usr/local/lib/python2.3/copy.py", line 270, in _deepcopy_dict
> y[deepcopy(key, memo)] = deepcopy(value, memo)
> File "/usr/local/lib/python2.3/copy.py", line 179, in deepcopy
> y = copier(x, memo)
> File "/usr/local/lib/python2.3/copy.py", line 307, in _deepcopy_inst
> state = deepcopy(state, memo)
> File "/usr/local/lib/python2.3/copy.py", line 179, in deepcopy
> y = copier(x, memo)
> File "/usr/local/lib/python2.3/copy.py", line 270, in _deepcopy_dict
> y[deepcopy(key, memo)] = deepcopy(value, memo)
> File "/usr/local/lib/python2.3/copy.py", line 206, in deepcopy
> y = _reconstruct(x, rv, 1, memo)
> File "/usr/local/lib/python2.3/copy.py", line 338, in _reconstruct
> y = callable(*args)
> File "/usr/local/lib/python2.3/copy_reg.py", line 92, in __newobj__
> return cls.__new__(cls, *args)
> TypeError: function() takes at least 2 arguments (0 given)
>
>




 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      07-05-2005
phil wrote:

> It is frustrating to think that in a language like python
> there might be things which you can't make a copy of.
> That is bizarre enough to wonder about a deep flaw or
> hopefully I'm just doing something very wrong.


To be honest, it doesn't really surprise me that you cannot copy Tkinter
objects. If you could make a copy of an object which is displayed on the
screen I would expect that copying would create another object on the
screen, and that probably isn't what you want. Other uncopyable objects
include files, stack frames and so on.

The deepcopy protocol does allow you to specify how complicated objects
should be copied. Try defining __deepcopy__() in your objects to just copy
the reference to the Canvas object instead of the object itself.

 
Reply With Quote
 
 
 
 
phil
Guest
Posts: n/a
 
      07-05-2005

> The deepcopy protocol does allow you to specify how complicated objects
> should be copied. Try defining __deepcopy__() in your objects to just copy
> the reference to the Canvas object instead of the object itself.


I can't figure out from the docs what __deepcopy__ is or how it

works.
I have about 25 classes of drawn objects. for instance
class linefromslope creates an instance of class line.
One of my "ugly solutions" involves a class prop: within each class,
put properties like slope and midpoint within the self.prop instance
and making a copy of that.
Would __deepcopy__ facilitate this?
Or am I assuming too much: is __deepcopy__ just a method
I invent to do what I want?




 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      07-05-2005
phil wrote:

>
>> The deepcopy protocol does allow you to specify how complicated
>> objects should be copied. Try defining __deepcopy__() in your objects
>> to just copy the reference to the Canvas object instead of the object
>> itself.

>
> I can't figure out from the docs what __deepcopy__ is or how it
>
> works.
> I have about 25 classes of drawn objects. for instance
> class linefromslope creates an instance of class line.
> One of my "ugly solutions" involves a class prop: within each class,
> put properties like slope and midpoint within the self.prop instance
> and making a copy of that.
> Would __deepcopy__ facilitate this?
> Or am I assuming too much: is __deepcopy__ just a method
> I invent to do what I want?
>


The docs say:

> In order for a class to define its own copy implementation, it can
> define special methods __copy__() and __deepcopy__(). The former is
> called to implement the shallow copy operation; no additional
> arguments are passed. The latter is called to implement the deep copy
> operation; it is passed one argument, the memo dictionary. If the
> __deepcopy__() implementation needs to make a deep copy of a
> component, it should call the deepcopy() function with the component
> as first argument and the memo dictionary as second argument.


__deepcopy__ is a method which overrides the default way to make a deepcopy
of an object.

So, if you have a class with attributes a, b, and c, and you want to ensure
that deepcopy copies a and b, but doesn't copy c, I guess you could do
something like:

>>> class MyClass:

_dontcopy = ('c',) # Tuple of attributes which must not be copied

def __deepcopy__(self, memo):
clone = copy.copy(self) # Make a shallow copy
for name, value in vars(self).iteritems():
if name not in self._dontcopy:
setattr(clone, name, copy.deepcopy(value, memo))
return clone

>>> class Copyable(object):

def __new__(cls, *args):
print "created new copyable"
return object.__new__(cls, *args)


>>> m = MyClass()
>>> m.a = Copyable()

created new copyable
>>> m.b = Copyable()

created new copyable
>>> m.c = Copyable()

created new copyable
>>> clone = copy.deepcopy(m)

created new copyable
created new copyable
>>> m.a is clone.a

False
>>> m.c is clone.c

True
>>>


As you can see, the deepcopy only creates deep copies of 2 of the 3
attributes, 'c' is simply copied across as a shallow copy.

and if you subclass MyClass you can modify the _dontcopy value to add
additional attributes which must not be copied.
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      07-05-2005
phil wrote:
> I posted the following yesterday and got no response


When you don't get as much of a response as you expected, you might
consider the advice here:

http://www.catb.org/~esr/faqs/smart-questions.html

Pointing you here is only meant to be helpful. If you don't feel the
advice applies to you, feel free to ignore it, but you clearly haven't
been getting the results from this forum that you expected.
--
Michael Hoffman
 
Reply With Quote
 
phil
Guest
Posts: n/a
 
      07-05-2005
> but you clearly haven't
> been getting the results from this forum that you expected.
>

Yes I have, this is a wonderful forum.

I was just providing more info due to more testing.




 
Reply With Quote
 
phil
Guest
Posts: n/a
 
      07-06-2005
Thanks, I used some of your methods and believe it is now
working.

I also did a lot of experiments, which I've needed to do,
investigating when references vs values are passed and
returned. Not as obvious as I thought.

Duncan Booth wrote:

> phil wrote:
>
>
>>
>>
>>>The deepcopy protocol does allow you to specify how complicated
>>>objects should be copied. Try defining __deepcopy__() in your objects
>>>to just copy the reference to the Canvas object instead of the object
>>>itself.
>>>

>>
>>I can't figure out from the docs what __deepcopy__ is or how it
>>
>>works.
>>I have about 25 classes of drawn objects. for instance
>>class linefromslope creates an instance of class line.
>>One of my "ugly solutions" involves a class prop: within each class,
>>put properties like slope and midpoint within the self.prop instance
>>and making a copy of that.
>>Would __deepcopy__ facilitate this?
>>Or am I assuming too much: is __deepcopy__ just a method
>>I invent to do what I want?
>>
>>

>
> The docs say:
>
>
>>In order for a class to define its own copy implementation, it can
>>define special methods __copy__() and __deepcopy__(). The former is
>>called to implement the shallow copy operation; no additional
>>arguments are passed. The latter is called to implement the deep copy
>>operation; it is passed one argument, the memo dictionary. If the
>>__deepcopy__() implementation needs to make a deep copy of a
>>component, it should call the deepcopy() function with the component
>>as first argument and the memo dictionary as second argument.
>>

>
> __deepcopy__ is a method which overrides the default way to make a deepcopy
> of an object.
>
> So, if you have a class with attributes a, b, and c, and you want to ensure
> that deepcopy copies a and b, but doesn't copy c, I guess you could do
> something like:
>
>
>>>>class MyClass:
>>>>

> _dontcopy = ('c',) # Tuple of attributes which must not be copied
>
> def __deepcopy__(self, memo):
> clone = copy.copy(self) # Make a shallow copy
> for name, value in vars(self).iteritems():
> if name not in self._dontcopy:
> setattr(clone, name, copy.deepcopy(value, memo))
> return clone
>
>
>>>>class Copyable(object):
>>>>

> def __new__(cls, *args):
> print "created new copyable"
> return object.__new__(cls, *args)
>
>
>
>>>>m = MyClass()
>>>>m.a = Copyable()
>>>>

> created new copyable
>
>>>>m.b = Copyable()
>>>>

> created new copyable
>
>>>>m.c = Copyable()
>>>>

> created new copyable
>
>>>>clone = copy.deepcopy(m)
>>>>

> created new copyable
> created new copyable
>
>>>>m.a is clone.a
>>>>

> False
>
>>>>m.c is clone.c
>>>>

> True
>
>
> As you can see, the deepcopy only creates deep copies of 2 of the 3
> attributes, 'c' is simply copied across as a shallow copy.
>
> and if you subclass MyClass you can modify the _dontcopy value to add
> additional attributes which must not be copied.
>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 threads; 1 more Tkinter and 1 more terminal. problem ChrisChia Python 3 08-16-2010 05:17 AM
What is the differences between tkinter in windows and Tkinter in theother platform? Hidekazu IWAKI Python 1 12-14-2009 03:44 PM
from Tkinter import *,win = Tk() "from Tkinter import *" Pierre Dagenais Python 0 08-03-2008 10:33 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments