Master the Mainframe '20 | Level 2.9: ZOAU2: Power Through Python🐍

Master the Mainframe '20 | Level 2.9: ZOAU2: Power Through Python🐍

Master the Mainframe 2020

In 2020, IBM has established a new approach to the 2020 MTM (Master the Mainframe) contest. This year we are using Visual Studio Code (for the first time) to establish a connection with z/OS and work with data sets and jobs, using Zowe™ and IBM Z Open Editor extensions. Talk about a user experience upgrade😎.

image.png

Picture 9.1: Viewing the contents of a data set using IBM Z Open Editor in VS Code.

In this post, I'd like to share my impression of this challenge. In the challenge, the goal is to build an app to validate credit card data using Z Open Automation Utilities and Python. The requirement is to read an input file (MTM2020.PUBLIC.CUST16 is the input data set name in the MTM2020 system) and print a report with a list of records of the invalid Credit card number (aka Payment Card Number).

💡: ZOAU (Z Open Automation Utilities) supports shell scripts, Python and Node.js. Which lets you perform many tasks on z/OS without needing to get into JCL at all 👏.

The input data set conforms to Track 1 Format B of ISO/IEC 7813 format 👉(en.wikipedia.org/wiki/ISO/IEC_7813).

image.png Picture 9.2: A snap from Wikipedia that shows the structure of Track 1 Format B. I've used this structure to come up with the record layout of the input file for COBOL.

Before this challenge, I've not heard of the Luhn algorithm. It is pretty interesting and simple. The Luhn Algorithm is an efficient method of checking if a credit card number is valid, locally, without needing to have the bank or financial institute process it. This way, cards can be checked directly on a web page for mistakes in typing or copying digits. More information about the Luhn algorithm can be found 👉 here. Zowe™ and Python🐍:

ZOAU supports shell scripts, Python and Node.js. The commands available through Z Open Automation Utilities in Python can be found 👉 here. In Layman's terms, IBM has developed a bridge between z/OS and Python by making these commands available for us to use within the Python scripts. We use 👉 zoautil_py.Datasets module extensively in this challenge. Modules in Python will have a set of functions, classes, and variables defined in them. zoautil_py.Datasets module has got several functions like,

  • create(name, type, size, format, class_name, length, offset) - Used to create a data set in z/OS
  • delete(dataset) - Used to delete a data set in z/OS
  • copy(source, target) - Used to copy the source data set into destination data set

You just have to refer to the corresponding function and pass necessary arguments in your Python script to perform the task it is intended to do.

Note: The documentation on creating a sequential data set or writing a report is not clear, see here for a good example of creating a data set as well as writing a report using ZOAU.

Building the logic using Python🐍:

Note: To access python scripts from your home directory, /z/zxxxxx, you should be signed up to 👉 MTM2020 and should've finished the challenges till Level 2.4.

There's already a python script (cc_check.py) available under your home directory (/z/zxxxxx) in Unix System Services (abbreviated to USS). There are LOTS of comments in the script to help you understand what the code is doing.

The python script (cc_check.py) was supposed to be a program that finds invalid credit card numbers in a big list, but it is incomplete. In its current state, all it does is check to see if a portion of the credit card number is even or odd, which is nice, but not really all that helpful. In this challenge, I'm supposed to edit this program so that instead of locating the odd numbers, it implements the Luhn algorithm to find the six invalid entries.

Note: We are supposed to use the sample code in luhn.py provided to you to perform the checking logic.

💡: A comment in Python starts with the hash character, #

I've posted a screenshot of the contents of cc_check.py below. I'll do my best here to make you understand what should be done to complete Level 2.9.

image.png Picture 9.3: A screenshot of the original cc_check.py code that is to be modified.

Lines 1 thru 7: The necessary ZOAU libraries for Python are imported so that you can use them in the script.

Lines 9 thru 16: The input data set, MTM2020.PUBLIC.CUST16 is being read into a variable called cc_contents. Some functions (like exists, delete) in the Datasets module are being used to check if the output data set is already existing in the Mainframe. If yes, then it is deleted.

Lines 17 and 18: Read👀 those comments in the 2 lines as they help you figure out what needs to be done.

Lines 21 thru 28: def keyword in Python is used to define functions. You all know that function is a group of statements that are intended to perform a specific task. This function's name is is_even and this function receives one argument from the caller. The purpose of this function is to check if the number passed as an argument by the caller, is even or odd. If it is even, the function returns 'True' to the caller. Else, it returns 'False'. Your task is to edit this script so that instead of locating the even numbers, it implements the Luhn algorithm to find the six invalid entries in the input file. You may use the sample code in the luhn.py file to perform the checking logic.

Lines 30 thru 35: Each record in the data set is stored as an element in a List named cc_list. Like how we read records one by one in COBOL, a for loop in Python is being used to read the elements of cc_list, one by one. cc_line variable is used to hold the element in any given iteration of the for a loop. Like how we use Reference Modification in COBOL to extract a portion of the string, the slicing technique (in Line 33) is being used on the List element to extract the Payment card number. The extracted card number is then passed as an argument to the is_even function.

💡: List is one of the most frequently used datatype in Python. The elements are stored in a list within square brackets ([ ]). Lists can have any number of items in them and the elements may be of different data types. Read more about lists and how to access the elements in a list 👉 here.

Lines 38 thru 57: Pretty much self-explanatory as there are a lot of comments. Once you're ready to write the 6 invalid credit card numbers to the output file, uncomment line 57.

And, there you go! Remember to refer to the luhn.py file and implement the Luhn algorithm in the place of the is_even function.

Bonus: Pay attention, whenever you find the word hint in a comment line. Review the input file MTM2020.PUBLIC.CUST16 and understand that is a file that is converted into a list. Review list splicing and that will help. Here are more tips below:

  1. The Luhn algorithm is given to you in luhn.py, you just have to include it in your code in cc_check.py. It is a copy-paste only.
  2. The cc_check is using the is_even function. Remove it, since we will now use the Luhn algorithm.
  3. Remove all the instances of is_even from the code and use the Luhn algorithm instead.
  4. cc_digits = int(cc_line[8:16]) ---> you need to determine where the credit cards begin and end. Find that in the file "MTM2020.PUBLIC.CUST16".
  5. Read the comments 2x and comment on the last line.

ZOAU is an alternative way of interacting with tasks on z/OS through scripting instead of writing JCL's and submitting jobs.

Hope you found this helpful. Thx👍