global event listeners

H

Hunter Peress

I want to do things directly before an object is changed. eg is there
something like:


a= "hi"

objectChangeListener(a,lambda(previous_value,new_value):\
global_counter.append(previous_value) ; return new_value)



so then this global_counter var will increment whenever a is changed.
PS: im not looking to hear "know your codebase better"
 
J

Josiah Carlson

Hunter Peress said:
I want to do things directly before an object is changed. eg is there
something like:


a= "hi"

objectChangeListener(a,lambda(previous_value,new_value):\
global_counter.append(previous_value) ; return new_value)

so then this global_counter var will increment whenever a is changed.
PS: im not looking to hear "know your codebase better"

You cannot do precisely what you want to do. I'm sorry. You can do
something similar.

class counter(object):
def __init__(self, value):
self.__value = value
self.counter = 0

def get_v(self):
return self.__value
def set_v(self, val):
self.__value = val
self.counter += 1
value = property(get_v, set_v)

Properties are your friend.

- Josiah
 
H

Hunter Peress

i suppose i could do this, and have an "if self.__name__ ==
what_im_looking_for and type(self) == what_im_also_looking_for"

next is how can i make every object in a given runtime inherit from
this class
 
J

Josiah Carlson

Hunter Peress said:
i suppose i could do this, and have an "if self.__name__ ==
what_im_looking_for and type(self) == what_im_also_looking_for"

next is how can i make every object in a given runtime inherit from
this class

Before I answer your question, I will state that it is common courtesy
to quote the relevant portions of the email you are replying to; it
makes picking up on the conversation easier.

class counter(object):
counter = 0
def __init__(self, value):
self.__value = value

def get_v(self):
return self.__value
def set_v(self, val):
self.__value = val
counter.counter += 1
value = property(get_v, set_v)


- Josiah
 
H

Hunter Peress

class counter(object):
counter = 0
def __init__(self, value):
self.__value = value

def get_v(self):
return self.__value
def set_v(self, val):
self.__value = val
counter.counter += 1
value = property(get_v, set_v)



- Josiah

Well yes, but im looking for a non-1:1 way of making every object be a
child of this new class. i would have to do an insane amount of
regex.... ;-)
 
B

Bengt Richter

Well yes, but im looking for a non-1:1 way of making every object be a
child of this new class. i would have to do an insane amount of
regex.... ;-)

If you are willing to prefix your variables with a magic dotted prefix (can be one letter ;-)
then you could do something like:
... def __init__(self, ofinterest = ''):
... dict.__init__(self, [(name,[]) for name in ofinterest.split()])
... def __setattr__(self, name, value):
... if name in self:
... self[name].append(value)
... object.__setattr__(self, name, value)
...
>>> magic = Magic('a c x')
>>> magic {'a': [], 'x': [], 'c': []}
>>> vars(magic) {}
>>> magic.b = 222
>>> vars(magic) {'b': 222}
>>> magic {'a': [], 'x': [], 'c': []}
>>> magic.a = 111
>>> vars(magic) {'a': 111, 'b': 222}
>>> magic {'a': [111], 'x': [], 'c': []}
>>> magic.a = 'after 111'
>>> magic {'a': [111, 'after 111'], 'x': [], 'c': []}
>>> vars(magic) {'a': 'after 111', 'b': 222}
>>> magic.x = 'ex'
>>> magic.y = 'wy'
>>> vars(magic) {'a': 'after 111', 'x': 'ex', 'b': 222, 'y': 'wy'}
>>> magic {'a': [111, 'after 111'], 'x': ['ex'], 'c': []}
>>> for k,v in sorted(magic.items()): print '%5s assumed %s values: %s'%(k,len(v),v)
...
a assumed 2 values: [111, 'after 111']
c assumed 0 values: []
x assumed 1 values: ['ex']

I don't know what you want to with this, so it's hard to tailor suggestions. You could
obviously do a version that treated all attribute assignments as "of interest", e.g.,
... def __setattr__(self, name, value):
... self.setdefault(name, []).append(value)
... object.__setattr__(self, name, value)
...
>>> m = Magic()
>>> m {}
>>> vars(m) {}
>>> m.x = 111
>>> m.x = 222
>>> m.y = 333
>>> vars(m) {'y': 333, 'x': 222}
>>> m {'y': [333], 'x': [111, 222]}
>>> for k,v in sorted(m.items()): print '%5s assumed %s values: %s'%(k,len(v),v)
...
x assumed 2 values: [111, 222]
y assumed 1 values: [333]

Regards,
Bengt Richter
 
J

Josiah Carlson

Hunter Peress said:
Well yes, but im looking for a non-1:1 way of making every object be a
child of this new class. i would have to do an insane amount of
regex.... ;-)

Not really. You could (as another poster suggests) make an object with
some number of dynamically created attributes, and call such an object
something like 'M' (for magic)...

M.name
M.name2
etc.

An interesting feature request would be:
Allow properties to be assigned to modules after module instantiation,
coupled with the ability to get the current module.

With those two features, then what you want is possible, though those
two features /aren't/ going to make it into Python 2.4, it it would take
quite a bit of work to make it happen in Python 2.5.


- Josiah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top