473,779 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(lis t):
""" subclass of list """
def __init__(self, *args, **kwargs):
list.__init__(s elf, *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',lis tInstances)
setattr(myclass ,'newlist',deep copy(listInstan ces))

print myclass.initlis t == myclass.newlist
myclass.newlist .append(c)
print myclass.initlis t == 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 2058
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(lis t):
""" subclass of list """
def __init__(self, *args, **kwargs):
list.__init__(s elf, *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',lis tInstances)
setattr(myclass ,'newlist',deep copy(listInstan ces))
An easier, more readable, way to do that is just this:

myclass.initlis t = listInstances
myclass.newlist = deepcopy(listIn stances)
print myclass.initlis t == myclass.newlist
myclass.newlist .append(c)
print myclass.initlis t == 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
9097
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 only way I could think of was to determine the extension of the
1
2123
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 table). The purpose is to notify somehow different instances of the application about the data changes, since they should refresh their local query results. Is there any function, stored procedure, system database entry, etc. I can exploit to get...
7
2781
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
9913
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 some custom actions to change the permissions on the install directory and its sub folders/files so that they can be accessed correctly via a network share later on. We would want to add Everyone full access to the install directory (its
8
12201
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 http://www.sqldev.net/misc/ListSQLSvr.htm I run all of them on a Windows XP Professional 64bit machine which has 2 instances: mymachine\SQLExpress (Express Edition) and MSSQLSERVER (Developer Edition (64bit)) SQL Browser Service: not running
5
1476
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 words, I can do this: .... pass .... 33
2
5528
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 view's ItemMouseHover event can tell me that mouse is on the item and here I can change my cursor to hand. But I have no clue to change it back to default cursor when mouse is moved out of the list view item.
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.