Jython applets newbie report and help request (Thanks D-Man)!!!

Ron Stephens rdsteph at earthlink.net
Wed May 30 23:18:06 EDT 2001


I have decided to post the actual Java code created by jythonc from one of
my python scripts, in the hope some of the kind and intelligent folks on
here giving me some helpful comments. By the way, could the problem be the
use of the python "input" and " print" ?? I don't think so, since the java
code below seems to have been converted to acceptable java equivalents for
user inputs...hmmm do I need to rewrite only the actual input/output
sections in "real" java??? Or can I use a little JavaScript just to do
input/output, somehow interfaced with the Java applet code???

Shouldn't have to, correct???

Here's the .java file code, created by jythonc from my python code (pretty
cool, hey, even though it doesn't work as a web applet yet :_))))

import org.python.core.*;

public class JYesNo extends java.lang.Object {
    static String[] jpy$properties = new String[] {"python.modules.builtin",
"exceptions:org.python.core.exceptions",
"python.options.showJavaExceptions", "true"};
    static String[] jpy$packages = new String[] {"java", null};

    public static class _PyInner extends PyFunctionTable implements
PyRunnable {
        private static PyObject s$0;
        private static PyObject s$1;
        private static PyObject s$2;
        private static PyObject i$3;
        private static PyObject s$4;
        private static PyObject s$5;
        private static PyObject s$6;
        private static PyObject s$7;
        private static PyObject s$8;
        private static PyObject s$9;
        private static PyObject s$10;
        private static PyObject s$11;
        private static PyObject s$12;
        private static PyObject i$13;
        private static PyObject s$14;
        private static PyObject s$15;
        private static PyObject s$16;
        private static PyObject s$17;
        private static PyObject s$18;
        private static PyObject s$19;
        private static PyFunctionTable funcTable;
        private static PyCode c$0_get_list;
        private static PyCode c$1_get_number;
        private static PyCode c$2_lambda;
        private static PyCode c$3_main;
        private static void initConstants() {
            s$0 = Py.newString("C:\\jython-20\\JYesNo.py");
            s$1 = Py.newString(" get_list(heading, prompt) ->
list\012\012This function prompts for a list of things.  The heading is
printed on\012a line by itself, and the prompt must have a %d substitution
for the\012number of the item within the list.\012");
            s$2 = Py.newString("(enter a blank line to end the list)");
            i$3 = Py.newInteger(1);
            s$4 = Py.newString(" get_number(prompt) -> float\012\012This
function prompts for a number.  If the user enters bad input, such
as\012\"cat\" or \"3l\", it will prompt again.\012");
            s$5 = Py.newString("Yes");
            s$6 = Py.newString("No");
            s$7 = Py.newString("Enter your criteria ...");
            s$8 = Py.newString("Criterion %d: ");
            s$9 = Py.newString("Enter relative importance of criteria
(higher is more important)");
            s$10 = Py.newString("Criterion %s: ");
            s$11 = Py.newString("Enter score for each option on each
criterion");
            s$12 = Py.newString("Score for a %s decision ");
            i$13 = Py.newInteger(0);
            s$14 = Py.newString("The results are");
            s$15 = Py.newString("%5s %s");
            s$16 = Py.newString("Score");
            s$17 = Py.newString("Option");
            s$18 = Py.newString("You should decide Yes!!!");
            s$19 = Py.newString("You should decide No!!!");
            funcTable = new _PyInner();
            c$0_get_list = Py.newCode(2, new String[] {"heading", "prompt",
"ret", "i", "line"}, "C:\\jython-20\\JYesNo.py", "get_list", false, false,
funcTable, 0);
            c$1_get_number = Py.newCode(1, new String[] {"prompt", "res"},
"C:\\jython-20\\JYesNo.py", "get_number", false, false, funcTable, 1);
            c$2_lambda = Py.newCode(2, new String[] {"x", "y"},
"C:\\jython-20\\JYesNo.py", "<lambda>", false, false, funcTable, 2);
            c$3_main = Py.newCode(0, new String[] {},
"C:\\jython-20\\JYesNo.py", "main", false, false, funcTable, 3);
        }


        public PyCode getMain() {
            if (c$3_main == null) _PyInner.initConstants();
            return c$3_main;
        }

        public PyObject call_function(int index, PyFrame frame) {
            switch (index){
                case 0:
                return _PyInner.get_list$1(frame);
                case 1:
                return _PyInner.get_number$2(frame);
                case 2:
                return _PyInner.lambda$3(frame);
                case 3:
                return _PyInner.main$4(frame);
                default:
                return null;
            }
        }

        private static PyObject get_list$1(PyFrame frame) {
            /*  get_list(heading, prompt) -> list

            This function prompts for a list of things.  The heading is
printed on
            a line by itself, and the prompt must have a %d substitution for
the
            number of the item within the list.
             */
            Py.println(frame.getlocal(0));
            Py.println();
            Py.println(s$2);
            frame.setlocal(2, new PyList(new PyObject[] {}));
            frame.setlocal(3, i$3);
            while (i$3.__nonzero__()) {
                frame.setlocal(4,
frame.getglobal("raw_input").__call__(frame.getlocal(1)._mod(frame.getlocal(3))));

                if (frame.getlocal(4).__not__().__nonzero__()) {
                    break;
                }
                frame.getlocal(2).invoke("append", frame.getlocal(4));
                frame.setlocal(3, frame.getlocal(3)._add(i$3));
            }
            Py.println();
            return frame.getlocal(2);
        }

        private static PyObject get_number$2(PyFrame frame) {
            // Temporary Variables
            PyException t$0$PyException;

            // Code
            /*  get_number(prompt) -> float

            This function prompts for a number.  If the user enters bad
input, such as
            "cat" or "3l", it will prompt again.
             */
            frame.setlocal(1, frame.getglobal("None"));
            while
(frame.getlocal(1)._is(frame.getglobal("None")).__nonzero__()) {
                try {
                    frame.setlocal(1,
frame.getglobal("float").__call__(frame.getglobal("raw_input").__call__(frame.getlocal(0))));

                }
                catch (Throwable x$0) {
                    t$0$PyException = Py.setException(x$0, frame);
                    if (Py.matchException(t$0$PyException,
frame.getglobal("ValueError"))) {
                        // pass
                    }
                    else throw t$0$PyException;
                }
            }
            return frame.getlocal(1);
        }

        private static PyObject lambda$3(PyFrame frame) {
            return
frame.getglobal("cmp").__call__(frame.getlocal(0).__getitem__(i$3),
frame.getlocal(1).__getitem__(i$3)).__neg__();
        }

        private static PyObject main$4(PyFrame frame) {
            // Temporary Variables
            int t$0$int, t$1$int, t$2$int, t$3$int, t$4$int, t$5$int;
            PyObject t$0$PyObject, t$1$PyObject, t$2$PyObject, t$3$PyObject,
t$4$PyObject, t$5$PyObject, t$6$PyObject, t$7$PyObject, t$8$PyObject,
t$9$PyObject, t$10$PyObject, t$11$PyObject, t$12$PyObject;

            // Code
            frame.setglobal("__file__", s$0);
            org.python.core.imp.importOne("java", frame);
            frame.setglobal("get_list", new PyFunction(frame.f_globals, new
PyObject[] {}, c$0_get_list));
            frame.setglobal("get_number", new PyFunction(frame.f_globals,
new PyObject[] {}, c$1_get_number));
            frame.setglobal("options", new PyList(new PyObject[] {s$5,
s$6}));
            frame.setglobal("criteria",
frame.getglobal("get_list").__call__(s$7, s$8));
            frame.setglobal("rankings", new PyDictionary(new PyObject[]
{}));
            Py.println();
            Py.println(s$9);
            Py.println();
            t$0$int = 0;
            t$1$PyObject = frame.getglobal("criteria");
            while ((t$0$PyObject = t$1$PyObject.__finditem__(t$0$int++)) !=
null) {
                frame.setglobal("c", t$0$PyObject);

frame.getglobal("rankings").__setitem__(frame.getglobal("c"),
frame.getglobal("get_number").__call__(s$10._mod(frame.getglobal("c"))));
            }
            frame.setglobal("score", new PyDictionary(new PyObject[] {}));
            Py.println();
            Py.println(s$11);
            Py.println();
            t$1$int = 0;
            t$3$PyObject = frame.getglobal("options");
            while ((t$2$PyObject = t$3$PyObject.__finditem__(t$1$int++)) !=
null) {
                frame.setglobal("o", t$2$PyObject);
                Py.println();
                Py.println(s$12._mod(frame.getglobal("o")));
                Py.println();
                t$2$int = 0;
                t$5$PyObject = frame.getglobal("criteria");
                while ((t$4$PyObject = t$5$PyObject.__finditem__(t$2$int++))
!= null) {
                    frame.setglobal("c", t$4$PyObject);
                    frame.getglobal("score").__setitem__(new PyTuple(new
PyObject[] {frame.getglobal("o"), frame.getglobal("c")}),
frame.getglobal("get_number").__call__(s$10._mod(frame.getglobal("c"))));
                }
            }
            frame.setglobal("result", new PyDictionary(new PyObject[] {}));
            t$3$int = 0;
            t$7$PyObject = frame.getglobal("options");
            while ((t$6$PyObject = t$7$PyObject.__finditem__(t$3$int++)) !=
null) {
                frame.setglobal("o", t$6$PyObject);
                frame.setglobal("value", i$13);
                t$4$int = 0;
                t$9$PyObject = frame.getglobal("criteria");
                while ((t$8$PyObject = t$9$PyObject.__finditem__(t$4$int++))
!= null) {
                    frame.setglobal("c", t$8$PyObject);
                    frame.setglobal("value",
frame.getglobal("value")._add(frame.getglobal("rankings").__getitem__(frame.getglobal("c"))._mul(frame.getglobal("score").__getitem__(new
PyTuple(new PyObject[] {frame.getglobal("o"), frame.getglobal("c")})))));
                }
                frame.getglobal("result").__setitem__(frame.getglobal("o"),
frame.getglobal("value"));
            }
            frame.setglobal("results",
frame.getglobal("result").invoke("items"));
            frame.getglobal("results").invoke("sort", new
PyFunction(frame.f_globals, new PyObject[] {}, c$2_lambda));
            Py.println();
            Py.println(s$14);
            Py.println();
            Py.println(s$15._mod(new PyTuple(new PyObject[] {s$16, s$17})));

            t$5$int = 0;
            t$11$PyObject = frame.getglobal("results");
            while ((t$10$PyObject = t$11$PyObject.__finditem__(t$5$int++))
!= null) {
                t$12$PyObject = t$10$PyObject;
                frame.setglobal("option", t$12$PyObject.__getitem__(0));
                frame.setglobal("result", t$12$PyObject.__getitem__(1));
                Py.println(s$15._mod(new PyTuple(new PyObject[]
{frame.getglobal("result"), frame.getglobal("option")})));
            }
            if
(frame.getglobal("results").__getitem__(i$13)._gt(frame.getglobal("results").__getitem__(i$3)).__nonzero__())
{
                Py.println(s$18);
            }
            else {
                Py.println(s$19);
            }
            return Py.None;
        }

    }
    public static void moduleDictInit(PyObject dict) {
        dict.__setitem__("__name__", new PyString("JYesNo"));
        Py.runCode(new _PyInner().getMain(), dict, dict);
    }

    public static void main(String[] args) {
        String[] newargs = new String[args.length+1];
        newargs[0] = "JYesNo";
        System.arraycopy(args, 0, newargs, 1, args.length);
        Py.runMain("JYesNo$_PyInner", newargs, jpy$packages, jpy$properties,
"", new String[] {"JYesNo"});
    }

}




Ron Stephens wrote:

> OK, after much work and help from D-Man, I have managed to use jythonc
> to convert some of my Python scripts into .class files, .java files, and
> .jar files. This is really cool and I recommend folks try it!!!
>
> Now, the original python programs were quite simple, using only core
> python and no library modules. Also, the original python programs, when
> typed into the jython interactive shell, ran fine as jython scripts! I
> am pleased.
>
> Now, my whole purpose in this is to use them as java applets on html
> pages on my web site. I am now having trouble doing this, although I
> haven't been trying for long since i just finally got the compiled java
> classes and jar files finished, finally (hurray and thanks D Man)
>
> But my first attempt failed. I created an HTML page and entered a java
> applet using a jar file as so
>
> <HTML>
>
> <BODY>
>
> <APPLET CODE="jython-jyesno" ARCHIVE="jython-jyesno.jar">
>
> </BODY>
> </HTML>
>
> nothing happens when I view this HTML page with a web browser.
>
> I assume I have to use the jar file, rather than the class file, in
> order to include all the necessary code to run jython applets?
> Can anyone give me a pointer on what to try next?
>  D-Man?
>
> Anyone??
>
> Help greatly apprecizted...I am a complete and utter clueless newbie,
> but this is really  neat to have written a few python programs that
> though simple are meaningful to me, and then successfully compiling them
> into java files!
>
> If I get them running as java applets on web pages I may explode form
> happiness ;-)))
>
> Python is great!
>
> comp.lang.python folks are even greater!!! ;-)))))))
>
> Thanks for all your past help on python programs and also jython
>
> Ron Stephens
> rdsteph at earthlink.net






More information about the Python-list mailing list