Python On IBM I Experiences
Python On IBM I Experiences
For a long time, every time I was trying to learn a new language, the training was targeting how to
bounce red balls on the screen, and just the very last chapter of the course was mentioning
manipulating data, I remember that when I was training for a Java certification…
In this case, with Python, I tried just from the beginning to retrieve data from the data base and start to
work just on data, I think this is the real word for Python.
Python like other object oriented technologies, use SQL scripts to manage the data bases, I was trying
with MySQL and with IBM I (DB2), and the differences are just a couple of lines where you define the
connection, the rest is the same.
When I was using java years ago when I was in Borders, we were running java just to send our books
inventory information to Google Clouds every week, the coding was very simple, just defining the JDBC
connections with the IBM i and Google data base and send it, just using SQL scripts.
With Python definitely you can do the same, and in a more simple way, you don’t need to define fields
or long classes to accomplish this target.
One of the challenges I had using Python in my company, was justifying the use, my boss question was,
why we need to use Python if we could accomplish the same with RPG coding?...and this was a fair
question, at the end I found one reason (at least for me on that time), with Python I will create full
customized excel files including Charts, something that you cannot do with RPG at all, this was my
starting point, defining a real target, I wanted to “expand” my resources, not replace them.
The first challenge was installing Python on the IBM i, I will not get in details here, but in black
and white, you need to have functional your IBM I Access Client Solution, if you run this URL,
you will see the login screen: http://your.server.name:2001/
If this is not happen, you will need to contact your network administration to make the right corrections.
After you have it active, you will need to go to Tools/Open Source Package Management to start
working downloading the python module into the system, don’t use other methodologies, follow the
instructions from the IBM I Access menus (you will need qsecofr profile to accomplish this task).
You will be entering the SSH connection parameters like IP system address, user and password
Then you will be selecting the python3 version from the Open Source Package management:
After the installation, you have to make sure to define the path where you want to point the python
language:
call qp2term
After you set the path, you will be able to execute any python program from your IFS:
For example:
python3 /home/tdvitale/python/hello.py
Note: Every time you run the command ‘python3’, you have to be in the qshell interactive session
running just QSH or call the object qp2term
Now the magic line to run a Python program from a CL or RPG program:
If for some reason, the program is not found because the path definition I mentioned before
failed, you still can use the full path like this one:
QSH CMD('/QOpenSys/pkgs/bin/python3 /home/tdvitale/python/hello.py')
One important point to how we connect with the IBM i, use the old ODBC driver from the iSeries Access,
this is free if you already have it in your installation.
I will not go explaining Python syntax since you can find them in millions of documents and website on
line.
In python, you will need to download the module “pyodbc”, in QSH session you will need to run the
command “pip3”
Import pyodbc
Connection = pyodbc.connect(
System=’10.10.2.21’,
UID=’UserID’
PWD=’password’)
Myresult = mycursor.fetchall()
For x in myresult:
Print(x)
I this simple program, you define the connection with the driver, IP system you are connecting, UserID
and Password, and then you create the cursor to execute an SQL script captured on the object
“Myresult”.
Again, I will not go deep on Python syntax, just pointing the steps to make Python works for business.
Note: when you define the connection, you could define the connection to point MySQL if you want it
(you have to download with PIP3 first the right module).
Now, the big challenge is to create an excel file with charts and send it to the user, the python program
will be called from a CL program.
PGM
The first command is just to call the python program prfcharts.py from the
IFS.
The second command is just to send the document generated FlashRptCharts.xlsx
to the user.
To create the excel file, you need to download the module xlswriter, here you
will find how this work.
https://xlsxwriter.readthedocs.io/
And this is the entire program I wrote to generate the excel file and the
charts
https://www.ibm.com/support/pages/python-script-accessing-db2-data-
simple-example
Code to test it
#*****************************************************************#
#*****************************************************************#
# F U N C T I O N S #
#*****************************************************************#
# This program will print chars for Flash Report for the last #
# 12 Months. #
#*****************************************************************#
# L O G O F P R O G R A M M O D I F I C A T I O N S #
#*****************************************************************#
#*****************************************************************#
import xlsxwriter
# Import module to connect with DB with the core and set connection
conn = db2.connect()
cur = conn.cursor()
workbook =
xlsxwriter.Workbook('/home/TDVITALE/PythonDev/FlashRptCharts.xlsx')
worksheet2 = workbook.add_worksheet('Charts')
worksheet1 = workbook.add_worksheet('Data')
# Protect Sheets
worksheet2.protect()
worksheet1.protect()
colorBlue = workbook.add_format({'color':'blue'})
worksheet1.set_column(0,11,18) # Data
data=cur.fetchone()
Period = PeriodT
cell_title = workbook.add_format()
cell_title.set_bold()
cell_title.set_font_color('blue')
cell_title.set_font_size('16')
worksheet1.write_string (0,0,'Item',bold)
worksheet1.write_string (0,1,'Year',bold)
worksheet1.write_string (0,2,'Month',bold)
worksheet1.write_string (0,3,'Amount',bold)
worksheet1.write_string (0,5,'Year',bold)
worksheet1.write_string (0,6,'Month',bold)
worksheet1.write_string (0,7,'Amount',bold)
worksheet1.write_string (0,9,'Year',bold)
worksheet1.write_string (0,10,'Month',bold)
worksheet1.write_string (0,11,'Amount',bold)
worksheet1.write_string (1,0,'Cash',colorBlue)
"order by month(a.DS_DATE)")
data = cur.fetchall()
row = 1
# Cash
# Loans Total
# Assets
# Deposits
# Branch Visits
# Active Customers
row +=1
data = cur.fetchall()
row = 1
# Cash
row +=1
# Loans Total
# Assets
# Deposits
worksheet1.write_number (row + 38, 5, Year)
# Branch Visits
# Active Customers
"order by month(a.DS_DATE)")
data = cur.fetchall()
row = 1
# Cash
# Loans Total
# Assets
# Deposits
# Branch Visits
# Active Customers
row +=1
# Cash ******************************************************
chart1.set_x_axis({'name': 'Months'})
chart1.set_y_axis({'name': 'Amounts'})
chart1.set_size({'width': 620, 'height': 350})
chart1.add_series({
'categories': '=Data!$C$2:$C$13',
'values': '=Data!$D$2:$D$13',
'name': '=Data!$B$2:$B$2'
})
chart1.add_series({
'categories': '=Data!$G$2:$G$13',
'values': '=Data!$H$2:$H$13',
'name': '=Data!$F$2:$F$2'
})
chart1.add_series({
'categories': '=Data!$K$2:$K$13',
'values': '=Data!$L$2:$L$13',
'name': '=Data!$J$2:$J$2'
})
# Total Loans ******************************************************
chart2.set_x_axis({'name': 'Months'})
chart2.set_y_axis({'name': 'Amounts'})
chart2.add_series({
'categories': '=Data!$C$15:$C$26',
'values': '=Data!$D$15:$D$26',
'name': '=Data!$B$15:$B$15'
})
chart2.add_series({
'categories': '=Data!$G$15:$G$26',
'values': '=Data!$H$15:$H$26',
'name': '=Data!$F$15:$F$15'
})
chart2.add_series({
'categories': '=Data!$K$15:$K$26',
'values': '=Data!$L$15:$L$26',
'name': '=Data!$J$15:$J$15'
})
chart3.set_x_axis({'name': 'Months'})
chart3.set_y_axis({'name': 'Amounts'})
chart3.add_series({
'categories': '=Data!$C$28:$C$39',
'values': '=Data!$D$28:$D$39',
'name': '=Data!$B$28:$B$28'
})
chart3.add_series({
'categories': '=Data!$G$28:$G$39',
'values': '=Data!$H$28:$H$39',
'name': '=Data!$F$28:$F$28'
})
chart3.add_series({
'categories': '=Data!$K$28:$K$39',
'values': '=Data!$L$28:$L$39',
'name': '=Data!$J$28:$J$28'
})
chart4.set_x_axis({'name': 'Months'})
chart4.set_y_axis({'name': 'Amounts'})
chart4.add_series({
'categories': '=Data!$C$41:$C$52',
'values': '=Data!$D$41:$D$52',
'name': '=Data!$B$41:$B$41'
})
'categories': '=Data!$G$41:$G$52',
'values': '=Data!$H$41:$H$52',
'name': '=Data!$F$41:$F$41'
})
chart4.add_series({
'categories': '=Data!$K$41:$K$52',
'values': '=Data!$L$41:$L$52',
'name': '=Data!$J$41:$J$41'
})
chart5.set_x_axis({'name': 'Months'})
chart5.set_y_axis({'name': 'Amounts'})
chart5.add_series({
'categories': '=Data!$C$54:$C$65',
'values': '=Data!$D$54:$D$65',
'line': {'color': 'green'},
'name': '=Data!$B$54:$B$54'
})
chart5.add_series({
'categories': '=Data!$G$54:$G$65',
'values': '=Data!$H$54:$H$65',
'name': '=Data!$F$54:$F$54'
})
chart5.add_series({
'categories': '=Data!$K$54:$K$65',
'values': '=Data!$L$54:$L$65',
'name': '=Data!$J$54:$J$54'
})
chart6.set_x_axis({'name': 'Months'})
chart6.set_y_axis({'name': 'Amounts'})
chart6.add_series({
'categories': '=Data!$C$67:$C$78',
'values': '=Data!$D$67:$D$78',
'name': '=Data!$B$67:$B$67'
})
chart6.add_series({
'categories': '=Data!$G$67:$G$78',
'values': '=Data!$H$67:$H$78',
'name': '=Data!$F$67:$F$67'
})
chart6.add_series({
'categories': '=Data!$K$67:$K$78',
'values': '=Data!$L$67:$L$78',
'name': '=Data!$J$67:$J$67'
})
workbook.close()