global event listeners

Bengt Richter bokr at oz.net
Wed Nov 17 11:36:06 CET 2004


On 17 Nov 2004 00:32:35 -0800, "Hunter  Peress" <hunterp at gmail.com> wrote:

>
>Josiah Carlson wrote:
>> "Hunter  Peress" <hunterp at gmail.com> wrote:
>> >
>> > 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
>
>>
>> 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)
>>
>> >>> a = counter(1)
>> >>> b = counter(2)
>> >>> a.counter
>> 0
>> >>> b.counter
>> 0
>> >>> a.value = 0
>> >>> a.counter
>> 1
>> >>> b.counter
>> 1
>> >>>
>>
>>
>>  - 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.... ;-)
>

If you are willing to prefix your variables with a magic dotted prefix (can be one letter ;-)
then you could do something like:

 >>> class Magic(dict):
 ...     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.,

 >>> class Magic(dict):
 ...     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



More information about the Python-list mailing list