The if-else construct in Jython
Syntax:if condition1 : true statements elif condition2 : true condition2 statements else: false statementsExample:
In your monitoring scripts you might need for the variable must be cross check across multiple servers. Assuming that you got the JVM Valules using if condition statements find which server is having more Java objects occupied.
# This script illustrates the if-else-elif usage print "Enter 3 server JVM Size in MB: " s1, s2, s3= input('3 integer values') print "Given JVM Values:", s1, s2, s3 print '#'*50 if s1 > s2 and s1 >s3: print s1, " s1 JVM server overloaded" elif s2 > s1 and s2 > s3: print s2, " s2 jvm overloaded" elif s3 > s1 and s3 > s2: print s3, " s3 JVM Overloaded" else: print "Error in input plz check" print '#'*50Output:
wsadmin>execfile('/home/vagrant/scripts/ifelse_sample.py') Enter 3 server JVM Size in MB: 3 integer values120, 300, 90 Given JVM Values: 120 300 90 ################################################## 300 s2 jvm overloaded ##################################################
The while loop in Jython
The while loop in Jython can be constructed with the three important statements: initialize conditional expression update statement either increment or decrements
# this is while loop sample i=1 while i<=10: print "server"+str(i) i+=1 else: print "completed in the else now.. "+str(i)Execution of this while_sample.py script as follows:
sadmin>execfile('/home/vagrant/scripts/while_sample.py') server1 server2 server3 server4 server5 server6 server7 server8 server9 server10 completed in the else now.. 11
While loop as infinite loop This is the common requirement for most of the scripting languages. You many need a menu that options can be reused multiple times then we can call those statements into the infinite loop. These infinite loop doesn't requires initialization, update statements. The condition will be only True value that is in Jython 1 as shown below example.
# There 3 options start, stop, status while 1: print '@'*50 print "Server Control Menu" print '@'*50 print "1. start server" print "2. Stop server" print "3. Status server" print '@'*50 mop=raw_input("Please enter your option:") if mop == '1': print "You selected start" elif mop == '2': print "You selected stop" elif mop == '3': print "You selected status" else: print "Invalid option" print "Exiting..." sys.exit()Execution of the Menu driven program in Jython :
wsadmin>execfile('/home/vagrant/scripts/while_menu.py')
While loop with infinite menu display |
The for loop in Jython
Whenever you work on list objects then use the for loop it is common in Jython. We have a special built-in Python function that is range(). This is having three variants:
- The range up to n range(n)
- The range starting, ending boundaries range(start, end)
- The range starting, ending with step opton range(start, end, step)
# This script illustrates range function and for loop r1=range(10) # 0..9 print "r1 range numbers" for i in r1: print i r2=range(2, 10) # 2..9 print "r2 range numbers" for i in r2: print i r3=range(2,10,2) print "r3 range numbers" for i in r3: print iThe execution of the above range testing for loop script saved as for_sample.py
The range() function with for loop in Jython |
To work with for loop we must have a list of elements. It actually designed for iterate on the each eliment of sequence type objects in the same way.
# This script illustrates the list operations with for loop mylist=[3,4,2,6,8] print "mylist:", mylist list2=[ x * 2 for x in mylist ] print "Comprehensive list", list2 filteredList=[ x * 3 for x in mylist if x > 4 ] print "filteredList:", filteredList nestList=[ x * 2 for x in [y+1 for y in mylist]] print "Nested List :", nestList
These for loops are very powerful because you may need some situations list of server without dmgr and web servers or vice versa. The filtering capability is really improves your programming ability to create optimistic code.
Jython for loop flexibility |
No comments:
Post a Comment