Sunday, August 24, 2014

Functions and Procedures in Jython wsadmin

Modular programming is the need in the every scripting language it will tear down the bigger problem statement into small pieces and easy to resolve. We can get the strategy in building the Jython Functions and Procedures for different tasks.



The are different ways to create the function body structure. A function is a sub program, which takes inputs as arguments, you can pass multiple arguments, same kind of data as agreements. A Function can return a value then that function call can be called in assignment statement, output statement, it can be in the expression.


  • Functions can be created to reduce the complexity in the scripts 
  • It  is easy to read and reuse them
  • Function definition became a procedure when there are no return statement in it


 

The function definition in Jython

The defining a new function starts with 'def' keyword. the function block begins with a name of the function and colon and followed by the Jython program control statement or JMX methods which are accessible on Admin Objects. At the end of the function you can have a return statement. If there is a need of validation to exit the function then we can use 'if-else' construct with different return expressions.

Function Definition in Jython




def add(x=1,y=2):  # default arguments
 z=x+y
 return z

def mul(x,y):
 return x*y

def greet():  # procedure  don't returns
 print 'Welcome to VybhavaTechnologies'

#==== main ====
a=100
b=200
print add(a,b) # function in output statement
c=add(a,b)+200 # function call in expression
print c
d=mul(a,b)  # function call in assignment
. print d
greet()

print 'Default args function:',add()
print 'Positional args:', add(y=240, x=300)

x= lambda a,b : a+b
print x(10, 30)

kb2mb=lambda kb: str(kb/1024)+'MB'


Output of the above example function in Jython as follows:

wsadmin>execfile('/home/krish/jython/functions.py')
300
500
Welcome to VybhavaTechnologies
Default args function: 3
Positional args: 540
40
2MB


Introspection in Jython functions


Using docStrings when a new function is defined. Any new user wish to modify it will give the description about how the function is constructed number of arguments and also tells how to use it. The docString can be accessible with __doc__ or func_doc function.

# This is illustrates functions

# Simple procedure definition
def cls():
        print "\n" * 50

def add(x, y):
        "This function takes two int args returns sum of them."
        z= x+y
        return z

def sub(x, y):
        "This will substract y from x and return it"
        z=x-y
        return z

def mul(x=1, y=10):
 """
 The mul function takes 2 int args and returns product of them.
 This function illustrates default values for arguments
 """
 z = x * y
 return z

#=== Main ====
cls()
a, b= input("Enter two numbers:")
result = add(a, b)
print result
result = add(a, b) + sub(a, b)
print result

if add(a,b) > 0:
        print "addition of a, b is positive"

result = mul(10)
print result, " 10 argument"

result = mul(10, 30)
print result, "10, 30 are arguments"

result = mul()
print result, " no args"

result = mul(y=40)
print result, " y arg passed"

#print z  scope of variables expires within the function only

Sample Jython Functions : functionEx.py 
Execution of functions in Jython, Lambda functions, introspection of function
Introspection of Jython docString comments sample for function definitions

Function calling in Jython


You can call the function in the following ways:

  1. Assignment statement
  2. Expression statement
  3. In the program controls
  4. print statements

In the above sample script we have function calls as following in the main program.
result = add(a, b)
print result
result = add(a, b) + sub(a, b)
print result

if add(a,b) > 0:
        print "addition of a, b is positive"



No comments:

Post a Comment

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow