By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,209 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,209 IT Pros & Developers. It's quick & easy.

modifying source at runtime - jython case

P: n/a
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
Nov 5 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
[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

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
Nov 5 '05 #2

P: n/a
In article <b1*****************@news.indigo.ie>, Alan Kennedy wrote:
[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

Nov 5 '05 #3

P: n/a
Jan Gregor wrote:
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....215f7ce8f5d609
http://groups.google.com/group/comp....99041de4b8feb0

Kent
Nov 5 '05 #4

P: n/a
In article <43**********@newspeer2.tds.net>, Kent Johnson wrote:
Jan Gregor wrote:
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....215f7ce8f5d609
http://groups.google.com/group/comp....99041de4b8feb0

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

Nov 6 '05 #5

P: n/a
Jan Gregor wrote:
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/Coo.../Recipe/160164

Kent
Nov 6 '05 #6

P: n/a
[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.
class a: .... def test(self):
.... print "First"
.... x = a()
x.test() First def test(self): .... print "Second"
.... a.test = test
x.test() 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.ht...00000000000000

[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?

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
Nov 6 '05 #7

P: n/a
Kent Johnson wrote:
Jan Gregor wrote:
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/Coo.../Recipe/160164

Kent


Yes, this is right way. And it's working.

Thanks,
Jan
Nov 8 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.