// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import org.python.util.PythonInterpreter;

public class EmbeddedJython {
    public static class Data {
        public int iv;
        public String sv;
        public Data(int iv, String sv) {
            this.iv = iv;
            this.sv = sv;
        }
        public void m() {
            iv++;
            sv += "X";
        }
        @Override
        public String toString() {
            return String.format("(%d,%s)", iv, sv);
        }
    }
	public static void main(String[] args) {
		PythonInterpreter i = new PythonInterpreter();
		i.set("n", 3);
		i.set("o",  new Data(123, "ABC"));
		i.exec("for i in range(n):\r\n" +
               "    print(\"Hi from Python!\")\r\n" +
               "o.iv = o.iv + 1\r\n" +
               "o.sv = o.sv + 'X'\r\n" +
               "o.m()\r\n");
		Data o = (Data)i.get("o").__tojava__(Data.class);
		System.out.println(o);
		i.close();
	}

}
