fhg
Volume 10, Number 25 -- August 25, 2010

An Introduction to Python on IBM i, Part 1

Published: August 25, 2010

by Garry Taylor


Note: All of the files needed for this article are available for in one download here.


When we think of programming on IBM i, our minds usually turn to the venerable RPG, or maybe CL. Some of us will think of ILE C, and those who frequently entertain dinner guests from IBM will have the word "Java" seared into their brain, and mistakenly use "EGL" instead of friends' names. However, ask a UNIX or Linux aficionado to name a scripting language, and they will probably come up with Perl, Ruby, or very likely, Python.

Python was created as an educational language. Consequently, Python is simple to learn, generally lacks legacy cruft found in just about every other language, and has a formality in its approach that is easier to understand than Perl, but not as daunting as Java. Python is an object-oriented language, but so casual in its syntax that you will hardly know it.

Python exists on just about any platform you can think of, from Android-powered cell phones to OpenVMS clusters. This cross-platform availability makes it an ideal candidate as a standard language in any company, as programmers can work on any platform and not exist in a UNIX, Windows, or OS/400 ghetto. The OS/400 port of Python was created by Per Gummedal, and the Website and forum maintenance is the hard work of Aaron Fransen. I wish to thank both for their vital efforts in providing this superb programming language to this great platform.

Finally, Python is free. You can install it on as many machines as you like, whatever tier, however many processors or sockets, and the price is always the same.


Getting Started

The first step on your road to enlightenment is to install Python on your iSeries machines. This involves restoring three save files, which are available here.

The files you need are python25.zip, lib25.zip, and python.zip. Download these onto your desktop computer and uncompress them. The last save file contains an updated version of the Python service program. This service program is not 100 percent required, but I recommend you get it.

If you have a normal method for copying save files to your iSeries, use that method. Otherwise, follow the instructions on the www.iseriespython.com website, which you can find here.

I made some changes, as these instructions seem to contain a couple of errors that prevented installation on my machine.

1. Create the library in which Python will reside:

CRTLIB PYTHON25

2. Create two save files to populate with the save files you downloaded:

CRTSAVF PYTHON25/PYTHON25
CRTSAVF PYTHON25/LIB
CRTSAVF PYTHON25/PYTHON

3. Create two IFS directories:

MKDIR '/usr/python2.5'
MKDIR '/usr/python2.5/LIB'

4. Use FTP to upload the save files to your iSeries. In the command line FTP client on your desktop computer, enter the following:

  • ftp iserieshost--Where iserieshost is your iSeries IP address or hostname
  • cd python25--Go into the PYTHON25 library
  • binary--Switches to binary mode, so the save files are not treated as text
  • put python25--Upload first save file
  • put lib--Upload second save file
  • put python--Upload third save file

5. Restore the objects:

RSTOBJ OBJ(*ALL) SAVLIB(PYTHON25) DEV(*SAVF) SAVF(PYTHON25/PYTHON25)
RSTOBJ OBJ(*ALL) SAVLIB(PYTHON25) DEV(*SAVF) SAVF(PYTHON25/PYTHON)

RST '/QSYS.LIB/PYTHON25.LIB/LIB.FILE' OBJ(('/usr/python2.5' 
'*INCLUDE' '/usr/python2.5'))

6. The next step is to change a system value so that Python can run with no default locale. This means that Python will be able to read and write simple system values such as the date in the format that it expects. It is recommended you choose option "c" for a live system. Then . . .

a) Change the system default:

CHGSYSVAL QLOCALE '/QSYS.LIB/NO_NO.LOCALE'

b) Change one user:

CHGUSRPRF username LOCALE('/QSYS.LIB/NO_NO.LOCALE')

c) Change the current job:

ADDENVVAR ENVVAR(LANG) VALUE('/QSYS.LIB/NO_NO.LOCALE')

7. And you're done! You can now launch the Python interpreter:

PYTHON25/PYTHON

Key your first line of Python: print "Hello!" And press "Enter."

You now have a working Python system, and you're ready to write your first Python script.


A Very Basic Script

Python scripts are stored as stream files in the IFS. You can write your scripts in any editor you like. From a green screen session, you can use EDTF. I like to use a graphical editor with syntax highlighting, and open the file over FTP or SFTP. TextWrangler is a good choice on the Mac. If you're using Windows, Linux, or UNIX, try JEdit. Both are free, and both can open files directly over FTP.

