Search This Blog


Monday, August 24, 2015

WLST Programming Controls

Effective programming could be developed using right decision making in your scripts. Here I would like to share some basics of Python program controls that would be used in the WLST scripts.

if-else in WLST

This is common decision making control in every programming language same in WLST. Python based program control much simple no braces required when you construct the if condition. There is no complex confusions for string comparisons and numeric value comparisons. As you know there is no semicolons for statements. The relational operators we have to use straight English words and, or, not. 

if-elif-else in WLST : if-else ladder

Before you enter here you need to check this really required that gives optimizing your WLST script.

if-elif-else in WLST Scripting

Most of the WLST scripts that you develop for reuse with iterative process need to be in the looping structures for more time. If you write the script optimized way means you need to focus on few facts:
clean, faster, better

While loop can be used when you want to pass the iterations with numbers. We need to initialize the iteration variable and within the loop we need to increase or decrease that value to complete the loop. While loop will be entered into its block when the given expression is executed and resulted true.

Iterations in WLST Scripts

We can see the major categories of WLST iterations control flows available such as 'for loop' and 'while loop'. Before iterating important Python function for automatic number list generation is given below.

The range() function

When you are using for-loop best trick to use with range built in Python function. It will take three different optional arguments and gives you different variety of values in the range of list as output.

  • Until the given number
  • From one particular value to another
  • From one value to another with increment by value

Experimenting with range function to understand all its capabilities The range command always start from the 0 if you pass single argument that is considered as number of values you want as output.

wls:/offline> R=range(11)
wls:/offline> R
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
wls:/offline> R=range(2,10,2)
wls:/offline> R
[2, 4, 6, 8]
wls:/offline> R=range(2,10,3)
wls:/offline> R
[2, 5, 8]

While loop control

This loop control works as same as in C programming style loop. The execution of while loop should not have any control on the variable that is used for iterate the loop. You need to initialize, check the termination condition and in the while loop block it should be iterated by increase or decrease as per program needs.
WLST While loop - WLST for loop

wls:/offline> while i<10: print "jms"+str(i)+"_server"

The 'for loop' in WLST

Every scripts developer favorite iterator control is for loop. Let us dive into some of the examples with it. The example of the 'for loop' in WLST is given below
wls:/offline> slist=range(1,11)
wls:/offline> for i in slist:
...     print 'managed'+str(i)+'_server'
Now lets get our WebLogic stuff here :) Fetching only the managed servers excluding the adminserver, Here I've considered the AdminServer name can be any name but includes Admin or adm or MyADMIN etc. To fix this we can use lower() string function on the server name and check server names not contains 'adm' then it is managed server list!
wls:/offline> connect('weblogic','welcome1','t3://')
Connecting to t3:// with userid weblogic ...
Successfully connected to Admin Server 'wappAdmin' that belongs to domain 'wappdomain'.

wls:/wappdomain/serverConfig> domainRuntime()
Location changed to domainRuntime tree. This is a read-only tree with DomainMBean as the root.
For more help, use help(domainRuntime)

wls:/wappdomain/domainRuntime> cd('/ServerLifeCycleRuntimes/')
wls:/wappdomain/domainRuntime/ServerLifeCycleRuntimes> ls()
dr--   wappAdmin
dr--   wappMS1
dr--   wappMS2

wls:/wappdomain/domainRuntime/ServerLifeCycleRuntimes>  x=ls(returnMap='true')
dr--   wappAdmin
dr--   wappMS1
dr--   wappMS2

wls:/wappdomain/domainRuntime/ServerLifeCycleRuntimes> [s for s in x if 'adm' in s.lower()]
wls:/wappdomain/domainRuntime/ServerLifeCycleRuntimes> [s for s in x if 'adm' not in s.lower()]
['wappMS1', 'wappMS2']
The last output is really awesome you will save lines as well because you don't need new line for if condition.

While loop usage vs for loop

1. Takes more time to executed 2. initialization, increase/decrease iterator variable 3. Easy to construct infinite loops with intervals For loop 1. Fastest loop 2. depends on range function to iterate on numbers 3. Flexible for including single lambda conditions Will update the following soon after sample executions completed. But these control flags works as in C language.
 coming soon 
The break statement in WLST, The continue statement in WLST, The pass statement in WLST

Popular Posts