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

how to detect change of list of instances

how do I detect a change in a list of class instances?

from copy import deepcopy

class CaListOfObj(list):
""" subclass of list """
def __init__(self, *args, **kwargs):
list.__init__(self, *args, **kwargs)

class CaClass(object):
pass

class CaData(object):
pass

myclass=CaClass()
a=CaData()
b=CaData()
c=CaData()

listInstances = CaListOfObj([a,b,c])
setattr(myclass,'initlist',listInstances)
setattr(myclass,'newlist',deepcopy(listInstances))

print myclass.initlist == myclass.newlist
myclass.newlist.append(c)
print myclass.initlist == myclass.newlist

gives
False
False

because deep copies of instances are different instances. what I want
to do is detect a change between .initlist and .newlist.

thanks

Mar 14 '07 #1
3 2038
On Tue, 13 Mar 2007 18:23:24 -0700, manstey wrote:
how do I detect a change in a list of class instances?

from copy import deepcopy

class CaListOfObj(list):
""" subclass of list """
def __init__(self, *args, **kwargs):
list.__init__(self, *args, **kwargs)

class CaClass(object):
pass

class CaData(object):
pass
CaData has no equality test defined, so by default equality falls back on
identity tests.

myclass=CaClass()
myclass is named badly. It isn't a class, it is an instance.
a=CaData()
b=CaData()
c=CaData()

listInstances = CaListOfObj([a,b,c])
setattr(myclass,'initlist',listInstances)
setattr(myclass,'newlist',deepcopy(listInstances))
An easier, more readable, way to do that is just this:

myclass.initlist = listInstances
myclass.newlist = deepcopy(listInstances)
print myclass.initlist == myclass.newlist
myclass.newlist.append(c)
print myclass.initlist == myclass.newlist

gives
False
False

because deep copies of instances are different instances. what I want
to do is detect a change between .initlist and .newlist.
You need to define what "equal" means for two different instances of
CaData, otherwise it will fall back on checking to see if they are the
same instance.

--
Steven D'Aprano

Mar 14 '07 #2
Thanks.

All I want to know is whether the newlist, as a list of instances, is
modified. I thought equality was the way to go, but is there a simpler
way? How can I monitor the state of newlist and set a flag if it is
changed in anyway?
Mar 14 '07 #3
manstey a écrit :
Thanks.

All I want to know is whether the newlist, as a list of instances, is
modified. I thought equality was the way to go, but is there a simpler
way? How can I monitor the state of newlist and set a flag if it is
changed in anyway?
<thinking-out-loud>
Override the mutators - or just wrap them in a decorator. But I don't
know if it's "simpler". And FWIW, it won't catch modifications of
contained objects - only modifications of the list itself. So you'd also
need to wrap contained objects in an object that would itself detect all
changes and notify the container. Err... Looks like equality is the way
to go.
</thinking-out-loud>
Mar 14 '07 #4

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

Similar topics

7
by: Phil Powell | last post by:
How do you detect an image MIME type if you know of the image in a directory? For example, I know of: if (is_file("$myImagePath/$myImageName")) { // FIND MIME TYPE BUT HOW I DUNNO } The...
1
by: AndreA | last post by:
Hello I´m using MS-SQL Server 2k with a custom application connecting to it. I need a quick and simple way to detect any change (insert, update, delete) to my database (not just to a single...
7
by: Rob | last post by:
I do not want multiple instances of a vb.net program running concurrently... How may I detect this ? Thanks
1
by: Aek | last post by:
What is the best way to recursively change the permissions of the directory we are installing into? Is there a nice way to do this in C# ..NET? We are using an MSI installer and will need to add...
8
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to get a list of SQL Server Instances thru a VB.NET application. I have tried GetDataSource and SMO. I have also tried using ListSQLSvr.exe from...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
2
by: Mohit | last post by:
Hi all, I am working on a windows application with a list view on a form. Now I wanted to show hand cursor when mouse is over list view item and default(arrow) cursor at other places. List...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.