• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Passing Parameters To Python

    August 5, 2019 Mike Larsen

    Python scripts offer great benefits to developers, whether used standalone or in conjunction with RPG programs. As I’ve been exploring Python and recently I had a need to execute a Python script from an RPG program. In addition to executing the script, I also wanted to pass it parameters.

    One of the benefits of passing parameters is to give us the ability to soft-code programs or scripts. Soft-coding makes programs more flexible and re-usable, and helps to reduce maintenance.

    To illustrate how to do this, I’ve coded a very simple RPG program and an even simpler Python script. I’ll start by showing the Python script, and then I’ll show how to execute it from RPG using QShell.

    This story contains code, which you can download here.

    I start by importing the “sys” Python modules, shown in the first piece of code, which allows me to print to the terminal and grab the parameters.

    import sys 
    

    The rest of the code show next is just printing the parameters to the terminal. I told you it was a very simple script!

    print (sys.argv[0]) # prints the name of the Python script
    print (sys.argv[1]) # prints parameter 1
    print (sys.argv[2]) # prints parameter 2
    print (sys.argv[3]) # prints parameter 3
    print (sys.argv[4]) # prints parameter 4
    

    To show the results of the script, I can execute it from SSH Terminal in ACS (Figure 1).

    Figure 1. Execute Python script

    I call my script parameters1.py and pass it four parameters (“lexie”, “slayer”, “yankees”, and “two words”). You may be wondering why I enclosed the fourth parameter — “two words” — in quotes. I did this because Python interprets a space as the end of a parameter. If I hadn’t enclosed the fourth parameter in quotes, Python would have seen these as two separate parameters and I would have actually passed five parameters instead of four.

    Looking at the results, you’ll see that there is an additional piece of information that was displayed on the terminal: “parameters1.py”. That’s because the actual first argument passed to the Python script is the name of the script. It printed because I coded the script to print the argument zero, as shown in the first line of code from Figure 2.

    Now that the Python script is working, it’s time to see how we can execute this from RPG. I populated four variables with the parameter values I’m sending to the script, seen in the next piece of code below. In a production process, these would likely be passed in as parameters or retrieved from a table, but I hard-coded them here to make the example easier to follow.

    dcl-s firstParameter  char(10) inz('lexie');
    dcl-s secondParameter char(10) inz('slayer');
    dcl-s thirdParameter  char(10) inz('yankees');
    dcl-s fourthParameter char(10) inz('two words');
    

    Next, I define the path to my Python script as well as the name of the script.

    pathToPython = 'python3';
    pythonScript = 'parameters1.py';
    

    The last part of the RPG program calls QShell to execute the Python script.

       // Use Qshell to execute the Python script.
       //
       // Note: Parameters are detected by a space in Python, so the
       // 'fourthParameter' needs to be in quotes since it has two
       // words separated by a space.  if I didn't put it in quotes,
       // Python would see them as two separate parameters.
    
       CmdStr = 'Qsh Cmd(' + quotes +
                             %trim(pathToPython)       + ' ' +
                             %trim(pythonScript)       + ' ' +
                             %trim(firstParameter)     + ' ' +
                             %trim(secondParameter)    + ' ' +
                             %trim(thirdParameter)     + ' ' +
                       '"' + %trim(fourthParameter)    + '"' + ''')';
    
       Callp Run(Cmdstr:%Size(CmdStr));
       
    

    When I call the RPG program from a command line, it opens a QShell terminal to show the results (Figure 2).

    Figure 2. Results from the Python script

    I received the same results as I did when I ran the Python script directly, and that is exactly what I expected.

    So, I executed my RPG program from a command line on IBM i, but I thought it would be cool to try something else. What if I wanted to call my RPG from SSH Terminal? Let’s give it a shot.

    It turns out that all I have to do is execute the system command to call the RPG program (Python_ex1) and the results of the Python script are printed to the terminal (Figure 3).

    Figure 3. Calling an RPG program from SSH Terminal

    Pretty cool, right?

    Although the example is pretty simple, you should now have the tools you need to build production processes that use both RPG programs and Python scripts together. The complete code for the Python script and RPG program used in this article is available for download.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, Python, qshell, RPG

    Sponsored by
    UCG Technologies – Vault400

    Do the Math When Looking at IBM i Hosting for Cost Savings

    COVID-19 has accelerated certain business trends that were already gaining strength prior to the start of the pandemic. E-commerce, telehealth, and video conferencing are some of the most obvious examples. One example that may not be as obvious to the general public but has a profound impact on business is the shift in strategy of IBM i infrastructure from traditional, on-premises environments to some form of remote configuration. These remote configurations and all of their variations are broadly referred to in the community as IBM i hosting.

    “Hosting” in this context can mean different things to different people, and in general, hosting refers to one of two scenarios. In the first scenario, hosting can refer to a client owned machine that is housed in a co-location facility (commonly called a co-lo for short) where the data center provides traditional system administrator services, relieving the client of administrative and operational responsibilities. In the second scenario, hosting can refer to an MSP owned machine in which partition resources are provided to the client in an on-demand capacity. This scenario allows the client to completely outsource all aspects of Power Systems hardware and the IBM i operating system and database.

    The scenario that is best for each business depends on a number of factors and is largely up for debate. In most cases, pursuing hosting purely as a cost saving strategy is a dead end. Furthermore, when you consider all of the costs associated with maintaining and IBM i environment, it is typically not a cost-effective option for the small to midsize market. The most cost-effective approach for these organizations is often a combination of a client owned and maintained system (either on-prem or in a co-lo) with cloud backup and disaster-recovery-as-a-service. Only in some cases of larger enterprise companies can a hosting strategy start to become a potentially cost-effective option.

    However, cost savings is just one part of the story. As IBM i expertise becomes scarce and IT resources run tight, the only option for some firms may be to pursue hosting in some capacity. Whatever the driving force for pursing hosting may be, the key point is that it is not just simply an option for running your workload in a different location. There are many details to consider and it is to the best interest of the client to work with an experienced MSP in weighing the benefits and drawbacks of each option. As COVID-19 rolls on, time will tell if IBM i hosting strategies will follow the other strong business trends of the pandemic.

    When we say do the math in the title above, it literally means that you need to do the math for your particular scenario. It is not about us doing the math for you, making a case for either staying on premises or for moving to the cloud. There is not one answer, but just different levels of cost to be reckoned which yield different answers. Most IBM i shops have fairly static workloads, at least measured against the larger mix of stuff on the public clouds of the world. How do you measure the value of controlling your own IT fate? That will only be fully recognized at the moment when it is sorely missed the most.

    CONTINUE READING ARTICLE

    Please visit ucgtechnologies.com/IBM-POWER9-systems for more information.

    800.211.8798 | info@ucgtechnologies.com

    Article featured in IT Jungle on April 5, 2021

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Four Hundred Monitor, August 5 Midrange Dynamics Speeds Table Updates In Db2 Mirror Clusters

    5 thoughts on “Guru: Passing Parameters To Python”

    • Justin Taylor says:
      August 5, 2019 at 10:24 am

      Is there a way to get the results into the RPG? I have a very basic way, using UNIXCMD (https://github.com/Justin-E-Taylor/RPG-calling-Python). Richard Schoen has a much more comprehensive option (https://github.com/richardschoen/PythonOniLibrary).

      Viva la Python! 😀

      Reply
      • Mike says:
        August 10, 2019 at 9:17 am

        I haven’t attempted this yet, but these look like good options!

        Reply
    • Roger Nassar says:
      August 6, 2019 at 8:03 am

      Thank you. How can python pass parameters to RPG/COBOL?

      Reply
      • Mike says:
        August 10, 2019 at 9:25 am

        Great question, Roger. I’ll do some research on this and write an article if I find a good solution.

        Reply
    • Kevin says:
      September 30, 2019 at 1:14 pm

      Great Article. FYI I had to change variable pathToPython from ‘python3’ to ‘/QOpenSys/pkgs/bin/python3’
      in the rpg program. For some reason OS cannot find python3 when the QSH command is executed.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 29 Issue: 44

This Issue Sponsored By

  • Maxava
  • WorksRight Software
  • RPG & DB2 Summit
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • Stacking Up IBM i And Windows In The Clouds
  • Midrange Dynamics Speeds Table Updates In Db2 Mirror Clusters
  • Guru: Passing Parameters To Python
  • Four Hundred Monitor, August 5
  • IBM i PTF Guide, Volume 21, Number 30

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Query Supervisor Gives Database Engineers New Power
  • IBM Unveils New and Improved IBM i Services
  • 3 Takeaways from the 2021 PowerTech Security Report
  • Four Hundred Monitor, April 14
  • IBM i PTF Guide, Volume 23, Number 15
  • Big Blue Unveils Spring 2021 IBM i Technology Refreshes
  • Thoroughly Modern: Innovative And Realistic Approaches To IBM i Modernization
  • Guru: Web Services, DATA-INTO and DATA-GEN, Part 2
  • Back To The Future With A New IBM i Logo
  • IBM i PTF Guide, Volume 23, Number 14

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2021 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.