Here is the first script, hello.py, which you can download here.

def main():
i = 0
while i < 10:
print str(i)+") Hello"
i += 1
main()

If you like, you can copy this code and paste it directly into your stream file, but you need to be aware of a couple of things.

If you like, you can copy this code and paste it directly into your stream file.

Python requires code to be indented to indicate where blocks start, so there is one tab before "i = 0", and two tabs before the print statement. This is very important to Python, and also enforces a clean writing style.

Let's go through this script line by line:

The first line defines a function with the name of main. A set of parentheses in which we can define parameters that are passed to the function follow the function name. In this case there are no parameters. Ending the line with a colon tells Python that a block of code follows.

In the main function, we first define a variable, and set it to the value of 0. In Python, you don't need to say what type of variable it is, such as character, integer, decimal, etc. You just give a variable a name and set it to a value. Python works out the rest.

Now we start a "while" loop. This is very similar to a "while" loop in most other languages. In English, we are saying "while the value of i is less than 10, repeat this block of code". Again, we end the line with a colon to tell Python that a block of code is next. In the block of code, the first word "print", rather predictably, prints whatever line follows. On UNIX systems, print would print to your screen or terminal. On the iSeries, the printed data goes straight into a spool file. After the print statement, we have the str() function, which converts just about any value into a string (a stream of characters). Using str() allows us to join the value of the numeric value of i to to the character string ") Hello".

The final line of the while loop adds 1 to the variable "i", so each time the loop is iterated, i increases by one. After 10 loops, i will equal 10 and the loop will exit. The expression i += 1 is a quick shorthand for "i = i + 1" or "add 1 to i".

Now we have exited the while loop, and as the end of the loop is the last line in the function, we exit the function, too.

The last line in the file calls the main() function. Without this line, Python would interpret the file, but not know where to enter processing, and exit. Since Python doesn't require header files or prototypes, we could just as easily call main() before it is defined. Python will race through the file and find the function for us.

To run the Python script, type PYTHON25/PYTHON on a CL command line and press F4.

Enter the path to the script, which will be something like:

/home/garry/hello.py

Press Enter and the script will run. If entered correctly, the command line will reappear immediately. Use WRKSPLF to look at spool files, page down to the bottom of the list, and display the last QPRINT spool file. You should be greeted with:

0) Hello
1) Hello
2) Hello
3) Hello
4) Hello
5) Hello
6) Hello
7) Hello
8) Hello
9) Hello

Let's try something a little more useful.


Another Script

This time we're going to use Python to do something bordering on helpful. This script takes a tab-separated stream file of customer details, and for each customer, checks if he/she has a valid email address and logs a suitable message to the spool file. This script is available to download here.

def file_read(filename):
fh = open(filename,"r")
data = fh.read()
fh.close()
return data
def main():
data = file_read("/home/garry/customers.txt")
lines = data.split("\n") #Split on a line feed
for line in lines:
cols = line.split("\t") #Split on a tab
email = cols[1]
if email.find("@") != -1:
print "'" + email + "' appears to be a valid email address"
else:
print "'" + email + "' does not seem to be a real email address"
main()

This script contains two functions. The main() function is the first function to run. You can call your functions just about anything you like. I have called it main(), as this is what the first function called is named in C. The other function, file_read(), takes one parameter, the name for a stream file. The file is opened, all data read from it and stored in a variable called "data". The file is then closed, and the data is returned out of the function to where the function was called from. It is prudent to make tiny functions like this for just about everything. That way if you want to put in some error handling or debug print statements, you only need to put it in one place, and not all over your program. You will also notice a lot of "#" characters in this script. These are for commenting in Python. Any text after a "#" is ignored until the end of the line.

The main() function calls file_read() to get the contents of the stream file, then splits up the file on a line feed, putting each line in its own element of a tuple (a list, a sort of array). Now we can iterate over those lines with the "for" statement. The for statement is saying "For each item in this list, do this…", so it is very much like a while loop, except that rather than check the statement is true for each loop, it runs the loop until it runs out of items in the list.

Now we're into the block of code run by the "for" statement, and the first action is to again use the "split" method on the line, this time we split on a tab ("\t"), so that each line is split into a tuple that contains each bit of information about a single customer. As it happens, the stream file contains data like this:

name<tab>email<tab>phone<linefeed>
name<tab>email<tab>phone<linefeed>
name<ta....

