Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Reloading nested modules (http://www.velocityreviews.com/forums/t319636-reloading-nested-modules.html)

Andy Jewell 07-13-2003 09:33 PM

Reloading nested modules
 
Does anyone know of a way to dynamically reload all the imported modules of a
client module?

I'm writing a program that I have broken down into quite a few submodules, and
the 'configuration' is done with modules too. As I am developing the app, I
need to test bits and pieces here and there. This is what I currently do:

In each module, I have a section, just after the main imports like so:


-----------8<-----------
# normal imports:

from ooby import squibble,dibble
import dooby
import doo

# reloads for debugging

import ooby # create a reference to ooby so we can reload it
reload(ooby)
reload(dooby)
reload(doo)

# main module code begins...
-----------8<-----------


The debugging imports of course will be dropped when the program is stable.

I also often find myself doing similar stuff from the command-line, like:

reload(ooby.oojar); reload(ooby.dibble); reload(ooby); ooby.somefunc(.....)

I just wondered if anyone had develped a better 'spell' or even a small script
that uses some clever intorspecton hack, before I go and start trying to
reinvent the wheel...

-andyj


Greg Fortune 07-14-2003 07:01 PM

Re: Reloading nested modules
 
I've wondered about the same problem and considered that as a solution, but
never tested it. I assume that *wouldn't* rebind all of the imports that
have already happened.. ie, I think the references to the old copies of
the modules would hang around even though new ones have been imported.

Regardless, the scoping doesn't work so an attempt to reload the embedded
module assumes it is available in local scope and fails. Again, that would
lead me to believe my first statement is true, but when I tested just now,
I got no further than the scoping problem...

I've been intended to write something that will take a module name and
rebind it in all namespaces that have it currently, but haven't got around
to it. If I ever do, I'll post it here :)

Greg

Martin v. L÷wis wrote:

> Andy Jewell <andy@wild-flower.co.uk> writes:
>
>> Does anyone know of a way to dynamically reload all the imported
>> modules of a client module?

>
> You could iterate over sys.modules and invoke reload for all of them.
>
> Regards,
> Martin



Martin v. =?iso-8859-15?q?L=F6wis?= 07-14-2003 08:21 PM

Re: Reloading nested modules
 
Greg Fortune <lists@gregfortune.com> writes:

> I've wondered about the same problem and considered that as a solution, but
> never tested it. I assume that *wouldn't* rebind all of the imports that
> have already happened.. ie, I think the references to the old copies of
> the modules would hang around even though new ones have been imported.


Depends on the import. For

import foo

the reload would take effect, as, on reload, the module object stays,
its dictionary stays, and it is just the dictionary contents that is
recreated.

For

from foo import bar

you still have the old value of bar after reloading.

> Regardless, the scoping doesn't work so an attempt to reload the embedded
> module assumes it is available in local scope and fails. Again, that would
> lead me to believe my first statement is true, but when I tested just now,
> I got no further than the scoping problem...


What scoping problem?

> I've been intended to write something that will take a module name and
> rebind it in all namespaces that have it currently, but haven't got around
> to it. If I ever do, I'll post it here :)


Just ask Guido to borrow you the time machine - this has already been
done.

Regards,
Martin



All times are GMT. The time now is 01:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.