modifying source at runtime - jython case

Discussion in 'Python' started by Jan Gregor, Nov 5, 2005.

  1. Jan Gregor

    Jan Gregor Guest

    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
    Jan Gregor, Nov 5, 2005
    #1
    1. Advertising

  2. Jan Gregor

    Alan Kennedy Guest

    [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
    Alan Kennedy, Nov 5, 2005
    #2
    1. Advertising

  3. Jan Gregor

    Jan Gregor Guest

    In article <b11bf.18224$>, 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
    >
    Jan Gregor, Nov 5, 2005
    #3
  4. Jan Gregor

    Kent Johnson Guest

    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...ca4/18215f7ce8f5d609?rnum=15#18215f7ce8f5d609
    http://groups.google.com/group/comp...eaa/e599041de4b8feb0?rnum=22#e599041de4b8feb0

    Kent
    Kent Johnson, Nov 5, 2005
    #4
  5. Jan Gregor

    Jan Gregor Guest

    In article <436cd898$>, 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...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
    Jan Gregor, Nov 6, 2005
    #5
  6. Jan Gregor

    Kent Johnson Guest

    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/Cookbook/Python/Recipe/160164

    Kent
    Kent Johnson, Nov 6, 2005
    #6
  7. Jan Gregor

    Alan Kennedy Guest

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

    --
    alan kennedy
    ------------------------------------------------------
    email alan: http://xhaus.com/contact/alan
    Alan Kennedy, Nov 6, 2005
    #7
  8. Jan Gregor

    Jan Gregor Guest

    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/Cookbook/Python/Recipe/160164
    >
    > Kent


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

    Thanks,
    Jan
    Jan Gregor, Nov 8, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmhhdmluaQ==?=

    Reading and modifying .resx files at runtime

    =?Utf-8?B?QmhhdmluaQ==?=, Jul 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    5,944
    =?Utf-8?B?QmhhdmluaQ==?=
    Jul 13, 2006
  2. David McNab
    Replies:
    0
    Views:
    485
    David McNab
    Apr 9, 2004
  3. Myriam Abramson

    modifying images at runtime

    Myriam Abramson, Oct 19, 2006, in forum: Java
    Replies:
    1
    Views:
    314
    Oliver Wong
    Oct 19, 2006
  4. Divya Prakash
    Replies:
    0
    Views:
    430
    Divya Prakash
    Dec 19, 2006
  5. Pinegar, Kent T

    FW: [Jython-users] Jython Licensing Question

    Pinegar, Kent T, Jul 24, 2008, in forum: Python
    Replies:
    0
    Views:
    346
    Pinegar, Kent T
    Jul 24, 2008
Loading...

Share This Page