[PyKDE] Deep copy

Vio vmilitaru at sympatico.ca
Tue Jul 22 21:16:02 BST 2003


Yes, some kind of property list comprising everything that makes the 
target object "unique" or distinct from a freshly constructed one - 
let's call it the object's DNA - may be the way to go. So deepcopying an 
object then becomes a 2 stages process:

1. create a new object with 'default' attributes

    cloneObj = obj.__class__()

assuming all goes well here :)

2. Apply the attributes which makes the target object unique from a 
'default' one. Well, this obviously doesn't sound too complicated to 
code, only it's "costly" for me, your average "lazy coder", which has to 
figure out what maked each object "deviate from default values".
Hmm, or how about something like this perhaps, which only looks for 
'deviations from the norm' and deepcopies that:

def cloneObj(obj):

    dictO = {} # 'O' stands for 'original' :)
    for name, val in obj.__dict__.iteritems():
        dictO.update({name : val})

    # this should give me a new instance with default attributes
    clone = obj.__class__() # assuming no problems here :)

    # let's get lazy
    for name, val in obj.__dict__.iteritems():
       if val != dict[name]:

          # found something unique - deepcopy it
          try:
              val = copy.deepcopy(dict[name])
          except:
             print 'DAMN>',name,val,dict[name]

           setattr(clone, name, val)
    return clone


Ok, I don't know if this will bomb or what, time to try it now.
V.






Frederick Polgardy Jr wrote:

>On Monday 21 July 2003 08:18 pm, Vio wrote:
>  
>
>>Hi,
>>I need to implement copy/cut/paste of whole Pyqt objects. This
>>apparently requires 'deep copies' of a target object: one initial
>>copy used as the 'clipboard' object (my cookie-cutter), then all
>>other copies created by 'deep-copying' the clipboard object.
>>    
>>
>
>Why not "cut" or "copy" into some kind of property map which you can use 
>for construction of new objects?  You wouldn't necessarily need a 
>prototype "clipboard" object, though it would probably suffice.  I 
>wonder how much help you can elicit from the introspection attributes 
>(__dict__, etc.) to aid in your cause.  Clearly, what you want are the 
>"simpler" attributes (numbers, strings, rects, colors, etc.), not 
>things like handles or references, which are either unique, or totally 
>unimportant.
>
>Also how much of this "copy construction" are you doing, that 
>construction and member copying on the new instance would be so costly?  
>Cut/copy/paste is normally a human-time operation, and no human is 
>going to be able to tell the difference.
>
>Good luck....
>Fred
>
>
>  
>





More information about the PyQt mailing list