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

Notify of change to list

Hello everyone,

I searched through groups to find an appropriate answer to this one
but could only find these which didn't meet my program's needs:
http://groups.google.com/group/comp....4b7119afa5f8df,
http://groups.google.com/group/comp....cfd49a5c8a56e2.

My question is how can my program be notified of a change to a class
attribute that is a list? I have a class defined that has a list as an
attribute and the program needs to know when the user changes this
attribute either completely or elementwise via an interpreter built-in
to the program. Using __setattr__ I can be informed whenever it is
rebound (x.attr = [2,3,4]) but not when changes are made elementwise
(x.attr[1] = 99). I tried using properties to get changes but again
the same result occurs.

class c(object):
def __init__(self):
self._x = 0

def getx(self):
return self._x

def setx(self, x):
print "setting x: ", x
self._x = x

x = property(getx, setx)

# all of the following need to be caught
a.x = [1,2,3] # is rebound from 0 so is caught
a.x[1] = 99 # element-wise change only which isn't caught
a.x = [4,5,6] # is rebound so is caught
a.x[0:3] = [11,12,13] # though a "complete" change in terms of
elements, this is element-wise and is not rebound and is thus not
caught
>>>
setting x: [1, 2, 3]
setting x: [4, 5, 6]

From what I understand, changes will only be caught if the attribute
is completely rebound (ie, completely re-assigned), but I cannot force
my users to do this. Short of hacking around with the interpreter to
catch element-wise reassignments (possible, but I'd rather avoid), can
anyone suggest a way to do this?

Alan
Jun 27 '08 #1
3 1747
On Jun 13, 12:00*pm, "Alan J. Salmoni" <salm...@gmail.comwrote:
My question is how can my program be notified of a change to a class
attribute that is a list?
You can't. But you can replace lists inside your class with a list
that notifies changes.

Something like this:

class NotifyingList(list):
def __setitem__(self, i, v):
print 'setting', i, 'to', v
list.__setitem__(self, i, v)

[[You'll likely need to redefine __setslice__, __delitem__,
__delslice__, append and extend as well]].
>>x = NotifyingList([1, 2, 3])
x[1] = 4
setting 1 to 4
>>x
[1, 4, 3]

If users of your class are allowed to replace attributes with their
own lists, you'll have to catch these and convert to NotifyingLists;
and it may be somewhat messy.

I hope this is useful to you.

--
Paul Hankin
Jun 27 '08 #2
Paul - thank you very much for your help, it was much appreciated. I
thought that this was the kind of thing I had to do.

I also messed around with properties to try and solve this problem,
but noticed a similar thing (ie, that changing a mutable attribute's
element is different to rebinding it): when a "propertied" attribute
is rebound, the set method is called as expected. However, when
setting an element of a mutable like a list, the get method is called
and the set is not. This seems to be a bit of a gotcha. I'm sure that
calling the get method is justified in the terms of Python internals,
but as a user it seems a little counter-intuitive. Without being an
expert on language design, my first thought was that changing an
element was a "set" operation. Here's a toy:

class tobj(object):
def __init__(self):
self._x = [0,1,2,3,4,5,6]

def setx(self, x):
print "At setx"
self._x = x

def getx(self):
print "At getx"
return self._x

d = property(getx, setx)

a = tobj()
print "setting one element only"
a.d[1] = 3
print "rebinding the attribute"
a.d = 99

which comes up with:

setting one element only
At getx
rebinding the attribute
At setx

Does anyone know why it works like this? I would guess that to "set"
one element, the attribute needs to be retrieved which means the get
method is called. I also guess that only rebinding calls the set
method which is why it isn't called here.

Alan

On Jun 13, 11:19 am, Paul Hankin <paul.han...@gmail.comwrote:
On Jun 13, 12:00 pm, "Alan J.Salmoni" <salm...@gmail.comwrote:
My question is how can my program be notified of a change to a class
attribute that is a list?

You can't. But you can replace lists inside your class with a list
that notifies changes.

Something like this:

class NotifyingList(list):
def __setitem__(self, i, v):
print 'setting', i, 'to', v
list.__setitem__(self, i, v)

[[You'll likely need to redefine __setslice__, __delitem__,
__delslice__, append and extend as well]].
>x = NotifyingList([1, 2, 3])
x[1] = 4
setting 1 to 4
>x

[1, 4, 3]

If users of your class are allowed to replace attributes with their
own lists, you'll have to catch these and convert to NotifyingLists;
and it may be somewhat messy.

I hope this is useful to you.

--
Paul Hankin
Jun 27 '08 #3
I recently wanted to do the same kind of thing. See this tread:
http://groups.google.com/group/comp....7c3b7950424e1c
for details on how to do it.
Jun 27 '08 #4

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

Similar topics

1
by: Alessandro GARDICH | last post by:
Hi to all I have problem with NOTIFY/LISTEN ... I'm writing a C++ application with libpqxx, I thought was a problem of the lib but I try also with a C program using libpq with the same result,...
0
by: Marcin Grzębski | last post by:
Hi, I want do create control (based on UserControl) that contains collection MyItemCollection (inherited from ICollection) of objects MyItem. I spent so much time to force this collection save...
4
by: Joe Lester | last post by:
I'm using PostgreSQL 7.4.1. I have 140 clients connected on average using libpq. When one client sends "NOTIFY timeclock;" to the server all 140 clients are listening for it. After receiving a...
4
by: Glenn Sullivan | last post by:
Hi, I have been trying to get LISTEN/NOTIFY working in with JDBC. I cannot seem to get notified. I looked in the e-mail archive and saw a lot of similiar questions a couple of years ago. I...
3
by: Frank van Vugt | last post by:
L.S. Either the docs or I are missing something.... While using libpq I noticed that listen/notify calls were being converted to lowercase. A further look showed that the listen/notify calls...
5
by: Ted Shab | last post by:
Hi, I'm trying to come up with a relatively simple multi-master replication solution. This is for multiple databases that need to be discreet, and change relatively infrequently (10-30 updates...
4
by: simo | last post by:
I have a requirement to create a sorted list of objects, stored within an assembly, and I would like to use a After Insert/Update trigger to notify that assembly that something has changed, rather...
1
by: ourxp | last post by:
Hi I have an idea but not quite sure how to implement it. I have a skeleton of a plug in architecture. My plans are to have the host use a notify icon and place it in the system tray. The plug...
9
Frinavale
by: Frinavale | last post by:
I've seen this question being asked over and over again and don't know the answer. Situation: There's a web browser displaying a list off stuff. There's a button that lets the end user add a new...
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:
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...
0
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...
0
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,...

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.