OpenQwaq Python Integration

This is the first part of our series about the Cool bits of OpenQwaq. OpenQwaq has a complete integration with Python. What this means is that from OpenQwaq you can call Python code simply via something like here:

Python initialize. "initialize the bridge"
module := Python loadModule: 'ExampleModule'.
result := module call: 'add' with: 3 with: 4.
instance := module call: 'ExampleClass'.
instance call: 'exampleMethod' with: instance.

Of course, you can also do some fun reflective exercises such as:

Python initialize. "initialize the bridge"
modules := (Python loadModule: 'sys') get: 'modules'.
modules keys do:[:moduleName|
    moduleName = 'ExampleModule' 
        ifTrue:[Transcript show: 'ExampleModule is currently loaded']].

The bridge also allows callbacks from Python into Squeak code. From the Python you can talk back to Squeak like here:

import Squeak

def sampleCallback(rcvr, message):
    Squeak.send(rcvr, selector, ())

Squeak.send() function takes receiver, selector, and args and delivers it to the running OpenQwaq instance. OpenQwaq picks it up via the Python callback process and delivers it the proper receiver.

There is lots of fun stuff you can do that way. The best way to experiment with it, is to simply play around and inspect various results of operations. You can always dig deeper and you’ll almost certainly learn something about Python. I know I did 🙂


%d bloggers like this: