954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Taking Swing without cafeine with Jython

0
By Tony Veijalainen on Apr 10th, 2011 2:17 am

Here is experiment of how VM and Swing UI manage the sort test posted previously, instead of CPython and Tkinter. The timing is not so sweet, but at least no Java required!

K:\jython2.5.2>java -jar jython.jar swing_test.py
256000 words generated in 10.93 s
Sorted in 1.68 s
List prepared in 22.71 s
import random
import string
import time

import java.lang as lang
import javax.swing as swing
import java.awt as awt

def exit(event):
    lang.System.exit(0)

t0 = time.clock()
word_max = 256000
names = [''.join(random.choice(string.ascii_uppercase)
                 for count in range(6))
         for word_count in range(word_max)]
t0 -= time.clock()
print('%i words generated in %.2f s' % (word_max, -t0))

t0 = time.clock()
names.sort()
t0 -= time.clock()
print('Sorted in %.2f s' % -t0)

t0 = time.clock()
win = swing.JFrame("Sort names", windowClosing=exit)
win.contentPane.layout = awt.FlowLayout()
namelist = awt.Choice()
for name in names:
    namelist.addItem(name)

win.add(namelist)
win.pack()
win.size=(200, 200)
win.show()
t0 -= time.clock()
print('List prepared in %.2f s' % -t0)

Speedier list by JList.

K:\jython2.5.2>java -jar jython.jar jlist_ex.py
256000 words generated in 9.84 s
Sorted in 1.48 s
Time taken for list: 1.37 s
import random
import string
import time

import javax.swing as swing
import java.awt as awt

class JListWindow(object):
  def __init__(self, title, data):
    t0 = time.clock()
    frame = swing.JFrame(title)
    frame.setLayout(awt.FlowLayout())
    frame.setDefaultCloseOperation(swing.WindowConstants.EXIT_ON_CLOSE)

    spane = swing.JScrollPane()
    spane.getViewport().setView(swing.JList(data))
    self.label = swing.JLabel('Time to build list')
    frame.add(self.label)
    frame.add(spane)
    frame.pack()
    frame.setVisible(True)

    t0 -= time.clock()
    self.label.text = 'Time taken for list: %.2f s' % -t0
    print(self.label.text)


if __name__ == '__main__':
    t0 = time.clock()
    word_max = 256000
    names = [''.join(random.choice(string.ascii_uppercase)
                     for count in range(6))
             for word_count in range(word_max)]
    t0 -= time.clock()
    print('%i words generated in %.2f s' % (word_max, -t0))

    t0 = time.clock()
    names.sort()
    t0 -= time.clock()
    print('Sorted in %.2f s' % -t0)

    JListWindow("Jython Sorted List", names)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Making object based on JFrame:

import random
import string
import time

import javax.swing as swing
import java.awt as awt


def random_words(word_max, length):
    return [''.join(random.choice(string.ascii_uppercase)
                     for count in range(length))
             for word_count in range(word_max)]

class WordWindow(swing.JFrame):
    def __init__(self, length=6,  word_max=256000):
        super(swing.JFrame, self).__init__()
        self.contentPane.layout = awt.FlowLayout(  )
        self.setDefaultCloseOperation(swing.WindowConstants.EXIT_ON_CLOSE)
        self.title = 'Testing Swing and Jython'
        self.label = swing.JLabel()
        self.add(self.label)
        self.pack()
        self.show()

        t0 = time.clock()
        self.names = random_words(word_max, length)
        t0 -= time.clock()
        self.label.text = '%i words generated in %.2f s. ' % (word_max, -t0)
        self.pack()

        t0 = time.clock()
        self.names.sort()
        t0 -= time.clock()
        self.label.text += ' Sorting %.2f s.   ' % -t0
        self.pack()

        t0 = time.clock()
        spane = swing.JScrollPane()
        spane.getViewport().setView(swing.JList(self.names))
        t0 -= time.clock()
        self.label.text += 'Build: %.2f s. ' % -t0

        self.add(spane)
        self.pack()

if __name__ == '__main__':
    my_swing = WordWindow()
    print(my_swing.label.text)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Great Posting Friends thanks for information.

kashmirhouse
Newbie Poster
2 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

You are free to upvote and even to give reputation. ;)

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: