28 Apr 2007

AUC Python and Objective C Workshop

Thursday - Day 1;

It was 29C today, very nice weather. I arrived in Sydney yesterday. The plane trip was good, compared to the fact that i kept on thinking it would be a 45 minute ride on a Mad Mouse, but it wasn’t. Other than two or three “major” air pockets or turbulence, it was a good flight. Maybe watching Heroes also helped. (I did it!)

Yesterday, i just came down to my hotel checked-in left my bag and then went scouting for Cliftons Training Centre. It wasn’t that hard to find, a train ride to Circular Quay Station, just a couple of minutes of walking down George St to the left side. I did this because if i was going to get lost i would have done that yesterday instead of today. I didn’t get lost. The cool thing is that the centre is close to Sydney Harbor Bridge and the Opera House. So i walked a bit and took some photos with my N73. Then went back to my hotel.

The next day (today,May 3rd) I woke up at 7:30am. After i got ready i went out to Central Station. I left early as i didn’t have any wireless connection in my hotel. I went out looking for a cafe where they had wireless next to the training centre. Chance had it i found Starbucks only to realise that to access the internet you need a prepaid card or a credit card and i had neither. By the time i drank my Iced Cafe Latte and it was time for me to head to the centre.

Walking into the centre going up the lift (i hate the things by the way) asking for the direction to the room. Then stepping into the room. Our tutor is James Bekkama a PhD student at CSU. After all the six students arrived it started.

The usual introduction started, with everyone saying where they are from and experience with Python and/or Objective C. Me and another student were the only two with experience, but me being the only one who was new to the Mac scene. After the introduction we where asked to install Xcode if we haven’t already. I had already installed Xcode and even PyObjC. After all of us was up and running (except for one person who did some weird thing with his Mac moving /etc somewhere and aliasing it, Xcode not liking, he had to re-install Mac OS X!).

The first day was all Python. Starting off with the basics of the language, such as it being dependent on indentation for code blocks which is always a let down for people who are coming from C/C++, Java etc. Using the interactive shell, variable assignment, retrieving input from the user. Then we moved on to using Xcode for Python development. This was needed as PyObjC includes templates for Xcode which eases the development process. Setting up Xcode for Python had some steps to follow but not hard to do but easy to forget. After setting up Xcode we moved on to general language syntax. After this we started writing up a Address Book script. Starting off we a basic function based script then converting it into a more cleaner Object Oriented script.

Before i forget to mention we had morning tea during lunchtime, and lunchtime during afternoon tea. It was good fun. After lunch we started on a CGI script to create a web interface for adding entries into the Address Book. This was interesting because i couldn’t get Python CGI scripts to run under Apache that is pre-installed on my MacBook. I was trying to get mod_python installed instead of just using the magic hash bang thing!

If i admit it, the first day was a bore, except for the CGI part. James does know his quite a bit about CGI (Python,Objective C in general as well) scripting, but i was more interested in the Objective C bridge. To learn to develop applications that look like “real” Mac applications but programmed with Python with no need to learn Objective-C.

Friday - Day 2;

Second day i didn’t wake up that early as there is trains more or less every 2-8 minutes that goes around the City Loop in Sydney. So i arrived at the training centre ten minutes earlier for a quick email and Digg.com check (10 minutes isn’t enough for a Digg ;) ) .

We started by configuring a new PyObjC project on Xcode. Again there were some steps we needed to follow. Then the fun began, with a simple example.

>>> import AppKit
>>> AppKit.NSBeep()

You can guess that all this does is a “beep” sound, very useful just like the classic “Hello World” example. No seriously, it is an easy way of checking if PyObjC is correctly installed.

Keep in mind that i’ve had no previous experience with Objective-C. When i got my MacBook i said to myself “might as well learn the native development framework and language. So i looked at some tutorials on Objective-C and some screen cast on YouTube about Objective-C development. When i saw NSObject or NS* i was like “what is NS”, then NIB files. It was just too many things to learn and again university was like “here more work for you, we don’t really want you to have a social life”. So i abandoned my sojourn into Cocoa.

Now thanks to this workshop i now know what NS and NIB are. Next Step and Next Interface Builder respectively. Once everyone confirmed that PyObjC was installed and working we then started looking at the syntax mapping between Objective-C and Python. What i mean by syntax mapping is this. The way to call a method of an object in Objective-C is done like the following;

[aObject doThis:art1 withThis:arg2];

and in PyObjC this would be translated to


i like this better. No square brackets or anything, except the underscores, which are needed to represent the colon’s in Objective-C. (Colon’s in Python are used to denote the beginning of a block).

So converting any Objective-C API call to PyObjC is easy, whenever you see a colon you replace it with an underscore in Python. Here is another example of creating NSString objects in PyObjC

myString = NSString.stringWithString(u”Hello World”)

instead of a very ugly looking Objective-C version

myString = NSString.alloc().initWithString
(u”Hello World”)

After this we had morning tea. Once back we started working on a Aqua interface for the Address Book we created yesterday. I must admit the Interface Builder is a very handy tool. You add your buttons and then create outlets and connect them VISUALLY (holding down ctrl?). We worked on this until lunch then we came back to more fun stuff, Bluetooth.

PyObjC can directly control Bluetooth via the NSBluetoothIO (if i remember correctly Xcode is crashing for some reason) but we just used a framework, lightblue. To do a device scan of bluetooth devices all you need to do is this,

>>> import lightblue
>>> lightblue.finddevices()

making sure Bluetooth is switched on. This call returns a list containing all devices with Bluetooth switched on and in Discoverable mode, with their MAC addresses. Using this information it is very easy to create some sort of Bluetooth proximity detector, which James did, continuously search for devices, and see if the device your looking for is in range, meaning that it was detected.

Then it was all over and time to head back. Everyone said their goodbye’s and nice to “meet you”. I made some good connections into the Mac development world.