Aegidius
 Plüss   Aplulogo
 
in examples
Use the asterisk (*) character for wildcard searches
Searches on more than one word will be treated "as a phrase"
 
serverstatus
 www.aplu.ch
 www.java-online.eu
   
© 2014, V8.4
 
  Jython
 
 

 

Python/Jython for Beginners and Gurus

"The purpose of a programming language is to let software developers express their intensions as simply and directly as possible." [from Jim Hugunin, Jython Essentials, O'Reilly]

Right! For this reason the choice of the programming language depends on:

  • You, the software developer, student or teacher
  • Your intentions (your field of interest, the task you must accomplish)
  • What is simple for you (your programming skills, your way of thinking)

Thus any discussion about "good" or "bad" programming languages reflects always a personal point of view. Nevertheless when intentions are well defined, like in an educational institution were students start programming at about the same level with similar faculties, the choice of the programming language is much more restricted. It is clear that due to the complexity of a static strong typed language that needs compilation, Java may not be the pick of the bunch.

We remember the eighties where programming was widespread due to the simplicity and attraction of PCs and their inherent programming language: BASIC. At that time you turned the gear on and typed

> PRINT 2 + 3

and you were delighted to see the result immediately. Nowadays, using Java for the same trivial problem is MUCH more complicated, so awkward that no kid will do it just for fun!

  1. Install the JRE
  2. Install the Java Develoment Kit (JDK)
  3. Install and start an IDE, e.g. Eclipse, Netbeans
  4. Edit the program

    public class Adder
    {
      public static void main(String[] args)
      {
        System.out.println(2 + 3);
      }
    }

  5. Compile and execute the program.

Because programming has become too complicated for simple tasks, we want to go back to the roots. But running BASIC, Pascal or LOGO on a modern PC is like communicating using Morse code in the time of all present mobile phones. It may be a nostalgia for some people (e.g. a scout or a HAM radio operator like me), but for nearly everyone it is much more motivating to learn how to handle a mobile phone than a Morse keyer. Here comes the modern version of the BASIC language: it is called Python. Python is considered to be as simple as BASIC, but provides most of the concepts of modern programming languages. Download the latest Python distribution from www.python.org, install it and open the Python shell. Type

>>> 2 + 3

and you get the result. Not even the PRINT command is necessary! Python is elegant and brings you back the interactivity of interpreted languages and provides a lot of features and tools.

But you may miss the wealth of Java, especially when you think about the plethora of useful and well designed packages available in Java. Using cross-language calls of Java routines from Python is possible but cumbersome. But fortunately there is a simple solution how to use Java and Python side by side: Jython is a hybrid Python version that is fully integrated into the Java VM. With Jython you have most of the power of Python plus a seamless integration of all Java packages. Download the latest Jython distribution from www.jython.org, install it and open the Jython shell. It looks like Python! Most of the Python code also runs on Jython. But for GUI components you should better use Java Swing. In Jython you get a full fledged GUI with a few lines of code. Type

>>> from javax.swing import *

>>> win = JFrame("Welcome")
>>> tf = JTextField(preferredSize = (400, 20))
>>> win.contentPane.add(tf)
>>> win.pack()
>>> win.show()

and a window with a one line text field opens. Now you can populate the field interactively, type

>>> tf.setText("Hallo")

and the text appears miraculously in the window.

Welcom

You should consider to use Python/Jython in the following three situations:

  • You are a Python programmer and want access to the rich world of Java packages
  • Your are a Java programmer and want have a scripting interface for prototyping, testing and rapid prototyping
  • You teach programming languages for beginners and use Python/Jython instead of non-executable pseudo-code


Most of the Java packages available on this Web site are "Jython aware" and can be easily integrated into your Jython code. See the links on the sidebar for examples.

Let´s see how it works. Download the turtlegraphics package ch.aplu.turtle from the library jythonturtle.zip (download here, unpack and copy jythonturtle.jar anywhere on your computer, e.g. in the subdirectory jars of your local disk). Start Jython and import the package:

>>> import sys
>>> sys.path.append("c:/jars/jythonturtle.jar")
>>> from ch.aplu.turtle import Turtle

Here we assume a Windows OS with disk C:. Under Mac/Linux, if you have copied jythonturtle.jar into /home/jars directory, use

>>> sys.path.append("/home/jars/jythonturtle.jar")

Now you are ready to create a turtle called ann and tell her to move forward:

>>> ann = Turtle()
>>> ann.forward(100)

But ann has a lot of other capabilities. She may draw a star from the current location with just a few instructions. Try it!

>>> ann.fillToPoint(0, 0)
>>> for i in range(9):
...     ann.left(160).forward(200)

(You must indent with some spaces on lines starting with ... and then press twice the return key.)

jython


TigerJython
A Jython programming environment for beginners

Developing programs in a operation system console is outdated and cumbersome, but modern Integrated Development Systems (IDE) are an overkill for writing simple Python/Jython programs. The beginner has no need for all the sophisticated features of a professional IDE that only prevents him to concentrate on the programming task. One of the simplest programming environment for Jython is TigerJython, written in Scala by Tobias Kohn. The main operations are icon based and therefore independent of a specific language. Error messages and the tutorial are in English and German. The complete development system, including the Python/Jython interpreter is packed into a single jar file tigerjython.jar, so no installation other than the Java Runtime Environment (JRE) is needed. Important extensions (support for enums, libraries for Turtle graphics, 2D graphics window, game development, Lego NXT, socket programming, etc.) are included. Because of this, TigerJython can easily be used in a class room or computer pool without intervention of a system manager. The student may run it on his/her own USB stick that contains all program code and continue the work at home, even on a different operating system (Linux, Mac or Windows).

downlink Download tigerjython.jar

tigerIDE