Tuesday, October 6, 2015

Jython List in wsadmin

A list can hold following type of data
  • variables
  • numbers
  • strings 

List varieties on wsadmin shell


Let us have different data into the list and then try to access the elements using index values.
wsadmin>varlist=[100,'Raghav', 99.9,'M']
wsadmin>print varlist
[100, 'Raghav', 99.9, 'M']
wsadmin>print varlist[2]
99.9
wsadmin>print varlist[1]
Raghav



Number List in Jython


Lets experiment with different numbers on Jython shell. When I entered big number then Jython shell throwing Overflow error. To fix this we need to append 'L' this is only for assigning the values of List elements.
wsadmin>numlist=[9000,9999999999, 8988,0]
Traceback (innermost last):
  (no code object) at line 0
OverflowError: integer literal too large
wsadmin>numlist=[9000,9999999999L, 8980, 0]
wsadmin>print numlist[1]
9999999999
wsadmin>print numlist[3]
0


String Lists


String variable also works in similar to other data types. Here we are considering the string as one element. Lets do experiment with this...

wsadmin>strlist=['Vybhava','Technologies','wsadmin','Jython Scripting']
wsadmin>print strlist
['Vybhava', 'Technologies', 'wsadmin', 'Jython Scripting']
wsadmin>print strlist[3]
Jython Scripting



Sequence operators on List


Concatenation of multiple lists with + operator

wsadmin>newlist=strlist + numlist
wsadmin>print newlist
['Vybhava', 'Technologies', 'wsadmin', 'Jython Scripting', 9000, 9999999999L, 8980, 0]


Multiplying with * operator.

wsadmin>strlist * 2
['Vybhava', 'Technologies', 'wsadmin', 'Jython Scripting', 'Vybhava', 'Technologies', 'wsadmin', 'Jython Scripting']



Membership operation on List with 'in'


The 'in' operator is simple English word that help you to write conditional statement which will check the given element is member of list.

wsadmin>'wsadmin' in strlist
1
wsadmin>'python' in strlist
0



Slice the list

You can extract the required elements from the list using square braces [] with range separator as colon (:). The format of slice is [start:end] if you are not mention start value then it will fetches from the begin of the list. Similarly for end value is blank means it is going to slice up to the end of the list.


wsadmin>strlist=['server1','server2','web1','web2']
wsadmin>print strlist[1:2]
['server2']
wsadmin>print strlist[1:3]
['server2', 'web1']
wsadmin>print strlist[:2]
['server1', 'server2']
wsadmin>print strlist[2:]
['web1', 'web2']




List methods

The python dir() command will gives you the method/attribute list of any object. Here we got 9 list methods, lets experiment on each and try to understand the usage.
wsadmin>emptylist=[]
wsadmin>print emptylist
[]

wsadmin>dir(emptylist)
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

wsadmin>emptylist.append('Praveen')
wsadmin>emptylist

wsadmin>emptylist.insert(1,'Sudheer')
wsadmin>emptylist
['Preetham', 'Sudheer', 'Praveen']

wsadmin>revlist=emptylist
wsadmin>revlist.reverse()
wsadmin>emptylist
['Praveen', 'Sudheer', 'Preetham']
wsadmin>revlist
['Praveen', 'Sudheer', 'Preetham']

wsadmin>r=emptylist[:]
wsadmin>r.reverse()
wsadmin>r
['Preetham', 'Sudheer', 'Praveen']
wsadmin>emptylist
['Praveen', 'Sudheer', 'Preetham']
wsadmin>emptylist.extend('Pavan')
wsadmin>emptylist
['Praveen', 'Sudheer', 'Preetham', 'P', 'a', 'v', 'a', 'n']
wsadmin>emptylist.pop()
'n'

wsadmin>emptylist.remove('P')
wsadmin>emptylist
['Praveen', 'Sudheer', 'Preetham', 'a', 'v', 'a']

wsadmin>emptylist.append('Preetham')
wsadmin>emptylist.append('Preetham')
wsadmin>emptylist
['Praveen', 'Sudheer', 'Preetham', 'a', 'v', 'a', 'Preetham'  , 'Preetham']
wsadmin>emptylist.count('Preetham')
3


wsadmin>emptylist.sort()
wsadmin>emptylist
['Praveen', 'Preetham', 'Preetham', 'Preetham', 'Sudheer', '  a', 'a', 'v']





Sequence functions on list objects

You could use sequence functions on tuple, list, string objects. Here I am going to show you on the list how it works:

wsadmin>numlist
[129, 22, 3]
wsadmin>min(numlist)
3
wsadmin>max(numlist)
129
wsadmin>cmp(r,emptylist)
1
wsadmin>cmp(numlist,emptylist)
-1
wsadmin>t=(10, 4, 5, 2)
wsadmin>t2l=list(t)
wsadmin>t2l
[10, 4, 5, 2]
wsadmin>t2l.append(100)
wsadmin>t.append(55)  ############### Tuples are immutable they don't allow us to add elements!!
WASX7015E: Exception running command: "t.append(55)"; except  ion information:
com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "", line 1, in ?
AttributeError: 'tuple' object has no attribute 'append'



No comments:

Post a Comment

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow