Beginning Python Scripting (Jython for IBM WebSphere Application Server 7.x)
Last Updated on August 17, 2021 by Hammad Rauf
Jython is an implementation of the Python language written in Java It was implemented in Java programming language, so that is how it gets its name (J-ython). Jython language is used by IBM to provide an alternate way of administering IBM WebSphere using Jython scripts. It is alternate to the method of administering WebSphere via the Deployment Manager Console (A browser based graphical administration application).
Advantage of using Jython/Python is that it is a scripted programming language, so it can be useful when creating automated solutions to common administration tasks.
In this blog I will discuss some very basic scripting tips for the Jython/Python language. This discussion is general enough to apply to both Jython and Python.
In this blog I will talk about the following:
- Indentation Based Programming
- User Defined Functions
- Modules (Libraries)
- Standard Modules
- Sample Code
- Further Reading
Indentation Based Programming
In the context of typed or printed text “indent” means leaving a space to the left or right of printed text. In a programming language scripting context it refers to the space left on the left hand side (assuming a left-to-right English like programming language) of individual lines of instructions (commands).
Historically, some programming languages, like COBOL language, had tight checking for Indentation that allowed certain commands to begin at a certain column number in the text editor. It was required by the old compilers and it improved readability of the code when printed on a 120 column line-printer. With advent of Structured Programming paradigm, positional commands were replaced when programmers were allowed to type any where on the lines in a text editor. Structured Programming Languages supported readability as an option using TABs and spaces within the code. In languages like Pascal, BASIC and C, code-blocks (Begin-End keywords or Curly Braces) also helped in code organization and readability. In addition to help in code organization code-blocks also serve as a small program fragment (Sub routine, method or function).
Python uses indentation, line-breaks and colon(:) character to identify code-blocks and sub routines. In my first program I used a mix of TAB and spaces to do indentation and that was a source of many errors. I had to delete all the TAB characters and replace them with spaces. I love this feature since it automatically improves code readability. It does somewhat restrict the free flowing of commands anywhere in the text-editor, but the good side is that it forces the programmer to follow a “Good-Practice” and use indentation as a requirement rather then an option.
User Defined Functions
Code can be organized into smaller pieces using Functions. Python uses the keyword Def to define a function. Def is followed by any valid function name followed by a colon (:) character. In the sample Table 2 (support.py) below 2 functions are defined with parameters. Notice that unlike C language there is no need of curly braces to indicate the logical code blocks that are made apparent by the use of white-space and new-line characters.
Modules (Libraries)
Modules are another feature for organizing code. Also some pre-written modules are made available by Jython/Python creators to provide pre-coded functionality.
A Module is a file (either pre-compiled to intermediate code or in raw source code format) containing python code. I can write one myself, or use some provided by others. Usually a module will contain a group of related functions. For example there is a pre-packaged Module called “sys” that provides functions related to Operating System environment where Python/Jython is executing. In the sample Table 2 (support.py) below one such module has been written, just to demonstrate what a module can contain.
To use a module, “import” keyword is used as in this sample below.
Standard Modules
Jython does not provide all the standard modules that may be available for Python. But the most commonly used modules should be available. You can check the modules folder of your Jython installation to find out which modules are available as a standard.
Sample Code
import sys, support if ( len(sys.argv) >= 2 ) : print("Arguments - %s" % str(sys.argv)) support.print_func("Hammad") support.print_func2("Me") |
Table 1: File “runme.py” |
def print_func( par ): print("Hello : %s" % par) return def print_func2( par ): print("The number is : %f" % 54.75); print("Bye Bye : %s" % par) return |
Table 2: File “support.py” |
C:\Python>python runme.py test 1 2 3 Arguments - ['runme.py', 'test', '1', '2', '3'] Hello : Hammad The number is : 54.750000 Bye Bye : Me |
Table 3: Execution session output (In Windows/DOS) |
Further Reading
- http://www.ibm.com/developerworks/java/tutorials/j-jython1/j-jython1.html – Date accessed: April 7, 2015 – A nice introductory tutorial on IBM Jython programming.
- https://www.python.org/ – Date accessed: April 7, 2015, This link is the source for downloading the interpreter and some good documentation.
- http://www.tutorialspoint.com/python/python_command_line_arguments.htm – Date accessed: April 7, 2015, This link describes in detail how to work with command line arguments in Python.
- http://www.jython.org/archive/22/userfaq.html#what-parts-of-the-python-library-are-supported – Date accessed: April 8, 2015, This link provides a breif discussion on what Modules are supported by Jython implementations.