Jython .py to .class confusion
Ype Kingma
ykingma at accessforall.nl
Thu May 23 16:31:34 EDT 2002
Scott,
scott wrote:
>
> I'm trying to use "jythonc" in Jython 2.1 to create a .class file from a
> .py file. I've read Chapter 9 of Bruce Eckels draft of "Thinking in
> Patterns" (http://64.78.49.204/) which has a section on this very task,
> but I am unable to grok it.
>
> It looks like I need:
> 1. an "@sig" string as the first line of my method to deal with strong /
> weak typing issues (?something like "@sig public javax.swing.???
> getPicks()" ?)
> 2. Information about Java package creation from the python code.
> Apparently this involves the --package flag of jythonc.
>
> I'm stuck here. Perhaps if I give a concrete example, someone will show
> the modifications / steps required to create a .class file from this .py
> file.
>
> Assume this file (loto3.py) is the only file on a floppy disk in a
> Windows 2000 system running Jython 2.1 on Java 1.4.0:
>
> ---------------------------------------------------
> import random
> import javax.swing as swing
>
> def getPicks():
> li = []
> i = 0
> numPicks = int(swing.JOptionPane.showInputDialog("How many picks?:
> "))
> theRange = int((swing.JOptionPane.showInputDialog("What range?: ")))
> + 1
> while i < numPicks:
> elem = random.randrange(1, theRange)
> if elem not in li:
> li.append(elem)
> i = i+1
> conv = str(li)
> return swing.JOptionPane.showMessageDialog(None, "Your #'s are: " +
> conv)
>
> getPicks()
> --------------------------------------------------
jythonc expects
class loto3(someJavaClass):
....
in the file loto3.py, ie.
import java
class loto3(java.lang.Object):
def getPicks(self):
"@sig public java.lang.String getPicks()" # java signature
....
And then in another .py file:
import loto3 # or from itsPackage import loto3
yourLoto = loto3.loto3() # iirc.
print yourLoto.getPicks()
It's in the manual at www.jython.org, but the manual is just a bit terse...
Have fun,
Ype
--
email at xs4all.nl
More information about the Python-list
mailing list