[Pythonmac-SIG] lists and accessors

Ronald Oussoren ronaldoussoren at mac.com
Thu Jul 8 23:16:10 CEST 2010


On 8 Jul, 2010, at 23:00, Georg Seifert wrote:

> Hi,
> 
> In my app I will access my cocoa data classes from a python script (run from within the app). This works fine, I can access all the methods and properties. For some of the array properties I use dedicated getter and setter (getItemAtIndex: , setItemAtIndex: ...).
> 
> I have a cocoa object:
> 
> @interface myObject : NSObject {
> 	NSMutableArray *_items;
> }
> @property(retain) NSArray * items;
> - (void) setItem:(id) Item atIndex: (int) Index;
> - (id) itemAtIndex: (int) Index;
> @end
> 
> Is it possible to access like:
> 	object.items()[i]	# it would be even better to get rid of the parenthesis.
> or
> 	object.items.append(NewItem)
> 
> Calling from python should call the cocoa accessors instead of accessing the list directly. (I need to set some values and implemented undo in the accessors.)
> 
> My users are used to plain python scripting environments and don’t like the cocoa method names.
> 
> Is it somehow possible to tell python what accessors to use or is there a way to supply python wrappers? Or could I build a bridgesupport file?

Not easily. I've implemented code for the upcoming 2.3 release of pyobjc that will generate the accessors for you when you create a property in Python, but haven't gotten around to implementing the other direction.

Something that likely works (but I haven't tested this):

   class ItemProxy (object):
       def __init__(self, owner):
           self._ower = owner

       def __getitem__(self, i):
          return self.itemAtIndex_(i)

       ...

   del myClass.__dict__['items']
   myClass.__dict__['items'] = lambda self: ItemProxy(self)

A future version of PyObjC will allow you to configure ObjC properties in native classes using bridgesupport files and/or function in the objc module.

Ronald

P.S. I want to release PyObjC 2.3 this saturday, but need to squash a couple of bugs in the 64-bit support before I do the release.

P.P.S. The major feature in PyObjC 2.3 is support for Python 3. 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3567 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20100708/3a2f2a3e/attachment.bin>


More information about the Pythonmac-SIG mailing list