You can download this file here.

Name is element 0, email is element 1, and phone is element 2. So in our script we can make a new variable to hold email, by setting it to the value contained in cols[1], i.e., element 1 in the "cols" tuple. Now we can use the "find" method in the string to search for an "@" symbol. If an "@" symbol is found, its index in the string will be returned, otherwise the value -1 is returned. So now we can use an "if" statement to decide which print statement to use. The above "if" statement is saying: If the index of "@" in variable email is not -1, then print that the email address is valid, else print that it is not a valid email address. Now, the fact that this string contains an "@" symbol is not complete proof that the email address is valid, but it is enough as a quick demonstration.

On running this new script, check your spool files again. You should see output of:

'garry@company.com' appears to be a valid email address
'nadine.company.com' does not seem to be a real email address
'andy@apple.com' appears to be a valid email address
'damo.ibm.com' does not seem to be a real email address

So in this case, our script has correctly determined what is a valid email, and what is not.


What We Have Learned, and Will Learn

In part one of this introduction to Python, we have learned how to create a couple of simple Python scripts and execute them. We have used what are possibly the two most vital constructs in any programming language: "while" and "if". If you are new to object-oriented techniques, it may also surprise you to learn that you have been using them. The strings are in fact Python objects, and the "split" and "find" capabilities are in fact methods within these objects.

We have also learned how to open, read from, and close stream files, work with their content, and react differently based on what we find in the content.

In part two of this series, we will discover how to access built in SQL features in OS/400 using Python. We'll set up databases, insert data, and learn how Python makes it very easy indeed to use the superb SQL features in IBM i. We will also learn how to better integrate Python into existing workflows and jobs, using its strengths to complement native CL or RPG programs.


Garry Taylor is a professional software developer, working with IBM i, Solaris, and Mac. Based in London, Garry is a co-founder of The Escapers, and developer for Kisco Information System. Send your questions or comments for Garry to Ted Holt via the IT Jungle Contact page.




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
SEQUEL SOFTWARE

MYTH: Accessing Data Is Complex and Expensive

REALITY: Dispel the myths of IBM i data access and
bring data to your desktop quickly with SEQUEL.

From simple ad hoc queries to executive dashboards, SEQUEL delivers
IBM i data in the format that works best for you. SEQUEL is the fastest,
easiest way to access your IBM i data.

Click here for a FREE Information Kit.


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Bytware:  Try StandGuard Network Security FREE for 30 days
DRV Technologies:  SpoolFlex transforms & distributes System i reports as PDF & Excel in REAL time!
COMMON:  Join us at the Fall 2010 Conference & Expo, Oct. 4 - 6, in San Antonio, Texas


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
IBM Ducks i Pricing on Most Entry Power7 Servers

BladeCenter S Express i Edition Gets a Power7 Upgrade

The Power 795: Cheaper Performance, Expensive Software

As I See It: The Once and Future HP Way

An Encryption Horror Story

Four Hundred Stuff
PHP and JavaScript Come Together in Zend Studio 8

SafeStone Taps RSA for SIEM Expertise

SkyView Gets Tough on User Profiles

Profound Updates I/O Handler for RPG Open Access

IGEL Adds 5250 Emulation to Linux Thin Clients

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
August 7, 2010: Volume 12, Number 32

July 31, 2010: Volume 12, Number 31

July 24, 2010: Volume 12, Number 30

July 17, 2010: Volume 12, Number 29

July 10, 2010: Volume 12, Number 28

July 3, 2010: Volume 12, Number 27

TPM at The Register
US puts $30bn of IT projects up for review

AMD nabs ex-Intel techie as server CTO

Oracle names self virtualization king

Big biz loved Dell servers and storage in Q2

Mobile PC buyers buying peppier boxes

HP rings up Hurd's final quarter

HP hires headhunter to replace Hurd

AIX 7.1 moves forward to Power7 iron

Amazon challenges cloudy startups

IBM whips out its TPC-C...cluster

SGI previews Q4 financials

IBM completes Power7 server arsenal

THIS ISSUE SPONSORED BY:

SEQUEL Software
WorksRight Software
System i Developer


Printer Friendly Version


TABLE OF CONTENTS
An Introduction to Python on IBM i, Part 1

DB2 for i: Process Stored Procedure Result Sets as Cursors

Admin Alert: One Year Out--Preparing for Your Next Power IBM i Upgrade

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2010 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement