[Pythonmac-SIG] PyObjC tutorial without Xcode
Dav Clark
dav at alum.mit.edu
Sat Jul 19 01:35:41 CEST 2008
I sent this just to Craig, but figured I should probably send to the
group in retrospect - at least this way something'll be online in the
pythonmac-sig archives...
Below my signature is a sample script demonstrating a really minimal
use of pyobjc. This is meant to be run from the command line, and
uses the BTop2 framework from Perfectly Scientific. You can download
that framework for free, but it won't do much if you don't have the
hardware!
Not exactly a tutorial, but darned simple for sure... I can't remember
if that __getattr__ thing ever worked... I don't think it did. The
Exception thing is _way_ useful (as indicated in the comments there).
The standard exception messages you'll get while using pyobjc are
close to useless.
I'll add also that I'd not be averse to chipping in to a non-Xcode
pyobjc tutorial online somewhere. I'm not terribly self-motivated on
that, but would do if someone asked for more.
Cheers,
Dav
--
Statistical Motion R&D
http://socrates.berkeley.edu/~ivrylab/
import objc
# # Way useful for debugging
# import PyObjCTools.Debugging as Debugging
# Debugging.installVerboseExceptionHandler()
btop_path = objc.pathForFramework('/Users/dav/Code/bTop.framework')
objc.loadBundle('btop', globals(), bundle_path=btop_path)
# Not sure why I need to do this... I shouldn't!
BTopBoard.setBTopFirmwareDirectory_(btop_path + '/Versions/A/Resources')
class BoardContainer(NSObject):
"""BoardContainer should be initialized by BoardControl below"""
board = None
def bTopAddBoard_(self, board):
self.board = board
self.board.setPortBit_direction_('B', 255)
self.board.refreshDigitalPortValues()
def send(self, val):
self.board.setPortBit_value_('B', 255-val)
self.board.refreshDigitalPortValues()
class BoardControl:
"""Instantiate the BoardContainer and provide a more pythonic
interface
Ultimately, this should implement a standard Parallel Port API"""
board = None
def __init__(self):
# Cocoa objects are constructed differently...
self.board = BoardContainer.alloc().init()
BTopBoard.setBTopPrimaryObserver_(self.board)
BTopBoard.allocBTopObserver()
# # try writing a function dispatch to catch undeclared funcs...
see
# # what's going on!
# def __getattr__(self, name):
# def handler(*args, **kwargs):
# print '*name*', name
# print '*args*', args
# print '*kwargs*', kwargs
# return handler
def send(self, val):
self.board.send(val)
More information about the Pythonmac-SIG
mailing list