Observable Python objects

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Observable Python objects

Andre Louw-2
I have been trying to use (abuse?) the observer swig interface to get my
Python objects to be 'Observable', not so simple! In fact, no luck!

I thought it would be as simple as defining an Observable class of type
'IsObservable' (all this in the SWIG interface file). The Python class would
then inherit from it, initialize itself and the Observable class and VOILA!
But alas, after much ranting and raving no luck?

Any suggestions?



Reply | Threaded
Open this post in threaded view
|

Re: Observable Python objects

Luigi Ballabio-2
On 2003.11.10 17:23, Andre Louw wrote:
> I have been trying to use (abuse?) the observer swig interface to get
> my Python objects to be 'Observable', not so simple! In fact, no  
> luck!

Andre',
        I'd go for containment instead of inheritance, as the latter  
does not happily cross the Python/C boundaries. Defining a Python
class like:

import QuantLib

class MyObserver:
    def __init__(self):
        self.obs = QuantLib.Observer(self.update)
    def update(self):
        print "Override me!"
    def registerWith(self,o):
        self.obs.registerWith(o)

and inheriting from MyObserver should work, but beware---I didn't test  
it. As a matter of fact, I didn't even tried to run it through the  
Python interpreter...

Later,
        Luigi


Reply | Threaded
Open this post in threaded view
|

RE: Observable Python objects

Andre Louw-2
In reply to this post by Andre Louw-2
Luigi,

I probably did not express myself properly.

The Observer side works well (even using inheritance), the problem being
that Python objects can only observe QuantLib C++ objects (those defined as
type 'isObservable' in their SWIG interface files). I'm looking at the other
side where a Python object becomes observable by other Python objects.

Something in the line of

class Foo(Observable):
        def __init__(self):
                Observable.__init__(self)
        def changeMe(self):
                self.notifyObservers()

class FooObserver:
    def __init__(self):
        self.obs = QuantLib.Observer(self.update)
    def update(self):
        print "Happens when Foo changed"
    def registerWith(self,o):
        self.obs.registerWith(o)

f = Foo()
fo = FooObserver()
fo.registerWith(f)
f.changeMe() --> should result in an update in FooObserver...

Andre


Reply | Threaded
Open this post in threaded view
|

Re: Observable Python objects

Luigi Ballabio-2
On 2003.11.11 06:07, Andre Louw wrote:

> Luigi,
>
> The Observer side works well (even using inheritance), the problem
> being that Python objects can only observe QuantLib C++ objects  
> (those defined as type 'isObservable' in their SWIG interface files).  
> I'm looking at the other side where a Python object becomes  
> observable by other Python objects.
>
> Something in the line of
>
> class Foo(Observable):
> def __init__(self):
> Observable.__init__(self)
> def changeMe(self):
> self.notifyObservers()

Oh, I see.

Hmm, I'll have to give it some thought. Can you file this as a feature  
request?

Later,
        Luigi