473,770 Members | 4,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reload All

Hi,
I use a 'reload all' feature in my app, that allow to reload every
module.

I first try this version :

import sys
def Reload():
for a in sys.modules.val ues():
if ( type(a) == type(sys) ): # I can't remember why
if ( '__file__' in dir ( a ) ): # to avoid reload of
extending C module
try:
## print a.__name__
reload ( a )
except ImportError:
Log ("Reload", "error importing module" +
a.__name__)
but there are some cases where the order of the reload is important.
I came into this one :

CODE :
===============
Base.py
----------
class Base:
def __init__(self):
print "Base Init"

Derived.py
------------
import Base
class Derived( Base.Base):
def __init__(self):
Base.Base.__ini t__(self)
print "Derived additional init"

In order to reload all to work, Base _MUST_ be reloaded before Derived.

So I continue with this code, modified from something I get on the forum
:
----------------------------------------------------
import inspect

def reload_from_roo t(root):
if(not inspect.ismodul e(root)):
print 'root is not a module!! Failing.'
print type(root)

#Keeping lists of pairs where pairs are (parent, child)
#to establish module relationships and prevent
#circular reloads
reload_me = [(None, root)]
reloaded = []

while(len(reloa d_me) > 0):
tmp = reload_me.pop()

for x in inspect.getmemb ers(tmp[1]):
if(inspect.ismo dule(x[1])):
if ( '__file__' in dir ( x[1] ) ):
if((tmp[1], getattr(tmp[1], x[0])) not in reloaded):
reload_me.appen d((tmp[1], getattr(tmp[1], x[0])))
## reload(tmp[1]) # If I use reload here, the child is
always reloaded before the parent
reloaded.append ((tmp[0], tmp[1]))

reloaded.revers e()
TheReloaded = []
for pair in reloaded:
if (pair[1] not in TheReloaded ): # Don't reload twice
the Base, else it can reload in this order :
# Base -
Derived1 - Base- Derived2
# and
Derived1 init will fails
reload (pair[1])
TheReloaded.app end( pair[1])
----------------------------------------------------
This code solves my current problems, but I can imagine some possible
issue with two levels hierarchies.

So is there a perfect solution ?

Thanks by advance,

Emmanuel

Jul 18 '05 #1
1 2144


Emmanuel a écrit :
Hi,

I use a 'reload all' feature in my app, that allow to reload every
module.

I first try this version :

import sys
def Reload():
for a in sys.modules.val ues():
if ( type(a) == type(sys) ): # I can't remember why
if ( '__file__' in dir ( a ) ): # to avoid reload of
extending C module
try:
## print a.__name__
reload ( a )
except ImportError:
Log ("Reload", "error importing module" +
a.__name__)

but there are some cases where the order of the reload is important.
I came into this one :

CODE :
===============
Base.py
----------
class Base:
def __init__(self):
print "Base Init"

Derived.py
------------
import Base
class Derived( Base.Base):
def __init__(self):
Base.Base.__ini t__(self)
print "Derived additional init"

In order to reload all to work, Base _MUST_ be reloaded before Derived.

So I continue with this code, modified from something I get on the forum
:
----------------------------------------------------
import inspect

def reload_from_roo t(root):
if(not inspect.ismodul e(root)):
print 'root is not a module!! Failing.'
print type(root)

#Keeping lists of pairs where pairs are (parent, child)
#to establish module relationships and prevent
#circular reloads
reload_me = [(None, root)]
reloaded = []

while(len(reloa d_me) > 0):
tmp = reload_me.pop()

for x in inspect.getmemb ers(tmp[1]):
if(inspect.ismo dule(x[1])):
if ( '__file__' in dir ( x[1] ) ):
if((tmp[1], getattr(tmp[1], x[0])) not in reloaded):
reload_me.appen d((tmp[1], getattr(tmp[1], x[0])))

## reload(tmp[1]) # If I use reload here, the child is
always reloaded before the parent
reloaded.append ((tmp[0], tmp[1]))

reloaded.revers e()
TheReloaded = []
for pair in reloaded:
if (pair[1] not in TheReloaded ): # Don't reload twice
the Base, else it can reload in this order :
# Base -
Derived1 - Base- Derived2
# and
Derived1 init will fails
reload (pair[1])
TheReloaded.app end( pair[1])
----------------------------------------------------

This code solves my current problems, but I can imagine some possible
issue with two levels hierarchies.


euh... Can I ???

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
3908
by: Ellinghaus, Lance | last post by:
> > Other surprises: Deprecating reload() >Reload doesn't work the way most people think >it does: if you've got any references to the old module, >they stay around. They aren't replaced. >It was a good idea, but the implementation simply >doesn't do what the idea promises. I agree that it does not really work as most people think it does, but how
4
1810
by: David MacQuigg | last post by:
I'm going to be teaching EEs some basic Python using the first few chapters of Learning Python, 2nd ed. by Mark Lutz. The discussion on Reloading Modules starting on page 266 is confusing and I believe incorrect. On page 266 it says that a reload "changes the existing module object in place." That's a little vague, but on page 267 it says "every reference to a module object anywhere in your program is automatically affected by a...
1
4822
by: zdhiu | last post by:
Hi javascript gurus, I have a simple html file (simple.html) with javascript. In html page there is sentence from variable defined in .js file (myFirst.js). Once I reload another .js file (mySecond.js), the value defined in the second one is NOT reloaded into html page. Please help out what's wrong in my code. BTW, I use IE6.0
19
31072
by: Darren | last post by:
I have a page that opens a popup window and within the window, some databse info is submitted and the window closes. It then refreshes the original window using window.opener.location.reload(). The problem is that after the reload, it brings you right to the top of the page. When I click 'refresh" on the original page, it brings me back to the original viewing position. Is there a way to duplicate this in from the popup window. Also,...
4
4665
by: Lonnie Princehouse | last post by:
So, it turns out that reload() fails if the module being reloaded isn't in sys.path. Maybe it could fall back to module.__file__ if the module isn't found in sys.path?? .... or reload could just take an optional path parameter... Or perhaps I'm the only one who thinks this is silly: >>> my_module = imp.load_module(module_name, *imp.find_module(module_name,path))
3
2184
by: John Salerno | last post by:
I understand that after you import something once, you can reload it to pick up new changes. But does reload work with from statements? I tried this: from X import * and then did my testing. I changed X and tried to reload it, but that didn't seem to work. I figure the reason is because the module itself doesn't exist as an object, only its names do. But I couldn't figure out how to pick up my new changes at this point. I think...
8
6831
by: T. Wintershoven | last post by:
Hi all, Is there a simple way in php to reload a page coded within an if statement.(see code below) It's very important that the session stays intact. The filename is RCStudent.php **************** A peace of code*************************** <?php session_start();
1
13391
by: bernhard.voigt | last post by:
Hey! I'm using ipython as my python shell and often run scripts with the magic command %run: In : %run script.py If modules are loaded within the script these are not reloaded when I rerun the script. Hence, when I changed some of the modules loaded, I have to call
0
1905
by: Rafe | last post by:
Hi, This seems to be an old question, and I've read back a bit, but rather than assume the answer is "you can't do that", I'd thought I'd post my version of the question along with a reproducible error to illustrate my confusion. My problem is that I'm using Python inside XSI (a 3D graphics application). If I want to restart Python, I have to restart XSI. This is no small amount of time wasted.
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.