473,322 Members | 1,431 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,322 software developers and data experts.

__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,devFile):
DevFile = open(devFile)
devList = [Device(line) for line in DevFile]
#etc, etc...
def __deepcopy__(self):
miniMe = copy.deepcopy(self)
miniMe.devList = tuple(devList)
return miniMe
class Device:
def __init__(self,line):
self.copyies = []
#do something with line here
def __deepcopy__(self):
miniMe = copy.deepcopy(self)
self.copyies.append(miniMe)
return miniMe

DevMan1 = DeviceManager(devfile)
devMan2 = copy.deepcopy(DevMan1)

Jul 18 '05 #1
3 2004
Here I found something on __deepcopy__
http://groups-beta.google.com/group/...7866340239edd7

if applied on your script it gives something like:
-import copy
-class DeviceManager(object):
- def __init__(self,devFile):
- DevFile = open(devFile)
- devList = [Device(line) for line in DevFile]
- #etc, etc...
-
- def __deepcopy__(self, memo):
- x = DeviceManager.__new__(DeviceManager)
- memo[id(self)] = x
- for n, v in self.__dict__.iteritems():
- setattr(x, n, copy.deepcopy(v, memo))
- return x
-
-class Device(object):
- def __init__(self,line):
- self.copies = []
- #do something with line here
-
- def __deepcopy__(self, memo):
- x = Device.__new__(Device)
- memo[id(self)] = x
- for n, v in self.__dict__.iteritems():
- setattr(x, n, copy.deepcopy(v, memo))
- return x
-
-DevMan1 = DeviceManager(r'c:\autoexec.bat')
-DevMan2 = copy.deepcopy(DevMan1)
-
-print DevMan1 is DevMan2
it prints False, so the deepcopy seems to work

Jul 18 '05 #2
In this thread a solution is given on how to work with __deepcopy__:
http://groups-beta.google.com/group/...7866340239edd7

For you this would give something like:
-import copy
-class DeviceManager(object):
- def __init__(self,devFile):
- DevFile = open(devFile)
- devList = [Device(line) for line in DevFile]
- #etc, etc...
-
- def __deepcopy__(self, memo):
- x = DeviceManager.__new__(DeviceManager)
- memo[id(self)] = x
- for n, v in self.__dict__.iteritems():
- setattr(x, n, copy.deepcopy(v, memo))
- return x
-
-class Device(object):
- def __init__(self,line):
- self.copies = []
- #do something with line here
-
- def __deepcopy__(self, memo):
- x = Device.__new__(Device)
- memo[id(self)] = x
- for n, v in self.__dict__.iteritems():
- setattr(x, n, copy.deepcopy(v, memo))
- return x
-
-DevMan1 = DeviceManager(r'c:\autoexec.bat')
-DevMan2 = copy.deepcopy(DevMan1)
-
-print DevMan1 is DevMan2

Jul 18 '05 #3


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!

Jul 18 '05 #4

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

Similar topics

2
by: Christopher | last post by:
I have a hash that is several levels deep. ie: 'vars' => { '$filers' => '10.10.10.10/32', '$networksa' => '10.10.10.10/32', '$networksb' => '10.50.0.0/16', '$wintel_boxes' =>...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
20
by: Charles Law | last post by:
Consider the following scenario: A data packet is sent out of a serial port and a return packet is expected a short time later. The application sending the packet needs to send another packet...
14
by: windandwaves | last post by:
Hi Folk I want a random list of numbers between 0 and 58 where each number can only occur once. I have written this ($f is an array): for($i = 0; $i < 13; $i++) { $rand =...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
10
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
3
by: GoogleEyeJoe | last post by:
Dear ladies and gents, I'm trying to determine whether the .NET Framework implements a means of transactional processing without the need for a database. Essentially, I'd like to enlist...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.