Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   self-aware list of objects able to sense constituent memberalterations? (http://www.velocityreviews.com/forums/t667497-self-aware-list-of-objects-able-to-sense-constituent-memberalterations.html)

Reckoner 01-27-2009 09:16 PM

self-aware list of objects able to sense constituent memberalterations?
 
I'm not sure this is possible, but I would like to have
a list of objects

A=[a,b,c,d,...,z]

where, in the midst of a lot of processing I might do something like,

A[0].do_something_which_changes_the_properties()

which alter the properties of the object 'a'.

The trick is that I would like A to be mysteriously aware that
something about the object 'a' has changed so that when I revisit A,
I will know that the other items in the list need to be refreshed to
reflect the changes in A as a result of changing 'a'.

Even better would be to automatically percolate the subsequent changes
that resulted from altering 'a' for the rest of the items in the list.
Naturally, all of these items are related in some parent-child
fashion.

that might be a lot to ask, however.

Any advice appreciated.


Chris Rebert 01-27-2009 09:27 PM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Tue, Jan 27, 2009 at 1:16 PM, Reckoner <reckoner@gmail.com> wrote:
> I'm not sure this is possible, but I would like to have
> a list of objects
>
> A=[a,b,c,d,...,z]
>
> where, in the midst of a lot of processing I might do something like,
>
> A[0].do_something_which_changes_the_properties()
>
> which alter the properties of the object 'a'.
>
> The trick is that I would like A to be mysteriously aware that
> something about the object 'a' has changed so that when I revisit A,
> I will know that the other items in the list need to be refreshed to
> reflect the changes in A as a result of changing 'a'.


Have you considered:

A.change_properties_of_item_and_know_about_it(0)

?

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

Steven D'Aprano 01-28-2009 05:46 AM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Tue, 27 Jan 2009 13:16:36 -0800, Reckoner wrote:

> I'm not sure this is possible, but I would like to have a list of
> objects
>
> A=[a,b,c,d,...,z]
>
> where, in the midst of a lot of processing I might do something like,
>
> A[0].do_something_which_changes_the_properties()
>
> which alter the properties of the object 'a'.
>
> The trick is that I would like A to be mysteriously aware that something
> about the object 'a' has changed so that when I revisit A, I will know
> that the other items in the list need to be refreshed to reflect the
> changes in A as a result of changing 'a'.


Can't be done if A is a built-in list, probably can't be done entirely
generically, but you can probably do it in a cooperative manner.


class TaintList(list):
tainted = False
def taint(self):
self.tainted = True
def untaint(self):
self.tainted = False

A = TaintList()


import functools
def taint(parent):
def decorator(func):
@functools.wraps(func)
def f(*args, **kwargs):
parent.taint()
return func(*args, **kwargs)
return f
return decorator


class TaintAwareThing(object):
def __init__(self):
self.attr = 0
@taint(A)
def change_attribute(self, x):
self.attr = x



>>> x = TaintAwareThing()
>>> y = TaintAwareThing()
>>> z = TaintAwareThing()
>>>
>>> A.extend([x, y, z])
>>> A.tainted

False
>>> x.change_attribute(5)
>>> A.tainted

True


Here is a second approach: create a proxy class TaintThing that wraps
whatever object you want, using delegation:

class TaintThing(object):
parent = A
def __init__(self, obj):
self.__dict__['_proxy'] = obj
def __getattr__(self, attr):
return getattr(self._proxy, attr)
def __setattr__(self, attr, value):
setattr(self._proxy, attr, value)
self.parent.taint()


Now change TaintList to automatically wrap anything stored in it:


# untested
class TaintList(list):
def append(self, obj):
list.append(self, TaintThing(obj))
# similar for __setitem__, extend, insert



--
Steven

Aaron Brady 01-28-2009 09:24 AM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Jan 27, 3:16*pm, Reckoner <recko...@gmail.com> wrote:
> I'm not sure this is possible, but I would like to have
> a list of *objects
>
> A=[a,b,c,d,...,z]
>
> where, *in the midst of a lot of processing I might do something like,
>
> A[0].do_something_which_changes_the_properties()
>
> which alter the properties of the object 'a'.
>
> The trick is that I would like A to be mysteriously aware that
> something about the *object 'a' has changed so that when I revisit A,
> I will know that the other items in the list need to be refreshed to
> reflect the changes in A as a result of changing 'a'.
>
> Even better would be to automatically percolate the subsequent changes
> that resulted from altering 'a' for the rest of the items in the list.
> Naturally, all of these items are related in some parent-child
> fashion.
>
> that might be a lot to ask, however.
>
> Any advice appreciated.


What you could do is specialize '__getitem__' (or '__getslice__') so
that whenever one of its items is accessed, the item is marked as
dirty or the entire list is refreshed. (Unproduced.)

def taintlist(list):
def __getitem__( self, key ):
x= super(taintlist, self).__getitem__( self, key )
self._dirty= True
self.refresh() #too early, unfortunately
return x
...

However, what you are probably after is something like this
(unproduced):

def taintlist(list):
def __getitem__( self, key ):
x= super(taintlist, self).__getitem__( self, key )
y= delegate( self, key, x )
return y

The 'delegate' class, also unproduced, automatically delegates
function calls (including member lookups) to the target. After the
delegated call returns, the list is notified-- hence the three
arguments to its constructor. (Unproduced.)

class delegate:
def __getattr__( self, key ):
attr= super( delegate, self ).__getattr__( self, key )
deleg= delegate( self.owner, self.ownerkey, attr )
return deleg
def __call__( self, *ar, **kw ):
res= self.attr( *ar, **kw )
self.owner.markdirty( )
return res

I'm not convinced it's possible, but there's a possibility... or
something like it. When you call a[0].meth(), three things happen:

x= '0' looked up on 'a'
y= 'meth' looked up on 'x'
z= 'y' called

You want control over the last of these parts, so you can call a
custom function instead. It becomes increasingly risky as the depth
increases, such as if the target class implements custom access, I
guess. In the 'delegate' shown, for example, it assumes that the
result is callable. You might need:

class delegate:
def __getattr__( self, key ):
attr= super( delegate, self ).__getattr__( self, key )
if not iscallable( attr ):
return attr
... #return delegate

Further, if the result is callable, that doesn't mean it will
necessarily be called. You should be able to tolerate this sequence:

x= '0' looked up on 'a'
y= 'meth' looked up on 'x'
z= attribute looked up on 'y' (instead of 'y' called)

Mind if we inquire after your progress?

John O'Hagan 01-28-2009 09:37 AM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Tue, 27 Jan 2009, Reckoner wrote:
> I'm not sure this is possible, but I would like to have
> a list of objects
>
> A=[a,b,c,d,...,z]
>
> where, in the midst of a lot of processing I might do something like,
>
> A[0].do_something_which_changes_the_properties()
>
> which alter the properties of the object 'a'.
>
> The trick is that I would like A to be mysteriously aware that
> something about the object 'a' has changed so that when I revisit A,
> I will know that the other items in the list need to be refreshed to
> reflect the changes in A as a result of changing 'a'.
>
> Even better would be to automatically percolate the subsequent changes
> that resulted from altering 'a' for the rest of the items in the list.

[...]

Interesting question.

Maybe this is too simple for your purpose (or maybe just wrong!), but could
you subclass list and give it an "update" method which keeps a dictionary of
the state of its members and/or calls another method that makes the
appropriate changes in the other members when a change occurs, something
like:

class SelfAwareList(list):

state_dict = {}

def update(self):
for i in self:
if i.state == 'some_condition':
self.do_stuff_to_other_members()
self.state_dict[i] = i.state

def do_stuff_to_other_members(self):
print 'doing stuff...'

?

You could manually call update() on the SelfAwareList instance after calling a
method on a SelfAwareList member, or even build it into the members' methods
so that it was automatic.

HTH,

John



koranthala 01-28-2009 12:42 PM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Jan 28, 2:16*am, Reckoner <recko...@gmail.com> wrote:
> I'm not sure this is possible, but I would like to have
> a list of *objects
>
> A=[a,b,c,d,...,z]
>
> where, *in the midst of a lot of processing I might do something like,
>
> A[0].do_something_which_changes_the_properties()
>
> which alter the properties of the object 'a'.
>
> The trick is that I would like A to be mysteriously aware that
> something about the *object 'a' has changed so that when I revisit A,
> I will know that the other items in the list need to be refreshed to
> reflect the changes in A as a result of changing 'a'.
>
> Even better would be to automatically percolate the subsequent changes
> that resulted from altering 'a' for the rest of the items in the list.
> Naturally, all of these items are related in some parent-child
> fashion.
>
> that might be a lot to ask, however.
>
> Any advice appreciated.


I think Python Cookbook has a recipe which deals with this.
- 6.12 Checking an Instance for Any State Change.

koranthala 01-28-2009 05:16 PM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Jan 28, 5:42*pm, koranthala <koranth...@gmail.com> wrote:
> On Jan 28, 2:16*am, Reckoner <recko...@gmail.com> wrote:
>
>
>
> > I'm not sure this is possible, but I would like to have
> > a list of *objects

>
> > A=[a,b,c,d,...,z]

>
> > where, *in the midst of a lot of processing I might do something like,

>
> > A[0].do_something_which_changes_the_properties()

>
> > which alter the properties of the object 'a'.

>
> > The trick is that I would like A to be mysteriously aware that
> > something about the *object 'a' has changed so that when I revisit A,
> > I will know that the other items in the list need to be refreshed to
> > reflect the changes in A as a result of changing 'a'.

>
> > Even better would be to automatically percolate the subsequent changes
> > that resulted from altering 'a' for the rest of the items in the list.
> > Naturally, all of these items are related in some parent-child
> > fashion.

>
> > that might be a lot to ask, however.

>
> > Any advice appreciated.

>
> I think Python Cookbook has a recipe which deals with this.
> - 6.12 Checking an Instance for Any State Change.


Were you able to get this? If not, let me know. I will try to type it
in here - (it is a big recipe, so not doing it now)

Paul McGuire 01-28-2009 05:33 PM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Jan 27, 3:16*pm, Reckoner <recko...@gmail.com> wrote:
> The trick is that I would like A to be mysteriously aware that
> something about the *object 'a' has changed so that when I revisit A,
> I will know that the other items in the list need to be refreshed to
> reflect the changes in A as a result of changing 'a'.
>


Check out the Enthought Traits package.

-- Paul

Reckoner 01-28-2009 05:39 PM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Jan 28, 9:16 am, koranthala <koranth...@gmail.com> wrote:
> On Jan 28, 5:42 pm, koranthala <koranth...@gmail.com> wrote:
>
>
>
> > On Jan 28, 2:16 am, Reckoner <recko...@gmail.com> wrote:

>
> > > I'm not sure this is possible, but I would like to have
> > > a list of objects

>
> > > A=[a,b,c,d,...,z]

>
> > > where, in the midst of a lot of processing I might do something like,

>
> > > A[0].do_something_which_changes_the_properties()

>
> > > which alter the properties of the object 'a'.

>
> > > The trick is that I would like A to be mysteriously aware that
> > > something about the object 'a' has changed so that when I revisit A,
> > > I will know that the other items in the list need to be refreshed to
> > > reflect the changes in A as a result of changing 'a'.

>
> > > Even better would be to automatically percolate the subsequent changes
> > > that resulted from altering 'a' for the rest of the items in the list.
> > > Naturally, all of these items are related in some parent-child
> > > fashion.

>
> > > that might be a lot to ask, however.

>
> > > Any advice appreciated.

>
> > I think Python Cookbook has a recipe which deals with this.
> > - 6.12 Checking an Instance for Any State Change.

>
> Were you able to get this? If not, let me know. I will try to type it
> in here - (it is a big recipe, so not doing it now)


Actually, I have the python cookbook, but cannot find the recipe you
mention. maybe I have an older version?

thanks.

koranthala 01-28-2009 05:49 PM

Re: self-aware list of objects able to sense constituent memberalterations?
 
On Jan 28, 10:39*pm, Reckoner <recko...@gmail.com> wrote:
> On Jan 28, 9:16 am, koranthala <koranth...@gmail.com> wrote:
>
>
>
> > On Jan 28, 5:42 pm, koranthala <koranth...@gmail.com> wrote:

>
> > > On Jan 28, 2:16 am, Reckoner <recko...@gmail.com> wrote:

>
> > > > I'm not sure this is possible, but I would like to have
> > > > a list of *objects

>
> > > > A=[a,b,c,d,...,z]

>
> > > > where, *in the midst of a lot of processing I might do something like,

>
> > > > A[0].do_something_which_changes_the_properties()

>
> > > > which alter the properties of the object 'a'.

>
> > > > The trick is that I would like A to be mysteriously aware that
> > > > something about the *object 'a' has changed so that when I revisit A,
> > > > I will know that the other items in the list need to be refreshed to
> > > > reflect the changes in A as a result of changing 'a'.

>
> > > > Even better would be to automatically percolate the subsequent changes
> > > > that resulted from altering 'a' for the rest of the items in the list.
> > > > Naturally, all of these items are related in some parent-child
> > > > fashion.

>
> > > > that might be a lot to ask, however.

>
> > > > Any advice appreciated.

>
> > > I think Python Cookbook has a recipe which deals with this.
> > > - 6.12 Checking an Instance for Any State Change.

>
> > Were you able to get this? If not, let me know. I will try to type it
> > in here - (it is a big recipe, so not doing it now)

>
> Actually, I have the python cookbook, but cannot find the recipe you
> mention. maybe I have an older version?
>
> thanks.


Mine is 2nd Edition.


All times are GMT. The time now is 07:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.