Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Jython & Swing Building a UI in a high-level language Agenda Overview of Jython Using Swing in Jython Jython and Eclipse HW 1 Python A very high-level language “Pseudo-code that runs” Tutorial and documentation at: http://www.python.org Jython Implementation of Python in Java http://www.jython.org Ease of Python, plus access to Java class libraries Overview of Jython Interpreted Dynamic (run-time) strong typing – No need to declare variables Modules, classes, functions Built-in types: tuples, lists, dictionaries Jython Syntax Whitespace is used to delimit blocks for i in range(5): if i < 2: print i print “Done” Jython Conditional Logic if expression: block elif expression: block else: block Jython boolean logic Generally…. Non-zero is true, 0 is false Objects are true, None* is false Empty lists are false use: and, or, not * None is the equivalent of Null Jython Loops while expression: block else: block # exits once on normal # loop end Note: pass, break, continue Jython Loops For variable in sequence: block else: block Jython Loops for x in (‘a’, 5, [1, 2]): print x for x in range(10): print x # prints 0, 1, 2, …, 9 for key, value in dict.items(): print key, “ : ” , value Jython List Comprehensions [expression for value in sequence] newList = [x + 2 for x in (1, 2, 3)] Jython Lists Heterogeneous, mutable l = [1, ‘test’, [9, 10], 5] len(l) #4 l[1] = ‘foo’ # [1, ‘foo’, [9, 10], 5] l.remove(5) # [1, ‘foo’, [9, 10]] l.append(2) # [1, ‘foo’, [9, 10], 2] newList = l[2:4] # [[9, 10], 2] Jython Strings s1 = ‘This is a string.’ s2 = “So is this.” s3 = s1 + ‘ ‘ + s2 s2.split() # [‘So’, ‘is’, ‘this.’] s2.find(‘is’) #3 Jython Tuples Heterogeneous, immutable t = (‘example’, 1, [1, 2]) len(t) #3 t[1] #1 t[1] = 3 # TypeError Jython Dictionaries (Key, value) pairs, similar to hash tables dict = {‘one’: 1, 2 : ‘two’ } dict.keys() # [‘one’, 2] dict.values() # [1, ‘two’] dict[3] = 4 dict.items() # [(‘one’, 1), # (2, ‘two’), (3, 4)] del dict[3] Jython Exception Handling try: block except: / except exception-type: block else: block # if no exceptions raised Jython Exception Handling try: block finally: block # executed even if exception Jython lambda functions f = lambda x, y: x + y + 2 f(1, 2) #5 Jython Functions def FuncName(args): block def AddNumbers(a , b): return a + b Jython Functions Keyword arguments and default values def AddSome(a, b=9, c=10): return a + b + c AddSome(1, 2, 3) AddSome(1, 2) AddSome(1) #6 # 13 # 20 Jython Classes class MyClass: def __init__(self, aValue = 5): self.aField = aValue def addToField(self, aNumber) self.aField += aNumber def printField(self): print self.aField inst1 = MyClass() inst2 = MyClass(10) inst1.aField = 6 inst1.addToField(1) inst1.printField() MyClass.printField(inst2) #7 # 10 Jython Classes: Inheriting class MyNewClass(MyClass): def __init__(self): MyClass.__init__(self, 8) inst = MyNewClass() inst.aField #8 Jython: Prototype-Instance inst = MyClass() inst.b = ‘test’ inst.b # test Jython Modules Basic unit for managing namespace Usually a file full of code (.class, .jar, .py) import javax.swing import javax.swing as swing from javax.swing import JFrame from javax.swing import * Swing Essentially a library of classes for building GUIs Part of the Java Foundation Classes Online tutorial: http://java.sun.com/docs/books/tutorial/uiswing/ Useful Swing Classes JFrame (essentially a window) JLabel JButton JTextField JTextArea JMenuBar JScrollPane JPanel JPasswordField JTable Jython and Swing Can work with Java classes from within Jython import javax.swing as swing frame = swing.JFrame(‘Hello World Swing’) frame.setDefaultCloseOperation( swing.JFrame.EXIT_ON_CLOSE) label = swing.JLabel(“Hello World”) frame.getContentPane().add(label) frame.pack() frame.setVisible(1) Jython and JavaBean Properties Replace object.getFoo() and object.setFoo(newFoo) with: object.foo object.foo = newFoo Can also assign in constructor object = MyClass(foo = newFoo) Jython and JavaBean Properties Code becomes: import javax.swing as swing frame = swing.JFrame(‘Hello World Swing’, defaultCloseOperation = swing.JFrame.EXIT_ON_CLOSE) label = swing.JLabel(“Hello World”) frame.contentPane.add(label) frame.pack() frame.visible = 1 Jython, Swing, and Listeners Jython also handles Listener wrapping for you Instead of: int count = 1; JButton button = new JButton(“Increment”); button.addActionListener( new ActionListener{ public void actionPerformed(ActionEvent e) { count++; }) Use: count = 1 def Increment(event): count += 1 button = swing.JButton(“Increment”, actionPerformed = Increment) Quick Jython & Java notes Jython classes can subclass Java classes (and interfaces) You can embed Jython in Java You can compile Jython to Java Type conversion can be a little subtle http://www.jython.org/docs/usejava.html Learning More Python and Jython websites Jython for Java Programmers – $34 on Amazon Jython Essentials – $17 on Amazon HW 1 Create a simple email client - Uses POP or IMAP to read Inbox - Uses SMTP to send email - UI to check mail, display Inbox - UI to read message - UI to send new mail, reply, forward Due Sept. 9 (next Thursday) Useful Jython libraries poplib – implements POP3 smtplib – implements SMTP imaplib – implements IMAP4 rfc822 – parsing email message (possibly) poplib import poplib M = poplib.POP3(‘gvu1.cc.gatech.edu') M.user( ‘jpierce’) M.pass_(‘mypassword’) numMessages = len(M.list()[1]) for i in range(numMessages): for j in M.retr(i+1)[1]: print j smtplib import smtplib server = smtplib.SMTP(‘gvu1.cc.gatech.edu’) server.sendmail(fromaddr, toaddrs, msg) server.quit() Eclipse and Jython Eclipse - http://www.eclipse.org/ Jython - Red Robin Jython plug-in - http://home.tiscali.be/redrobin/jython/ Next Time Interaction Models Formal Informal