Jython, Swing & Curry
Jython is currently, by far the most useful of the the three main Python implementations, Jython, CPython and IronPython. It allows access to the Java standard library which often provides more standard, better defined, and certainly better documented alternatives to much of what is in the Python standard library. It is also more cross-platform than .NET bound IronPython and offers superior multithreading support to CPython.
Its well known within the Jython community that having first-class functions in Python makes development with Swing more productive in Jython than it is with Java, by allowing us to attach functions or methods directly to Swing callbacks such as
actionPerformed, without the need for anonymous classes or excessive need for interface implementation. This example taken from What’s Good About Jython? shows this in action:
import javax.swing as swing def printMessage(event): print "Ouch!" if __name__== "__main__": frame=swing.JFrame(title="My Frame", size=(300,300)) frame.defaultCloseOperation=swing.JFrame.EXIT_ON_CLOSE; button=swing.JButton("Push Me!", actionPerformed=printMessage) frame.contentPane.add(button) frame.visible=1
The most powerful result of learning different programming languages is being able to apply concepts learned in one language, in another language. This type of ‘technology transfer’ drives progress in many science and engineering fields. This cross-fertilisation has always been possible, but is becoming both more feasible and popular thanks to multiparadigm languages such as Python and Ruby, and wider use of the STL and Boost in Standard C++ which encourage and facilitate functional and higher-order programming style. There are even books on the subject of Higher Order Perl.
Although its had many notable ongoing and recent successes (e.g. Emacs, Darcs, Pugs) the functional community is unfortunately widely perceived as academic or impractical. Indeed, Guido van Rossum has recently threatened to remove some functional programming support from Python, bizarrely whilst simultaneously adding higher-order features such as decorators. This is unfortunate because functional programming style and idioms can go a long way to improving software quality in any language – functions without side-effects are far easier to test, debug and reason about.
For example, the technique of currying - fixing a function argument and returning a new function which takes the remaining parameters can be very useful for development with the Swing UI toolkit supplied with Java.
In this example extracted from one of my own projects, where the user interface features two radio buttons, we look at how some higher-order techniques can be used to aid user-interface programming with Swing. In the code below,
fixed_radio = JRadioButton(text="Regular columns of fixed width", actionPerformed = self.handleFixedOption) separated_radio = JRadioButton(text="Fields separated by spaces, commas, or other characters", actionPerformed = self.handleSeparatedOption)
two bound-methods are attached to the
actionPerformed event of each button.
def handleFixedOption(self, event=None): self.controller.setOption(self.controller.FIXED) def handleSeparatedOption(self, event=None): self.controller.setOption(self.controller.SEPARATED)
We don’t actually use the
event parameter in this case, but it will be passed to the handler function we provide from
actionPerformed, so we must accommodate it. These two methods are identical apart from the value passed to
setOption internally, so we could factor out the common code and these two functions with a single function and pass the option value
SEPARATED in as an extra argument,
def handleOption(self, option, event=None): self.controller.setOption(option)
However, we now have a difficulty in that we can no longer bind our new function directly to the actionPerformed handler of the buttons, since
actionPerformed expects a function which takes single
event argument. We know at coding time which
option value should be attached to each button – but we don’t know until the code is run, which event will be passed from
actionPerformed. We need to bind the
option argument before the
event argument. The solution to this is currying.
Rather than writing our own currying facility, we can use on off-the-shelf implementation provided by the Xoltar Toolkit which provides some functional programming support in Python. In particular we use the
curry function to curry our
handleOption function into some new functions that only take take a single
from functional import curry fixed_radio = JRadioButton(text="Regular columns of fixed width", actionPerformed = curry(self.handleOption, self.controller.FIXED)) separated_radio = JRadioButton(text="Fields separated by spaces, commas, or other characters", actionPerformed = curry(self.handleOption, self.controller.SEPARATED))
The curry function takes the function to be curried as its first argument. In this case we pass the bound-method
self.optionHandler. The second argument to
curry is the value of the first parameter to the function to be curried, which want tobe fixed to this value in the created function, for example,
self.controller.FIXED. The result of the
curry function is a new function which accepts one less argument than the original function – so in this case we get a new function which only accepts the remaining
event parameter, and is therefore equivalent to the handleFixedOption function we started with, but which we have now avoided having to write.
The material advantage with such a simple example is minimal, but when such techniques are used throughout a project the savings in space and complexity can be substantial. The combination of functional concepts from borrowed from languages such a Haskell, facilitated by tools provided by the Xoltar Tookit, developed in dynamically-typed Jython and exploiting one of the better designed user interface APIs in the form of Swing can be concise and powerful.