modifying source at runtime - jython case

J

Jan Gregor

Hello folks

I want to apply changes in my source code without stopping jython
and JVM. Preferable are modifications directly to instances of
classes. My application is a desktop app using swing library.

Python solutions also interest me.

Solution similiar to "lisp way" is ideal.


Thanks for response,
Jan
 
A

Alan Kennedy

[Jan Gregor]
I want to apply changes in my source code without stopping jython
and JVM. Preferable are modifications directly to instances of
classes. My application is a desktop app using swing library.

Python solutions also interest me.

Solution similiar to "lisp way" is ideal.

OK, I'll play 20 questions with you.

How close is the following to what you're thinking?

begin SelfMod.java -------------------------------------------
//************************************************************
import org.python.util.PythonInterpreter;
import org.python.core.*;

class SelfMod
{

static String my_class_source =
"class MyJyClass:\n" +
" def hello(self):\n" +
" print 'Hello World!'\n";

static String create_instance = "my_instance = MyJyClass()\n";

static String invoke_hello = "my_instance.hello()";

static String overwrite_meth =
"def goodbye():\n"+
" print 'Goodbye world!'\n" +
"\n" +
"my_instance.hello = goodbye\n";

public static void main ( String args[] )
{
PythonInterpreter interp = new PythonInterpreter();
interp.exec(my_class_source);
interp.exec(create_instance);
interp.exec(invoke_hello);
interp.exec(overwrite_meth);
interp.exec(invoke_hello);
}

}
//************************************************************
end SelfMod.java ---------------------------------------------

need-to-complete-my-coursework-for-telepathy-101-ly y'rs
 
J

Jan Gregor

[Jan Gregor]
I want to apply changes in my source code without stopping jython
and JVM. Preferable are modifications directly to instances of
classes. My application is a desktop app using swing library.

Python solutions also interest me.

Solution similiar to "lisp way" is ideal.

OK, I'll play 20 questions with you.

How close is the following to what you're thinking?

I see.

Following try showed me that instances aren't affected by
modification in class definition.
but this helped
x.test = a().test OR x.test = y.test

The only thing left is how to initiate modification, it's a
swing application and i think interp.exec don't stop until
it's done. Maybe somehow call it from jython itself - but need
to pass PythonInterpreter instance.


Thanks.


class a:
def test(self):
print 'first'

x= a()

class a:
def test(self):
print 'second'

y= a()

x.test()
y.test()
begin SelfMod.java -------------------------------------------
//************************************************************
import org.python.util.PythonInterpreter;
import org.python.core.*;

class SelfMod
{

static String my_class_source =
"class MyJyClass:\n" +
" def hello(self):\n" +
" print 'Hello World!'\n";

static String create_instance = "my_instance = MyJyClass()\n";

static String invoke_hello = "my_instance.hello()";

static String overwrite_meth =
"def goodbye():\n"+
" print 'Goodbye world!'\n" +
"\n" +
"my_instance.hello = goodbye\n";

public static void main ( String args[] )
{
PythonInterpreter interp = new PythonInterpreter();
interp.exec(my_class_source);
interp.exec(create_instance);
interp.exec(invoke_hello);
interp.exec(overwrite_meth);
interp.exec(invoke_hello);
}

}
//************************************************************
end SelfMod.java ---------------------------------------------

need-to-complete-my-coursework-for-telepathy-101-ly y'rs
 
K

Kent Johnson

Jan said:
Hello folks

I want to apply changes in my source code without stopping jython
and JVM. Preferable are modifications directly to instances of
classes. My application is a desktop app using swing library.

Can you be more specific? Python and Jython allow classes to be modified at runtime without changing the source code or compiling new code. For example you can add and remove methods and attributes from a class and change the base classes of a class. You can also modify individual instances of a class to change their attributes and behaviour independently of other instances of the class. What are you trying to do? For example see
http://groups.google.com/group/comp...ca4/18215f7ce8f5d609?rnum=15#18215f7ce8f5d609
http://groups.google.com/group/comp...eaa/e599041de4b8feb0?rnum=22#e599041de4b8feb0

Kent
 
J

Jan Gregor

Can you be more specific? Python and Jython allow classes to be modified at runtime without changing the source code or compiling new code. For example you can add and remove methods and attributes from a class and change the base classes of a class. You can also modify individual instances of a class to change their attributes and behaviour independently of other instances of the class. What are you trying to do? For example see
http://groups.google.com/group/comp...ca4/18215f7ce8f5d609?rnum=15#18215f7ce8f5d609
http://groups.google.com/group/comp...eaa/e599041de4b8feb0?rnum=22#e599041de4b8feb0

Kent

thanks for links, I'll look at them.
Alan showed me some possibilities, i'm not quite sure how to initiate
modification and the way import keyword works in case of modified
imported modules.

my typical scenario is that my swing application is running, and i see
some error or chance for improvement - modify sources of app, stop and run
application again.
so task is to reload class defitions (from source files) and modify also
existing instances (their methods).

Jan
 
K

Kent Johnson

Jan said:
my typical scenario is that my swing application is running, and i see
some error or chance for improvement - modify sources of app, stop and run
application again.
so task is to reload class defitions (from source files) and modify also
existing instances (their methods).

Ok, I think my first reply completely missed the mark. IIUC what you want is hard. This recipe might help:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164

Kent
 
A

Alan Kennedy

[Jan Gregor]
Following try showed me that instances aren't affected by
modification in class definition.

Is this more like what you mean?

c:\>jython
Jython 2.1 on java1.4.2_09 (JIT: null)
Type "copyright", "credits" or "license" for more information..... def test(self):
.... print "First"
........ print "Second"
....
If that's what you're thinking, you should read up on class objects,
instance objects and method objects.

http://docs.python.org/tut/node11.html#SECTION0011320000000000000000

[Jan Gregor]
> my typical scenario is that my swing application is running, and
> i see some error or chance for improvement - modify sources of app,
> stop and run application again.

You're really talking about an IDE here.
> so task is to reload class defitions (from source files) and modify
> also existing instances (their methods).

You can modify the behaviour of all existing instances of a class, e.g.
their methods, by modifying the class itself. Any reference to the
method on the instance will resolve to the method definition from its
class, provided you haven't overwritten the method on the individual
instance. Resolving the method is done at invocation time, because
python/jython is a late-binding language.

Any closer?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top