Re: [Pyobjc-dev] property progress
                
                Brought to you by:
                
                    ronaldoussoren
                    
                
            
            
        
        
        
    | 
      
      
      From: Orestis M. <or...@or...> - 2010-01-21 16:52:57
      
     | 
| On 21 Jan 2010, at 17:38, Ronald Oussoren wrote:
> This now works on my machine:
> 
> import objc
> 
> 
> NSObject = objc.lookUpClass('NSObject')
> NSKeyValueObservingOptionNew = 0x01
> NSKeyValueObservingOptionOld = 0x02
> 
> 
> 
> class Observer (NSObject):
>    def init(self):
>        self = super(Observer, self).init()
>        self.keys = []
>        return self
> 
>    def dealloc(self):
>        for o, k in self.keys:
>            o.removeObserver_forKeyPath_(self, k)
> 
>    def observeValueForKeyPath_ofObject_change_context_(self, path, object, change, context):
>        print "===> %s\t\t%s"%(path, change)
> 
>    def register(self, object, keypath):
>        object.addObserver_forKeyPath_options_context_(
>                self, keypath, 
>                NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld,
>                0)
>        self.keys.append((object, keypath))
> 
> 
> class Person (NSObject):
>    def _init(self):
>        self = objc.super(Person, self).init()
>        self.firstName = None
>        self.lastName = None
>        return self
> 
>    firstName = objc.object_property()
>    lastName = objc.object_property()
> 
>    fullName = objc.object_property(read_only=True, ivar=objc.NULL)
>    fullName.depends_on(u'firstName')
>    fullName.depends_on(u'lastName')
> 
>    @fullName.getter
>    def fullName(self):
>        return "%s %s"%(self.firstName, self.lastName)
> 
> obs = Observer.alloc().init()
> pers = Person.alloc().init()
> obs.register(pers, "fullName")
> obs.register(pers, "firstName")
> obs.register(pers, "lastName")
> 
> pers.firstName = "Ronald"
> pers.lastName = "Oussoren"
> 
> print "done"
> print pers.fullName
> 
> 
> That is,  I can define something that behaves like a python property for Python code, but is implemented  using ObjC-style getter/setter methods and therefore participates properly in key-value observations.
> 
> What makes me really happy is that adding some hooks to the bridge allowed me to write class objc_property in Python, which should make it a lot more convenient to add simular properties that represent an array or set.
> 
> I haven't committed this to the repository yet, I have to add unittests and documentation before I do that.
That looks brilliant! Great job, thanks for all your efforts on PyObjC.
Orestis
> Ronald------------------------------------------------------------------------------
> Throughout its 18-year history, RSA Conference consistently attracts the
> world's best and brightest in the field, creating opportunities for Conference
> attendees to learn about information security's most important issues through
> interactions with peers, luminaries and emerging and established companies.
> http://p.sf.net/sfu/rsaconf-dev2dev_______________________________________________
> Pyobjc-dev mailing list
> Pyo...@li...
> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
 |