amigapython2.png (3614 bytes)   What is AmigaPython?

  What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for rapid application development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed. Python is © Stichting Mathematisch Centrum Amsterdam (Netherlands).

  What is AmigaPython?

Python is developed mainly for the Unix platform. Because the full source code is available, ports to many other platforms exist nowadays. These include Macintosh, Windows and AmigaDOS ofcourse! AmigaPython is the Python version for AmigaDOS. Not only is it largely compatible with the 'mainstream' Unix Python version, but I've tried to make it a real Amiga program by adding some exciting Amiga features to it:-

  • Workbench support - you can launch the interpreter, the scripts, and data files linked to Python scripts from Workbench
  • ARexx support - connect to your favorite ARexx enabled Amiga application. Script them with Python instead of ARexx.
  • AmigaDOS extensions - write powerful Python scripts able to perform many Amiga-specific tricks with the system and your files
  • Easy to install by using the standard Installer utility

You might want to read the more detailed overview. Currently I compile it with SAS/C 6.58 for the 680x0 CPU family (actually 68030 or higher with FPU), and AmigaDOS 2.04 or higher. The full source code is available so you can take a shot at compiling it for other CPU's (PowerPC?) or with another compiler (GCC?).

  Why did I make it? The history

Shouldn't it be obvious by now? We all want a powerful open scripting language on our Amigas!
Seriously, the trigger for me to port Python to the Amiga was because of a programming assignment I had to do during my CS study. We had to program a prototype relational database engine in Python. The moment I mastered Python I was hooked and thought "I want this on my Amiga at home. This is way better than ARexx". I'm talking Python 1.2 here. Along came 1.3, 1.4 (which in my opinion was the first truly usable and powerful AmigaPython version), 1.5 and at the time of writing 1.5.1.

  Example: 'finger' for ARexx

Enough talk. Let's see some Python to give you an idea. Note that this example is for AmigaPython only because it uses the ARexx extension!

A finger tool for ARexx - just like the 'finger' command you know to find out about a user. Is it possible? YES! First let's make a ARexx host in Python which provides us with a FINGER ARexx command. Here is the source:

# Python interface to the Internet finger daemon.

import sys, string
import socket
import ARexx

FINGER_PORT = 79

def finger(host, args):
	print host,args
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect(host, FINGER_PORT)
	s.send(args + '\n')
	reply=''
	while 1:
		buf = s.recv(1024)
		if not buf: break
		reply=reply+buf
	return reply

# The function to handle the arexx commands,
def cmd_dispatcher(host,msg,cmd,args):
	try:
		if cmd=='FINGER':
			msg.result=finger(args['HOST'],args['USER'])

	except socket.error,string:
		msg.rc=ARexx.RC_ERROR
		msg.rc2=string[0]
	return 1

h=ARexx.host()
h.setcommand('FINGER','HOST/A,USER',{'USER':''},cmd_dispatcher)
print 'The ARexx port name is ',h.name

h.run()  

We have an interface to the finger daemon in Python, and a full-fledged ARexx host which provides us with two commands: HELP and FINGER. The last line (h.run()) activates the message loop which will handle all commands until you press CTRL-C. HELP is a default command which is provided by the ARexx.host class for you, including a simple implementation. I defined the FINGER command with arguments:-

FINGER HOST/A,USER

The hostname to finger is necessary while the user name may be omitted (it defaults to the empty string). Now, let's see how we can access this ARexx host when it's running:

/* Finger client in ARexx */
options results

address 'PYTHON'  /* fill in your port name here */

say 'FINGER @localhost...'
FINGER 'localhost'
say RESULT

say 'FINGER root@localhost...'
FINGER 'localhost root'
say RESULT

HELP FINGER
say 'The help string on the FINGER command is:' RESULT

HELP BOGUSCOMMAND
if RC=0 then do
	say RESULT
	end
else do
	say 'An error occured for HELP BOGUSCOMMAND:' RC2
end

Voila! Ofcourse you could now use AmigaPython's ARexx capabilities to write another Python script which duplicates this ARexx script's behavior and calls the ARexx host (AmigaPython running the Python script above in another shell), effectively using ARexx as an interprocess communication layer. I'll leave this as an exercise to the reader.

If you have any questions about this example, don't hesitate to ask them. E-mail: amigapython (at) razorvine.net


©Irmen de Jong.