__deepcopy__ without recursive copies?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bernie

    __deepcopy__ without recursive copies?

    #!/usr/bin/env python
    import sys
    import copy

    '''
    How to define __deepcopy__ with out causing recursive calls to copies
    of self?

    Bernie Day 01/25/05

    I was using deepcopy on DeviceManager and this worked well. When I
    defined
    __deepcopy__, so I could have a handle to the children of the Master
    device Manager
    I got into trouble.
    I see that there is a method using a dic to limit this, but I can't
    seem to get it to work.

    This is not the code I was using but should represent it well.

    Thanks!
    '''


    class DeviceManager:
    def __init__(self,d evFile):
    DevFile = open(devFile)
    devList = [Device(line) for line in DevFile]
    #etc, etc...
    def __deepcopy__(se lf):
    miniMe = copy.deepcopy(s elf)
    miniMe.devList = tuple(devList)
    return miniMe
    class Device:
    def __init__(self,l ine):
    self.copyies = []
    #do something with line here
    def __deepcopy__(se lf):
    miniMe = copy.deepcopy(s elf)
    self.copyies.ap pend(miniMe)
    return miniMe

    DevMan1 = DeviceManager(d evfile)
    devMan2 = copy.deepcopy(D evMan1)

  • wittempj@hotmail.com

    #2
    Re: __deepcopy__ without recursive copies?

    Here I found something on __deepcopy__


    if applied on your script it gives something like:
    -import copy
    -class DeviceManager(o bject):
    - def __init__(self,d evFile):
    - DevFile = open(devFile)
    - devList = [Device(line) for line in DevFile]
    - #etc, etc...
    -
    - def __deepcopy__(se lf, memo):
    - x = DeviceManager._ _new__(DeviceMa nager)
    - memo[id(self)] = x
    - for n, v in self.__dict__.i teritems():
    - setattr(x, n, copy.deepcopy(v , memo))
    - return x
    -
    -class Device(object):
    - def __init__(self,l ine):
    - self.copies = []
    - #do something with line here
    -
    - def __deepcopy__(se lf, memo):
    - x = Device.__new__( Device)
    - memo[id(self)] = x
    - for n, v in self.__dict__.i teritems():
    - setattr(x, n, copy.deepcopy(v , memo))
    - return x
    -
    -DevMan1 = DeviceManager(r 'c:\autoexec.ba t')
    -DevMan2 = copy.deepcopy(D evMan1)
    -
    -print DevMan1 is DevMan2
    it prints False, so the deepcopy seems to work

    Comment

    • wittempj@hotmail.com

      #3
      Re: __deepcopy__ without recursive copies?

      In this thread a solution is given on how to work with __deepcopy__:


      For you this would give something like:
      -import copy
      -class DeviceManager(o bject):
      - def __init__(self,d evFile):
      - DevFile = open(devFile)
      - devList = [Device(line) for line in DevFile]
      - #etc, etc...
      -
      - def __deepcopy__(se lf, memo):
      - x = DeviceManager._ _new__(DeviceMa nager)
      - memo[id(self)] = x
      - for n, v in self.__dict__.i teritems():
      - setattr(x, n, copy.deepcopy(v , memo))
      - return x
      -
      -class Device(object):
      - def __init__(self,l ine):
      - self.copies = []
      - #do something with line here
      -
      - def __deepcopy__(se lf, memo):
      - x = Device.__new__( Device)
      - memo[id(self)] = x
      - for n, v in self.__dict__.i teritems():
      - setattr(x, n, copy.deepcopy(v , memo))
      - return x
      -
      -DevMan1 = DeviceManager(r 'c:\autoexec.ba t')
      -DevMan2 = copy.deepcopy(D evMan1)
      -
      -print DevMan1 is DevMan2

      Comment

      • Bernie

        #4
        Wow!



        That is clever, gives a lot of insight into how the __dict__ == the
        object.


        This is somewhat like the solution I am using from the Cookbook, an
        Empty object copy. This is cleaner and very much more concise.
        Thank you!

        Comment

        Working...