Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a class that behaves like any variable but has callback on change/read

I would like to create a class that behaves as a python variable but calls some callback function when the "variable" is changed/read.

In other words, I'd like to be able to use the class as follows:

x=myClass(change_callback, read_callback)

defines x as an instance of myclass. The constructor (INIT) takes 2 functions as paramater: a function to be called each time x is changed, and a function to be called each time x is "read"

The following statement:

x=1

would "save" 1 and trigger a call to change_callback(1) which could do anything.

The following statement:

a=x

would retrieve the stored value and call read_callback() which would possibly change the stored value and do other things.

I would like this to work with any type, e.g. to be able to write things like:

x=[1 2 3] which would trigger change_callback([1 2 3])

x.append(4) would trigger change_callback([1 2 3 4]) (and possibly a call to read_callback() before)

x={'a':1} would trigger change_callback({'a':1})

print(x) would trigger a call to read_callback()...and return the last stored value for printing, of course.

The idea is that any access to the variable could be logged, or generate other calculation... seemlessly.

I get the feeling this should be possible, but I don't really see what my object should inherit from... If I have to restrict me to one type,e.g. a list, is there any way to redefine all assignment operators (including methods like append()...) in "one go", keeping the original behaviour (the base class method) and adding the callback...

Or are there more appropriate ways (modules...) to achieve the same goals...?

Thanks in advance,

like image 656
user1159290 Avatar asked Dec 07 '22 15:12

user1159290


2 Answers

Objects don't know when they get assigned to a variable. Writing x = a adds a dict entry (or locals entry) that points to a. The a object doesn't get notified (though, in CPython, its reference count gets incremented).

The part that does get notified is the container where the object is assigned. In the case of a global assignment, the module dictionary gets updated. In the case of instance variable updates like a.b = x, there is a dotted lookup and store to the instance dictionary.

You can make those containers invoke arbitrary code on the lookup or store. Python's property provides this capability to new-style classes. The exec and eval operations let you specify a custom dict that can provide this capability to regular assignments. In both cases, you are in complete control of what happens upon assignment.

Summary: Lookup and store behaviors are controllable through the target of the assignment rather than the object being assigned.

Example:

from collections import namedtuple

CallbackVar = namedtuple('CallbackVar', ['change_callback', 'read_callback'])

class CallbackDict(dict):
    'Variant of dict that does callbacks for instances of CallbackVar'
    def __getitem__(self, key):
        value = dict.__getitem__(self, key)
        if isinstance(value, CallbackVar):
            return value.read_callback(key)
    def __setitem__(self, key, value):
        try:
            realvalue = dict.__getitem__(self, key)
            if isinstance(realvalue, CallbackVar):
                return realvalue.change_callback(key, value)
        except KeyError:
            pass
        return dict.__setitem__(self, key, value)

stmts = '''
x = CallbackVar(setter, getter)     # Attach getter() and setter() to "x"
x = 1                               # Invoke the setter()
x                                   # Invoke the getter()
'''

def getter(key):
    print 'Getting', key
    return 42

def setter(key, value):
    print 'Setting', key, 'to', value

exec stmts in globals(), CallbackDict()
like image 189
Raymond Hettinger Avatar answered Dec 09 '22 14:12

Raymond Hettinger


You might like to look at descriptors: http://docs.python.org/howto/descriptor.html

This will only work for object properties, though, which is probably enough for you.

like image 36
Marcin Avatar answered Dec 09 '22 15:12

Marcin