Re: Transferring data between Python and Cocoa (maybe a newbie question)
Re: Transferring data between Python and Cocoa (maybe a newbie question)
- Subject: Re: Transferring data between Python and Cocoa (maybe a newbie question)
- From: Nir Soffer <email@hidden>
- Date: Wed, 6 Feb 2008 13:19:40 +0200
On Feb 6, 2008, at 00:51, Ritesh Nadhani wrote:
Hello
So I started developing my first real life Cocoa app as part of my
school work.
My app needs to connect to various servers to fetch similar data. The
issue is that the servers might use different way to get data. Some
use XML-RPC (majority of them are RPC), some use client-server etc.
User can add more server by adding a plugin. We already have bunch of
python scripts that just do that.
I added various Python files implementing functions with similar
name. E.g
server1.py
-- def getname():
// do xml-rpc stuff
// return data as dictionary
server2.py
-- def getname():
// do client-server stuff
// return data as dictionary
Now from Cocoa, I basically want to call these scripts with their
function common function name (which I know is implemented in all
python files) and get the result back.
I did some research on it and I see that we can use pyobjc to do
something like this:
http://www.jonathansaggau.com/blog/2006/07/python_from_objectivec.html
The issue is that PyObjc is bundled only with 10.5 and for other OS X
version we have to install it. My customers have both 10.4 and 10.5
and some even have 10.3 and PyObjc might or might not be installed and
I dont want to make the users install it just for my app.
You can add the needed parts from PyObjC to your app, the user will
not have to install anything. py2app will do this automatically for you.
Other option would be to output the data as XML from Python and parse
it back in Cocoa and build a dictionary out of it.
It the data contains only standard types, you can return the data in
plist format:
>>> import plistlib, StringIO
>>> xmlrpc_result = {'a': 1, 'b': 42}
>>> plist = StringIO.StringIO()
>>> plistlib.writePlist(xmlrpc_result, plist)
>>> plist.getvalue()
'<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//
Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/
PropertyList-1.0.dtd">\n<plist version="1.0">\n<dict>\n\t<key>a</key>
\n\t<integer>1</integer>\n\t<key>b</key>\n\t<integer>42</integer>\n</
dict>\n</plist>\n'
Are these the only two options or there are other ways to do it? What
do you people suggest.
You can use WebServiceCore to do the rpc work directly in your app
instead of using Python.
You can use AppleScript to do the rpc work, and run it with
NSAppleScript. But I would not recommend it because AppleScript must
run the main thread and you want to do rpc in a background thread.
Best Regards,
Nir Soffer
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden