Showing posts with label Jython. Show all posts
Showing posts with label Jython. Show all posts

Thursday, July 24, 2014

Arrays and Indexers in Jython



Today's post is about Arrays and Indexers in Jython. Here below you will find a very easy to follow program, that demonstrate arrays and indexer, by implementing some simple tasks that will make you grasp the idea of those 2 features real quick. The main goal of this post, is not really teaching arrays because, come on, you probably already know "all" about them, in fact, it is more to show you how you do that in Jython, in this case, compared to all other 22 languages on future posts, which essentially, is the real aim behind this blog.

This is the second post of a dynamic language. As with IronPython's version of the program, code structure changed slightly from previous posts. Besides that, because you normally don't use Arrays per se in Python (except for numerical arrays when better performance is required), it is more practical to use Python's List object instead of Java's java.lang.ArrayList, which can definitely be used in Jython, specially when you need to inter operate with other JVM languages.
By the way, if you missed my most recent post, "New Series - Arrays and Indexers", check it out. It has more details about the following program, and a bunch of definitions for the concepts used on this, and the following, posts. Or you can check my previous posts about arrays in Boo (or Cobra or Jython later on) just to compare.

I encourage you to copy the code below and try it yourself, normally, all programs you find in this blog are source code complete, just paste it on your IDE and run it.

There is room for improvement of the code, using generics is one example, but Generics, Collections, lambdas, etc. will have their own "series" of posts.


import java  
from java.util import Random

# Console Program     
def main():     
    # Single-dimensional Array(s)     
    printtitle('Reverse Array Elements')   
  
    # Declare and Initialize Array (Python List) of Chars     
    # or letters = list('AEIOU')  
    # or letters = 5 * [' ']        
    letters = list(' ' * 5) # letters = []  
    letters[0] = 'A'        # letters.append('A')  
    letters[1] = 'E'        # letters.append('E')  
    letters[2] = 'I'        # letters.append('I')  
    letters[3] = 'O'        # letters.append('O')  
    letters[4] = 'U'        # letters.append('U')  
      
    printarray(letters)  
    inverse_letters = reversechar(letters)  
    printarray(inverse_letters)  
  
    printtitle('Sort Integer Array Elements')  
    # Declare and Initialize Array of Integers     
    numbers = [10, 8, 3, 1, 5]  
      
    printarray(numbers)     
    ordered_numbers = bubblesort(numbers)     
    printarray(ordered_numbers)  
  
    printtitle('Sort String Array Elements')    
  
    # Declare and Initialize and Array of Strings     
    names = ['Damian', 'Rogelio', 'Carlos', 'Luis', 'Daniel']  
  
    printarray(names)     
    ordered_names = bubblesort(names)     
    printarray(ordered_names)  
  
    # Multi-dimensional Array (Matrix row,column)     
    printtitle('Transpose Matrix')     
  
    matrix = [[6, 4, 24],        
              [1, -9, 8]]  
  
    printmatrix(matrix)  
    transposed_matrix = transposematrix(matrix)     
    printmatrix(transposed_matrix)    
  
    # Jagged Array (Array-of-Arrays)     
    printtitle('Upper Case Random Array & Graph Number of Elements')     
  
    ''' 
    Creating an array of string arrays using the String.Split method    
    instead of initializing it manually as follows:    
  
    $text = [     
        [ ["word1", "word2, "wordN"],     
        [ ["word1", "word2, "wordN"],     
        ... 
    ]    
      
    Text extract from: "El ingenioso hidalgo don Quijote de la Mancha"         
    '''  
    text = [  
    'Hoy es el dia mas hermoso de nuestra vida, querido Sancho;'.split(' '),      
    'los obstaculos mas grandes, nuestras propias indecisiones;'.split(' '),      
    'nuestro enemigo mas fuerte, miedo al poderoso y nosotros mismos;'.split(' '),      
    'la cosa mas facil, equivocarnos;'.split(' '),      
    'la mas destructiva, la mentira y el egoismo;'.split(' '),      
    'la peor derrota, el desaliento;'.split(' '),      
    'los defectos mas peligrosos, la soberbia y el rencor;'.split(' '),      
    'las sensaciones mas gratas, la buena conciencia...'.split(' ')  
    ]  
  
    printjaggedarray(text)     
    uppercaserandomarray(text)     
    printjaggedarray(text)     
    graphjaggedarray(text)  
  
    # Array Exceptions  
    printtitle('Common Array Exceptions')  
      
    printcommonarrayexceptions(None)     
    printcommonarrayexceptions(text)   
    a = java.util.ArrayList() 
    printcommonarrayexceptions(a)
    o = java.lang.Object
    printcommonarrayexceptions(o)
    # Accessing Class Array Elements through Indexer  
    printtitle('Alphabets')    
      
    vowels = Alphabet(5)  
    vowels[0] = 'a'  
    vowels[1] = 'e'  
    vowels[2] = 'i'  
    vowels[3] = 'o'  
    vowels[4] = 'u'  
  
    print '\nVowels = {%s}' % ','.join([vowels[0],vowels[1],vowels[2],vowels[3],vowels[4]])  
  
    en = Alphabet('abcdefghijklmnopqrstuvwxyz')     
    print 'English Alphabet = {%s}' % (str(en))  
  
    print 'Alphabet Extract en[9..19] = {%s}' % (str(Alphabet(en.slice(9, 10))))  
     
    word1 = ''.join([en[6], en[14], en[14], en[3]])  
    word2 = ''.join([en[1], en[24], en[4]])     
    word3 = ''.join([en[4], en[21], en[4], en[17], en[24], en[14], en[13], en[4]])     
    print "\n%s %s, %s!\n" % (word1, word2, word3)    
  
    raw_input()  
  
def reversechar(arr):  
    return list(reversed(arr))  
    # or   
    # return arr[::-1]  
    # or   
    # reversedarr = [], i = 0  
    # for j in range(len(arr) - 1,-1,-1):  
    #   reversedarr[i] = arr[j]  
    # return reversedarr  
    #  
  
def bubblesort(arr):  
    for i in reversed(arr):     
        for j in range(len(arr) - 1):     
            if arr[j] > arr[j + 1]:     
                swap = arr[j]     
                arr[j] = arr[j + 1]     
                arr[j + 1] = swap     
    return arr  
  
def transposematrix(m):     
    ''' Transposing a Matrix 2,3   
      
     A =  [6  4 24]T [ 6  1]   
          [1 -9  8]  [ 4 -9]   
                     [24  8]   
    '''  
    transposed = [len(m)*[0] for i in range(len(m[0]))]  
    for i in range(len(m)):     
        for j in range(len(m[0])):     
            transposed[j][i] = m[i][j]  
    return transposed    
  
def uppercaserandomarray(arr):     
    r = Random()     
    i = r.nextInt(len(arr))     
    for j in range(len(arr[i])):  
        arr[i][j] = arr[i][j].upper()  
  
def printarray(arr):  
    print '\nPrint Array Content ' + str(type(arr))[7:-2]  + '[' + str(len(arr)) + ']'  
    for i in range(len(arr)):     
        print ' array [{0:2}'.format(i) + '] = {0:2}'.format(arr[i])  
  
def printmatrix(m):     
    print '\nPrint Matrix Content ' + str(type(m))[7:-2]  + '[' + str(len(m)) + ',' + str(len(m[0])) + ']'  
    for i in range(len(m)):     
        for j in range(len(m[0])):     
            print ' array [{0:2},{1:2}] = {2:2} '.format(i, j, m[i][j])  
  
def graphjaggedarray(arr):     
    '''When using Arrays, we can use for(each) instead of for by index: 
    for s as (string) in arr:   
        for w as string in s:  
    '''   
    print '\nPrint Text Content ' + str(type(arr))[7:-2]  
    for i in range(len(arr)):     
        line = ''  
        line += 'Line{0:2}|'.format(i+1)  
        for j in range(len(arr[i])):     
            line += '{0:3}'.format('*')     
        line += '(' + str(len(arr[i])) + ')'  
        print line  
          
  
def printjaggedarray(arr):  
    print '\nPrint Jagged Array Content ' + str(type(arr))[7:-2]  
    for i in range(len(arr)):  
        line = ''  
        for j in range(len(arr[i])):        
            line += ' ' + arr[i][j]  
        if line == line.upper():  
            line += ' <-- [UPPERCASED]'  
        print line  
  
def printcommonarrayexceptions(arr):  
    try:  
        print type(arr)
        arr[100][100] = 'hola'
    except TypeError, e:  
        print '\nType Exception: \n', e
    except IndexError, e:
        print '\nIndex Exception: \n', e
    except java.lang.Exception, e:
        print '\nJava Exception: ', e
    #except e:  
    #    print '\nException:' 
    #else:  
  
def printtitle(message):      
    print ''    
    print '=' * 54     
    print message     
    print '=' * 54

class Alphabet:  
    # Array Field     
    _letters = []  
      
    # Indexer Get/Set Property  
    def __getitem__(self, idx):  
        return self._letters[idx]  
    def __setitem__(self, idx, value):  
        self._letters[idx] = str(value).upper()  
      
    # Read-Only Property  
    def get_Length(self):  
        return len(self._letters)     
    Length = property(fget=get_Length)  
  
    # Constructor  
    def __init__(self, param=None):             
        if type(param) == type(1):  
            self._letters = list(' ' * param)  
        elif type(param) == type(''):  
            self._letters = list(str(param).upper())  
        elif type(param) == type([]):  
            self._letters = param  
        else:  
            self._letters = None  
  
    # Overridden Method      
    def __str__(self):  
        return ','.join(self._letters)  
  
    # Method     
    def slice(self, start, length):  
        return self._letters[start:start+length]  
  
if __name__ == '__main__':  
    main()


The output:








VoilĂ , that's it. Next post in the following days.

Sunday, October 2, 2011

Factorial and Fibonacci in Jython



Here below a little program in Jython that implements 2 classes. There is the main class, called Fiborial (Fibo(nnacci)+(Facto)rial) that implements the Fibonacci and the Factorial algorithms in two ways, one Recursive (using recursion) and the other Imperative (using loops and states). The second class is just an instance class that does the same thing, but its there just to show the difference between static and instance classes, and finally a main function called as module level code.

You can also find 3 more little examples at the bottom. One prints out the Factorial's Series and Fibonacci's Series, the second one just shows a class that mixes both: static and instance members, and finally the third one that uses different return types for the Factorial method to compare the timing and result.

As with the previous posts, you can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you some more basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2011/01/new-series-factorial-and-fibonacci.html 

WARNING: the code that you will see below is not following python(ic) guidelines/idiomatic coding, I did it in purpose to compare python's syntax and features side by side with other programming languages... For instance, instead of using a python int or long I imported and used System.Numerics.BigInteger instead. Other examples, naming convention and so on, so bear with me!

I'm using the same Stopwatch java class that I used in the Java version of this post. I just added the .class in the python paths. http://carlosqt.blogspot.com/2011/05/stopwatch-class-for-java.html

The Fiborial Program

# Factorial and Fibonacci in Jython
import java
from java.util import Scanner
from java.math import BigInteger
from java.lang import System 
import Stopwatch

# Instance Class
# static classes are not supported in Python
class StaticFiborial:
    # Static Field
    __className = ''
    # no builtin static constructor/initializer support
    # you can initialize field at this point and even add extra code
    __className = 'Static Initializer'
    print __className
    # Static Method - Factorial Recursive  
    @staticmethod
    def factorialR(n):
        if n == 1:
            return BigInteger.ONE
        else:            
            return BigInteger.valueOf(n).multiply(StaticFiborial.factorialR(n-1))            
    # Static Method - Factorial Imperative
    @staticmethod
    def factorialI(n):
        res = BigInteger.ONE
        for i in range(n, 1, -1):            
            res = res.multiply(BigInteger.valueOf(i))
        return res
    # Static Method - Fibonacci Recursive 
    @staticmethod
    def fibonacciR(n):
        if n < 2:
            return 1
        else:
            return StaticFiborial.fibonacciR(n - 1) + StaticFiborial.fibonacciR(n - 2)
    # Static Method - Fibonacci Imperative
    @staticmethod
    def fibonacciI(n):
        pre, cur, tmp = 0, 0, 0
        pre, cur = 1, 1
        for i in range(2, n + 1):  
            tmp = cur + pre  
            pre = cur  
            cur = tmp  
        return cur 
    # Static Method - Benchmarking Algorithms 
    @staticmethod
    def benchmarkAlgorithm(algorithm, values):
        timer = Stopwatch()
        i = 0  
        testValue = 0  
        facTimeResult = BigInteger.valueOf(0)
        fibTimeResult = 0    
          
        # 'if-elif-else' Flow Control Statement    
        if algorithm == 1:  
            print '\nFactorial Imperative:'  
            # 'For in range' Loop Statement   
            for i in range(len(values)):
                testValue = values[i]  
                # Taking Time    
                timer.start()  
                facTimeResult = StaticFiborial.factorialI(testValue)  
                timer.stop()                            
                # Getting Time    
                print ' (' + str(testValue) + ') = ', timer.elapsed  
        elif algorithm == 2:  
            print '\nFactorial Recursive:'  
            # 'While' Loop Statement  
            while i < len(values):  
                testValue = values[i]  
                # Taking Time    
                timer.start()  
                facTimeResult = StaticFiborial.factorialR(testValue)  
                timer.stop()                            
                # Getting Time    
                print ' (' + str(testValue) + ') = ', timer.elapsed
                i += 1  
        elif algorithm == 3:  
            print '\nFibonacci Imperative:'   
            # 'For in List' Loop Statement               
            for item in values:
                testValue = item  
                # Taking Time  
                timer.start()  
                fibTimeResult = StaticFiborial.fibonacciI(testValue)  
                timer.stop()  
                # Getting Time  
                print ' (' + str(testValue) + ') = ', timer.elapsed                  
        elif algorithm == 4:
            print '\nFibonacci Recursive:'  
            # 'For in List' Loop Statement   
            for item in values:  
                testValue = item  
                # Taking Time  
                timer.start()  
                fibTimeResult = StaticFiborial.fibonacciR(testValue)  
                timer.stop()  
                # Getting Time                
                print ' (' + str(testValue) + ') = ', timer.elapsed
        else:  
            print 'DONG!'

# Instance Class  
class InstanceFiborial(object):
    # Instances Field  
    __className = ''
    # Instance Constructor  
    def __init__(self):  
        self.__className = 'Instance Constructor'
        print self.__className  
    # Instance Method - Factorial Recursive  
    def factorialR(self, n):
        # Calling Static Method  
        return StaticFiborial.factorialR(n)  
    # Instance Method - Factorial Imperative  
    def factorialI(self, n):  
        # Calling Static Method  
        return StaticFiborial.factorialI(n)  
    # Instance Method - Fibonacci Recursive    
    def fibonacciR(self, n):
        # Calling Static Method  
        return StaticFiborial.fibonacciR(n)  
    # Instance Method - Fibonacci Imperative  
    def fibonacciI(self, n):  
        # Calling Static Method  
        return StaticFiborial.fibonacciI(n)  


# Console Program  
def main():
    print 'Static Class'  
    # Calling Static Class and Methods  
    # No instantiation needed. Calling method directly from the class  
    print 'FacImp(5) = ', StaticFiborial.factorialI(5)
    print 'FacRec(5) = ', StaticFiborial.factorialR(5)
    print 'FibImp(11)= ', StaticFiborial.fibonacciI(11)
    print 'FibRec(11)= ', StaticFiborial.fibonacciR(11)
  
    print '\nInstance Class'  
    # Calling Instance Class and Methods  
    # Need to instantiate before using. Calling method from instantiated object  
    ff = InstanceFiborial()
    print 'FacImp(5) = ', ff.factorialI(5)
    print 'FacRec(5) = ', ff.factorialR(5)
    print 'FibImp(11)= ', ff.fibonacciI(11)
    print 'FibRec(11)= ', ff.fibonacciR(11)
  
    # Create a (Python) list of values to test    
    # From 5 to 50 by 5  
    values = []
    for i in range(5,55,5):
        values.append(i)

    # Benchmarking Fibonacci  
    # 1 = Factorial Imperative  
    StaticFiborial.benchmarkAlgorithm(1, values)  
    # 2 = Factorial Recursive  
    StaticFiborial.benchmarkAlgorithm(2, values) 
    # Benchmarking Factorial  
    # 3 = Fibonacci Imperative  
    StaticFiborial.benchmarkAlgorithm(3, values)  
    # 4 = Fibonacci Recursive  
    StaticFiborial.benchmarkAlgorithm(4, values)
    
    # Stop and exit
    sin = Scanner(System.in)    
    line = sin.nextLine()    
    sin.close()
  
if __name__ == '__main__':
    main()

And the Output is:



































Humm, looks like Fibonnaci's algorithm implemented using recursion is definitively more complex than the others 3 right? I will grab these results for this and each of the upcoming posts to prepare a comparison of time execution between all the programming languages, then we will be able to talk about the algorithm's complexity as well.

Printing the Factorial and Fibonacci Series
import java
from java.util import Scanner
from java.math import BigInteger
from java.lang import System, StringBuffer

class Fiborial:  
    # Using a StringBuffer as a list of string elements    
    @staticmethod
    def getFactorialSeries(n):
        # Create the String that will hold the list    
        series = StringBuffer()   
        # We begin by concatenating the number you want to calculate    
        # in the following format: "!# ="    
        series.append("!")  
        series.append(n)  
        series.append(" = ")
        # We iterate backwards through the elements of the series    
        for i in range(n, 0, -1):
            # and append it to the list    
            series.append(i)  
            if i > 1:   
                series.append(" * ")    
            else:     
                series.append(" = ")  
        # Get the result from the Factorial Method    
        # and append it to the end of the list    
        series.append(Fiborial.factorial(n).toString())
        # return the list as a string    
        return series.toString()  
  
    # Using a StringBuffer as a list of string elements    
    @staticmethod
    def getFibonnaciSeries(n):  
        # Create the String that will hold the list    
        series = StringBuffer()
        # We begin by concatenating the first 3 values which    
        # are always constant    
        series.append("0, 1, 1")    
        # Then we calculate the Fibonacci of each element    
        # and add append it to the list    
        for i in range(2, n+1):  
            if i < n:  
                series.append(", ")   
            else:  
                series.append(" = ")    
            series.append(Fiborial.fibonacci(i))  
        # return the list as a string    
        return series.toString()  
    @staticmethod      
    def factorial(n):
        if n == 1:
            return BigInteger.ONE
        else:
            return BigInteger.valueOf(n).multiply(Fiborial.factorial(n-1))
    @staticmethod  
    def fibonacci(n):  
        if n < 2:  
            return 1    
        else:    
            return Fiborial.fibonacci(n - 1) + Fiborial.fibonacci(n - 2)  
  
def main():
    # Printing Factorial Series    
    print ""  
    print Fiborial.getFactorialSeries(5)  
    print Fiborial.getFactorialSeries(7)  
    print Fiborial.getFactorialSeries(9)  
    print Fiborial.getFactorialSeries(11)  
    print Fiborial.getFactorialSeries(40)  
    # Printing Fibonacci Series    
    print ""  
    print Fiborial.getFibonnaciSeries(5)  
    print Fiborial.getFibonnaciSeries(7)  
    print Fiborial.getFibonnaciSeries(9)  
    print Fiborial.getFibonnaciSeries(11)  
    print Fiborial.getFibonnaciSeries(40)  
      
    sin = Scanner(System.in)    
    line = sin.nextLine()    
    sin.close()
  
if __name__ == '__main__':
    main()

And the Output is:



















Mixing Instance and Static Members in the same Class

Instance classes can contain both, instance and static members such as: fields, properties, constructors, methods, etc.

import java
from java.util import Scanner
from java.lang import System
    
# Instance Class
class Fiborial:
    # Instance Field  
    __instanceCount = 0
    # Static Field      
    __staticCount = 0    
    print "\nStatic Constructor", __staticCount
    # Instance Read-Only Property    
    # Within instance members, you can always use      
    # the "self" reference pointer to access your (instance) members.              
    def getInstanceCount(self):   
        return self.__instanceCount  
    InstanceCount = property(getInstanceCount, None, None)
    # Static Property
    # looks like it is not supported even if the code identify it as such    
    @staticmethod
    def getStaticCount():        
        return Fiborial.__staticCount        
    #StaticCount = property(getStaticCount, None, None)
    # The problem seems to be the use of: property(getStaticCount,..)
    # it requires an instance method and not a static one (Test.getStaticCount)    
    # Instance Constructor    
    def __init__(self):
        self.__instanceCount = 0    
        print "\nInstance Constructor", self.__instanceCount
    # No Static Constructor    
    #@staticmethod
    #def __init__():
    #    Fiborial.__staticCount = 0
    #    print "\nStatic Constructor", Fiborial.__staticCount
    # Instance Method
    def factorial(self, n):
        self.__instanceCount += 1   
        print "\nFactorial(" + str(n) + ")"
    # Static Method
    @staticmethod    
    def fibonacci(n):
        Fiborial.__staticCount += 1  
        print "\nFibonacci(" + str(n) + ")"

def main():
    # Calling Static Constructor and Methods    
    # No need to instantiate    
    Fiborial.fibonacci(5)  
      
    # Calling Instance Constructor and Methods    
    # Instance required    
    fib = Fiborial()    
    fib.factorial(5)  

    Fiborial.fibonacci(15)  
    fib.factorial(5)  
      
    # Calling Instance Constructor and Methods    
    # for a second object    
    fib2 = Fiborial()  
    fib2.factorial(5)  
      
    print ""  
    # Calling Static Property
    # using the static method referenced by the property
    #print "Static Count =", Fiborial.StaticCount
    print "Static Count =", Fiborial.getStaticCount()
    # Calling Instance Property of object 1 and 2    
    print "Instance 1 Count =", fib.InstanceCount
    print "Instance 2 Count =", fib2.InstanceCount
      
    sin = Scanner(System.in)    
    line = sin.nextLine()    
    sin.close()
  
if __name__ == '__main__':
    main()

And the Output is:























Factorial using int, float, java.math.BigInteger

So, it looks like integers in python can hold big integers, so using Jython int/long or java.math.BigInteger is the same so not much to say here.


import java
from java.util import Scanner
from java.math import BigInteger
from java.lang import System 
import Stopwatch

# Int/Long Factorial  
def factorial_int(n):  
    if n == 1:
        return int(1)
    else:  
        return int(n * factorial_int(n - 1))
      
# double/float Factorial
def factorial_float(n):
    if n == 1:
        return float(1.0)
    else:  
        return float(n * factorial_float(n - 1))

# BigInteger Factorial     
def factorial_bigint(n):
    if n == 1:
        return BigInteger.ONE
    else:
        return BigInteger.valueOf(n).multiply(factorial_bigint(n-1))
  
timer = Stopwatch()  
facIntResult = 0
facDblResult = 0.0  
facBigResult = BigInteger.valueOf(0)
i = 0  
      
print "\nFactorial using Int/Long"
# Benchmark Factorial using Int64    

for i in range(5,55,5):  
    timer.start()
    facIntResult = factorial_int(i)
    timer.stop()          
    print " (" + str(i) + ") =", timer.elapsed, " :", facIntResult  
      
print "\nFactorial using Float/Double"  
# Benchmark Factorial using Double  
for i in range(5,55,5):
    timer.start()  
    facDblResult = factorial_float(i)  
    timer.stop()          
    print " (" + str(i) + ") =", timer.elapsed, " :", facDblResult
      
print "\nFactorial using BigInteger"  
# Benchmark Factorial using BigInteger  
for i in range(5,55,5):  
    timer.start()
    facBigResult = factorial_bigint(i)  
    timer.stop()
    print " (" + str(i) + ") =", timer.elapsed, " :", facBigResult    

sin = Scanner(System.in)    
line = sin.nextLine()    
sin.close()

And the Output is:


Saturday, October 30, 2010

Jython - Basics by Example



Continue with the Basics by Example; today's version of the post written in Jython Enjoy!

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 


Greetings Program - Verbose
# Jython Basics  
import java
from java.util import GregorianCalendar, Calendar, Scanner
from java.lang import System
  
class Greet(object):  
    # Fields of Attributes  
    __message = ''  
    __name = ''    
    __loopMessage = 0  
    # Properties   
    def getMessage(self):   
        return self.__message  
    def setMessage(self, value):   
        self.__message = self.__capitalize(value)  
    Message = property(getMessage, setMessage)  
    def getName(self):   
        return self.__name  
    def setName(self, value):   
        self.__name = self.__capitalize(value)  
    Name = property(getName, setName)  
    def getLoopMessage(self):   
        return self.__loopMessage  
    def setLoopMessage(self, value):   
        self.__loopMessage = value  
    LoopMessage = property(getLoopMessage, setLoopMessage)  
    # Constructor or Initializer Method  
    def __init__(self):    
        self.__message = ''    
        self.__name = ''    
        self.__loopMessage = 0  
    # Overloaded Constructor    
    # No Overloaded Constructors Support in Python  
    # Method 1  
    def __capitalize(self, val):  
        # "if-then-else" statement    
        if len(val) >= 1:    
            return val.capitalize()  
        else:    
            return ""  
    # Method 2    
    def salute(self):    
    # "for" statement  
        for i in range(0, self.__loopMessage):  
            print self.__message, self.__name + '!'  
    # Overloaded Method  
    # No Overloaded Methods Support in Python. New methods instead.  
    # Method 2.1    
    def salute21(self, message, name, loopMessage):  
        # "while" statement  
        i = 0  
        while i < loopMessage:    
            print self.__capitalize(message), self.__capitalize(name) + '!'     
            i = i + 1  
    # Method 2.2  
    def salute22(self, name):    
        # "switch/case" statement is not supported      
        # so I'm using if then else if...   
        dtNow = GregorianCalendar()
        hh = dtNow.get(Calendar.HOUR_OF_DAY)
        if hh in range(6,12):      
            self.__message = "good morning,"      
        elif hh in range(12,18):      
            self.__message = "good evening,"      
        elif hh in range(18,23):      
            self.__message = "good afternoon,"      
        elif hh == 23 or hh in range(0,6):      
            self.__message = "good night,"    
        else:    
            self.__message = "huh?"    
        print self.__capitalize(self.__message), self.__capitalize(name) + '!'  
  
# Console Program  
def main():  
    # Define variable object of type Greet  
    # Instantiate Greet. Call Constructor  
    g = Greet()    
    # Call Set Properties  
    g.Message = "hello"    
    g.Name = "world"    
    g.LoopMessage = 5   
    # Call Method 2    
    g.salute()  
    # Call Method 2.1 and Get Properties    
    g.salute21(g.Message, "jython", g.LoopMessage)  
    # Call Method 2.2    
    g.salute22("carlos")    
    # Stop and exit    
    print "Press any key to exit..."    
    sin = Scanner(System.in)  
    line = sin.nextLine()  
    sin.close()  

main()

Greetings Program - Minimal
# Jython Basics  
from java.util import GregorianCalendar, Calendar, Scanner
from java.lang import System
  
class Greet(object):  
    # Fields of Attributes  
    __message = ''  
    __name = ''    
    __loopMessage = 0  
    # Properties   
    def getMessage(self):   
        return self.__message  
    def setMessage(self, value):   
        self.__message = self.__capitalize(value)  
    Message = property(getMessage, setMessage)  
    def getName(self):   
        return self.__name  
    def setName(self, value):   
        self.__name = self.__capitalize(value)  
    Name = property(getName, setName)  
    def getLoopMessage(self):   
        return self.__loopMessage  
    def setLoopMessage(self, value):   
        self.__loopMessage = value  
    LoopMessage = property(getLoopMessage, setLoopMessage)  
    # Constructor or Initializer Method  
    def __init__(self):    
        self.__message = ''    
        self.__name = ''    
        self.__loopMessage = 0  
    # Overloaded Constructor    
    # No Overloaded Constructors Support in Python  
    # Method 1  
    def __capitalize(self, val):  
        # "if-then-else" statement    
        if len(val) >= 1:    
            return val.capitalize()  
        else:    
            return ""  
    # Method 2    
    def salute(self):    
        # "for" statement  
        for i in range(0, self.__loopMessage):  
            print self.__message, self.__name + '!'  
    # Overloaded Method  
    # No Overloaded Methods Support in Python. New methods instead.  
    # Method 2.1    
    def salute21(self, message, name, loopMessage):  
        # "while" statement  
        i = 0  
        while i < loopMessage:    
            print self.__capitalize(message), self.__capitalize(name) + '!'     
            i = i + 1  
    # Method 2.2  
    def salute22(self, name):    
        # "switch/case" statement is not supported      
        # so I'm using if then else if...   
        dtNow = GregorianCalendar()
        hh = dtNow.get(Calendar.HOUR_OF_DAY)
        if hh in range(6,12):      
            self.__message = "good morning,"      
        elif hh in range(12,18):      
            self.__message = "good evening,"      
        elif hh in range(18,23):      
            self.__message = "good afternoon,"      
        elif hh == 23 or hh in range(0,6):      
            self.__message = "good night,"  
        else:    
            self.__message = "huh?"    
        print self.__capitalize(self.__message), self.__capitalize(name) + '!'  
  
# Console Program  
# Define variable object of type Greet  
# Instantiate Greet. Call Constructor  
g = Greet()    
# Call Set Properties  
g.Message = "hello"    
g.Name = "world"    
g.LoopMessage = 5   
# Call Method 2  
g.salute()  
# Call Method 2.1 and Get Properties    
g.salute21(g.Message, "jython", g.LoopMessage)  
# Call Method 2.2    
g.salute22("carlos")  
# Stop and exit    
print "Press any key to exit..."    
sin = Scanner(System.in)  
line = sin.nextLine()  
sin.close()


And the Output is:




















Private Fields and Methods in Jython

"There is limited support for class-private identifiers. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes.

Name mangling is intended to give classes an easy way to define 'private' instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger, and that's one reason why this loophole is not closed. (Buglet: derivation of a class with the same name as the base class makes use of private variables of the base class possible.)". Taken from: "http://docs.python.org/release/2.5.2/tut/node11.html#SECTION0011600000000000000000"

print 'Private Access Modifier Example:'  
class Greet(object):  
    # Fields of Attributes  
    __message = ''  
    __name = ''    
    __loopMessage = 0      
    # Constructor or Initializer Method  
    def __init__(self):    
        self.__message = ''    
        self.__name = ''    
        self.__loopMessage = 0   
    # Private Method  
    def __capitalize(self, val):  
        if len(val) >= 1:    
            return val.capitalize()  
        else:    
            return ""  
    # Public Method  
    def Salute(self):         
        for i in range(0, self.__loopMessage):
            print self.__message, self.__name + '!'
  
g = Greet()    
# publicly accessing private fields (_classname__fieldname)  
g._Greet__message = "hello"    
g._Greet__name = "world"    
g._Greet__loopMessage = 5   
# Call Public Method  
g.Salute()  
# Call Private Method (_classname__methodname)  
print g._Greet__capitalize('capitalized!')

Where to define Class Fields/Attributes in Jython

print ''  
print 'Class Attributes Example:'  
class ClassAttributes(object):  
    # You do not need to explicitly add the Fields/Attributes as shown below within the class:  
    # message = ''  
    # name = ''    
    # loopMessage = 0      
    # because they are added to the class as Fields/Attributes the first time they appear in   
    # your code. For example, here below, in the Initialize method,   
    # we have 2 fields (name and message)  
    def __init__(self):    
        self.message = '' # class field  
        self.name = ''  # class field  
    # and one more within a public method (loopMessage)  
    def Salute(self):  
        self.loopMessage = 0 # class field  
        localtest = 0   # local variable  
  
# Then, you can access each of them as you normally do:  
f = ClassAttributes()  
f.message = 'Hello'  
f.name = 'World'  
f.loopMessage = 5  
  
print f.message, f.name, f.loopMessage

Overloading Constructor and Methods in Jython

Python does not support Overloading Methods nor Constructors, instead, you can define one method with variable arguments and code the if-elif code to handle both(or multiple) cases yourself.

print ''  
print 'Overloaded-like Constructor and Method Example:'  
  
class Overloading(object):  
    # Constructor with variable arguments  
    def __init__(self, *args):  
        # if args list/sequence is not empty we use the arguments,   
        # otherwise we use the class fields
        if args:     
            self.message = args[0]  
            self.name = args[1]  
            self.loopMessage = args[2]  
        else:
            self.message = 'empty_message'  
            self.name = 'empty_name'  
            self.loopMessage = 2   
    # Method  with variable arguments  
    def Salute(self, *args):  
        # if args list/sequence is not empty we use the arguments,   
        # otherwise we use the class fields  
        if args:
            for i in range(0, args[2]):
                print args[0], args[1] + '!'  
        else:  
            for i in range(0, self.loopMessage):  
                print self.message, self.name + '!'     

# and now we use the "overloaded-like" constructor and method  
# calling constructor without parameters  
o1 = Overloading()  
# calling method without parameters  
o1.Salute()  
# calling method with parameters  
o1.Salute('Hello', 'Jython', 3)  
# calling constructor with with parameters  
o2 = Overloading('Hello', 'Carlos', 2)  
# calling method without parameters  
o2.Salute()


And the Output is:





Saturday, July 3, 2010

OO Hello World - Jython



Let’s have a look at the hello world program in Jython which is the Java implementation of the Python programming language for the JVM runtime.

The code is identical to the previous post of IronPython. This is because at the end, both are written in the Python programming language. Nevertheless, I decided to create separate posts for those developers looking for their preferred platform and also because of the “Jython Info” section, which of course differs between the two implementations. In the future, if I post about IronPython or Jython I will be using their respective libraries and frameworks, so it makes sense to differentiate.


By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html
where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.

class Greet(object):   
 name = ''  
 def __init__(self, name):  
  self.name = name.capitalize()  
 def salute(self):  
  print 'Hello', name, '!'  
  
# Greet the world!  
g = Greet('world')  
g.salute()

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
import java

class Greet(object): 
 name = ''
 def __init__(self, name):
  self.name = name.capitalize()
 def salute(self):
  print 'Hello', self.name, '!'

# Greet the world!
def main():
 g = Greet('world')
 g.salute()
 
main()

The Program Output:









Jython Info:
“Jython is an implementation of the Python programming language which is designed to run on the Java(tm) Platform. It consists of a compiler to compile Python source code down to Java bytecodes which can run directly on a JVM, a set of support libraries which are used by the compiled Java bytecodes, and extra support to make it trivial to use Java packages from within JPython. JPython has been renamed and superseded by Jython.” Taken from: (http://wiki.python.org/jython/JythonFaq/GeneralInfo)

Appeared:
1997-1999
Current Version:
Developed by:
Jim Hugunin > Barry Warsaw > Samuele Pedroni > Brian Zimmer > Frank Wierzbicki, Ted Leung
Creator:
Jim Hugunin
Influenced by:
Python (Guido van Rossum)
Predecessor Language
Predecessor Appeared
Predecessor Creator
Runtime Target:
JVM
Latest Framework Target:
JDK 6
Mono Target:
No
Allows Unmanaged Code:
Yes (using native Python/C libraries)
Source Code Extension:
“.py”
Keywords:
31
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Yes
Standard:
No
Latest IDE Support:
NetBeans 6.9
Eclipse
IntelliJ IDEA
Language Reference:
Extra Info: