exceptions

Hung Jung Lu hungjunglu at yahoo.com
Thu Jun 3 00:26:35 EDT 2004


Alexander Schmolck <a.schmolck at gmx.net> wrote:
> [1] Here are a few of the hacks I'm using, in case anyone might 
>     find them useful -- or even better tell me about better 
>     alternatives (If someone has cooked something reasoable 
>     for reloading modules, I'd love to hear about it).

I have already answered to you on one previous occasion, and told you
another time in this present thread. I've used this kind of trick in
wxPython. It's good for interactively developing widgets, without
re-starting your program.

Now here is a third try. :)

(
For the first time, see:
http://groups.google.com/groups?q=g:thl272312511d&dq=&hl=en&lr=&ie=UTF-8&selm=8ef9bea6.0308220959.74317026%40posting.google.com
)

You can of course use a metaclass to make things a bit easier. Here is
a draft version.

#----- autoupdate.py
'''metaclass for auto-update classes'''
class __metaclass__(type):
    # automatically keeps tracks of instances    
    def __new__(cls, class_name, bases, class_dict):
        import inspect
        module_name = class_dict['__module__']
        instance_dict_name = '_%s__instances' % class_name
        # see if there is already an older class
        import sys
        old_instance_dict = None
        if sys.modules.has_key(module_name):
            module = sys.modules[module_name]
            if hasattr(module, class_name):
                old_instance_dict = getattr(
                                        getattr(module, class_name),
                                        instance_dict_name)
        # add instance list
        import weakref
        class_dict[instance_dict_name] = weakref.WeakValueDictionary()
        # override the __init__
        if class_dict.has_key('__init__'):
            def new_init(self, *args, **kw):
                instance_dict_name = '_%s__instances' % (
                                     self.__class__.__name__)
                getattr(self.__class__,
                        instance_dict_name)[id(self)] = self
                self.__original_init__(*args, **kw)
            class_dict['__original_init__'] = class_dict['__init__']
            class_dict['__init__'] = new_init
        else:
            def new_init(self, *args, **kw):
                instance_dict_name = '_%s__instances' % (
                                     self.__class__.__name__)
                getattr(self.__class__,
                        instance_dict_name)[id(self)] = self
            class_dict['__init__'] = new_init            
        # build the class, with instance_dict
        new_class = type.__new__(cls, class_name, bases, class_dict)
        # copy over the instance dictionary, and update __class__
        if old_instance_dict is not None:
            for instance_id, instance in (
                              old_instance_dict.iteritems()):
                getattr(new_class,
                        instance_dict_name)[instance_id] = instance
                instance.__class__ = new_class
        # return new class
        return new_class

#----- Spam.py
from autoupdate import __metaclass__
class Egg:
    '''Put here your class code'''
    # def print(self):
    #     print 'Hello World!'

#----- from your Python console
import Spam
x = Spam.Egg()
# ... edit your Spam.py file and change code for Spam.Egg class,
#     e.g.: add a print() method to Spam.Egg by uncommenting
#     the corresponding lines. Save the file.
reload(Spam)
x.print() # prints 'Hello World!'
    
---------------------

regards,

Hung Jung



More information about the Python-list mailing list