[PyQt] properties and interfaces with dip 0.3

Lic. José M. Rodriguez Bacallao jmrbcu at gmail.com
Wed Jul 13 20:17:48 BST 2011


in this example:

class ITest(Interface):
    id = Str()

@implements(ITest)
class Test(Model):

    @ITest.id.getter
    def id(self):
        return 'getting id'

    @ITest.id.setter
    def id(self, v):
        print 'setting id'

    @observe('id')
    def on_change(self, change):
        print 'changing...'

    test = Test()
    test.id = 'xxx'
    print test.id

in the case of properties, it is necessary to use the syntax:
@Interface.attr but here I got another problem, the getter are not
being called. Why?

On Wed, Jul 13, 2011 at 3:08 PM, Phil Thompson
<phil at riverbankcomputing.com> wrote:
> On Wed, 13 Jul 2011 14:56:52 -0400, Lic. José M. Rodriguez Bacallao
> <jmrbcu at gmail.com> wrote:
>> ok, I see now, a couple of questions:
>> 1- and how to use observe function as a decorator
>
>    @observe('id')
>    def on_change(self, change):
>
> Note that you don't have to use 'ITest.id'. This is because the string is
> parsed later on than when @ITest.id is.
>
>> 2- there is any way that the observer/setter method of a property be
>> called when setting the initial value
>
> No.
>
> Phil
>
>> On Wed, Jul 13, 2011 at 2:24 PM, Phil Thompson
>> <phil at riverbankcomputing.com> wrote:
>>> On Wed, 13 Jul 2011 13:53:57 -0400, Lic. José M. Rodriguez Bacallao
>>> <jmrbcu at gmail.com> wrote:
>>>> yes, I know I can't think in interfaces like base classes but, as I
>>>> read in documentation that attributes are automatically added to te
>>>> concrete implementation of the interface, well, look at this sample:
>>>>
>>>> class ITest(Interface):
>>>>
>>>>     id = Str()
>>>>
>>>>
>>>> @implements(ITest)
>>>> class Test(Model):
>>>>
>>>>     @id.observer
>>>>     def on_change(self, change):
>>>>         print 'changing...'
>>>>
>>>>
>>>> if __name__ == '__main__':
>>>>     test = Test()
>>>>     test.id = 'xxx'
>>>>     print test.id
>>>>
>>>> it give me this error:
>>>>
>>>> Traceback (most recent call last):
>>>>   File
> "/home/jmrbcu/work/dev-projects/imagis/src/imagis/ui/widget.py",
>>>> line 49, in <module>
>>>>     class Test(Model):
>>>>   File
> "/home/jmrbcu/work/dev-projects/imagis/src/imagis/ui/widget.py",
>>>> line 51, in Test
>>>>     @id.observer
>>>> AttributeError: 'builtin_function_or_method' object has no attribute
>>>> 'observer'
>>>>
>>>> and if I change @id.observer to @ITest.id.observer ten the observer is
>>>> never called.
>>>
>>> ...because if you are using that pattern then the name of the method
> must
>>> be the same as the name of the attribute, ie. 'id' instead of
>>> 'on_change'.
>>>
>>> Phil
>>>
>



-- 
Lic. José M. Rodriguez Bacallao
Centro de Biofisica Medica
-----------------------------------------------------------------
Todos somos muy ignorantes, lo que ocurre es que no todos ignoramos lo mismo.

Recuerda: El arca de Noe fue construida por aficionados, el titanic
por profesionales
-----------------------------------------------------------------


More information about the PyQt mailing list