Monday, October 5, 2015

Tuples in Jython

Tuple

Hi everyone in this post I would like to workout how does the Jython tuple can be constructed and how to display their content and their operations.

WebSphere Admin must know about tuple because it is heavily used internal MBeans for storing data in them. What I have understood about Tuple data type is:

  • A tuple is a sequence of immutable Python Objects in the Jython
  • The  tuple content cannot be changed - only assignment that is why we don't see any other functions on them
  • Use parenthesis () to define
  • Here elements can be stored into a tuple with comma-separated values
  • Tuples can contain numbers, strings, nested sub-tuples, or nothing or combination that is heterogeneous types
Tuple datatype in Jython
Tuple in Jython

Let me experiment and prove that all.

# This script list how to use the tuple.

t1=(1,2,3,4)
print "integer tuple", t1

t2=(1.2,10.5,13.4)
print "float tuple", t2

t3=('app1','app2','app3')
print "string tuple", t3

print "Accessing tuple with indexes"

print "need t1 third element",t1[3]

print "second element from t2,t3", t2[2],t3[2]

print "length of t1",len(t1)


Let me execute the tuple examples script from the wsadmin command prompt.
wsadmin>execfile('/home/krish/jython/tuple.exa.py')
integer tuple (1, 2, 3, 4)
float tuple (1.2, 10.5, 13.4)
string tuple ('app1', 'app2', 'app3')
Accessing tuple with indexes
need t1 third element 4
second element from t2,t3 13.4 app3
length of t1 4

Tuple Basic Operations

Jython Tuple Basic Operations


Tuple with heterogeneous type elements

A tuple can be stored with different data type values together as well. For example we can have a tuple with the mix of integer, string, float elements together. These kind of data you might see in when you fetch data from runtime elements of WebSphere servers.

wsadmin>tmix=(10, 'Server', 23.005)
wsadmin>tmix[1]
'Server'


Tuple of tuples

A Tuple can have small tuples inside it. Accessing the tuple elements is same for all sequences that is using index values, when index points to the inside tuple it will retrives element as tuple.

wsadmin>tt=(10, (1,2,3), 45.4)
wsadmin>tt[1]
(1, 2, 3)
wsadmin>tt[1][1]
2


Tuple operators

Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.

wsadmin>tt+tmix
(10, (1, 2, 3), 45.4, 10, 'Server', 23.005)
wsadmin>tt * 3
(10, (1, 2, 3), 45.4, 10, (1, 2, 3), 45.4, 10, (1, 2, 3), 45.4)



Membership operator 'in' on Tuple


The 'in' operator is used to check the given value matches to one of the element in the tuple. This operator returns boolean values 1 for true, 0 for false.

wsadmin>tt
(10, (1, 2, 3), 45.4)
wsadmin>10 in tt
1
wsadmin>3 in tt[1]
1
wsadmin>3 in tt
0


Accessing Tuple with for loop

In Jython for loop works with Iterator where it gets each element into the iterator and if tuple has next element then moves to next iteration. You can use the iterator to do the required operations, validations required to fulfill the script logic.
wsadmin>for i in tt: print i
wsadmin>
10
(1, 2, 3)
45.4


Comparing Tuples

Sequence specific compare operator ' is ', it is simple English meaning.

wsadmin>resources=('jdbc','jms','security')
wsadmin>r=resources
wsadmin>r is resources
1
wsadmin>r in resources
0
wsadmin>r==resources
1

No comments:

Post a Comment

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow