Wednesday, October 7, 2015

String sequence in Jython wsadmin

 String sequence in Jython

Strings are set of characters enclosed with single quotes or double or triple quotes. String objects are similar to tuple sequence objects. String objects are immutable. You cannot change the current string content. We need to use the new string object for modify the contents.

Strings can be two variants:

  1. raw string
  2. normal string

All the help method out comes are raw strings they are parsed when it is given to print command.
wsadmin>rawstr=AdminTask.help()
wsadmin>rawstr
'WASX8001I: The AdminTask object enables the execution of available admin\n\tcommands.  AdminTask commands operate in two modes:\n\tthe default mode is one which AdminTask communicates with the WebSphere\n\tserver to accomplish its task. A local mode is also available, in which\n\tno server communication takes place. The local mode of operation is\n\tinvoked by bringing up the scripting client using the command line\n\t"-.

...
...
Use help command\n\t\t\t\t\tto get information on the target object\n\t\t\t\t\tand options.'
wsadmin>print rawstr
WASX8001I: The AdminTask object enables the execution of available admin
        commands.  AdminTask commands operate in two modes:
        the default mode is one which AdminTask communicates with the WebSphere
        server to accomplish its task. A local mode is also available, in which
        no server communication takes place. The local mode of operation is

... removed lines for conveniance 

You can say WASShell that don't parse the string, keep as it as raw string. To define raw string r'' must be prefixed for the string.
wsadmin>scriptpath='c:\scripts\test'
wsadmin>scriptpath
'c:\\scripts\test'
wsadmin>print scriptpath
c:\scripts      est
wsadmin>scriptpath=r'c:\scripts\test'
wsadmin>print scriptpath
c:\scripts\test


 Jython String functions

There are nearly 40+ string functions available in Python. Almost same works in Jython as well.
Lets experiment on simple string

wsadmin>serverid=AdminConfig.getid('/Server:server1')
wsadmin>serverid
'server1(cells/ubuntu-was8Node01Cell/nodes/ubuntu-was8Node01/servers/server1|server.xml#Server_1183121908656)'
wsadmin>s=serverid

The split() function

Strings can be split with a delimiter which can be any symbol or a character.
Let us split the string with '#' symbol. And also test the endswith() function in the for loop.

wsadmin>s.split('#')
['server1(cells/ubuntu-was8Node01Cell/nodes/ubuntu-was8Node01/servers/server1|server.xml', 'Server_1183121908656)']

wsadmin>for e in s.split('/'):
wsadmin> if e.endswith('Cell'): print e
wsadmin>
ubuntu-was8Node01Cell



String operations


Repetition with * operator
wsadmin>d='=*'
wsadmin>print d * 50
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*


Concatenation of string
wsadmin>s1="WebSphere Adminitration"
wsadmin>s2='wsadmin automation'
wsadmin>s3=s1+s2
wsadmin>print s3
WebSphere Adminitrationwsadmin automation


The strip functions, startswith, endswith, lower, upper, find functions


As you know strings are immutable you can translate when printing to stdout or screen.
s="Kishore,Wsadmin"
print s.split(",")

s1="    string strip example    "
print "s1.rstrip :", s1.rstrip()
print "lstrip :", s1.lstrip()
print "strip :", s1.strip()

print "endswith :", s.endswith('n')
print "endswith :", s.endswith('in')
print "startswith :", s.startswith('k')

print "lower case :", s.lower()
print "upper case  :", s.upper()

print "find substring :", s.find('shore')
print "replace substring  :", s.replace('Wsadmin','websphereadmin')


output

wsadmin>execfile('/home/vagrant/scripts/strfun.py')
['Kishore', 'Wsadmin']
s1.rstrip :     string strip example
lstrip : string strip example
strip : string strip example
endswith : 1
endswith : 1
startswith : 0
lower case : kishore,wsadmin
upper case  : KISHORE,WSADMIN
find substring : 2
replace substring  : Kishore,websphereadmin


General string function
x="k"
if x in s1:
        print "it is present"
else:
        print "it is not present"
x="K"
if x not in s1:
        print "it is not present"
else:
        print "it is present"
print "slicing",s1[2:6]

print "slicing",s1[2:]

print "slicing",s1[0:3],s1[:3]


o/p

wsadmin>execfile('/home/krish/jython/string.py')
string operators
cancatenation s1+s2 kishorekumar
multiplier operator s1*3 kishorekishorekishore
it is present
it is not present
slicing shor
slicing shore
slicing kis kis





1 comment:

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow