Reload All

Discussion in 'Python' started by Emmanuel, Apr 21, 2004.

  1. Emmanuel

    Emmanuel Guest

    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.values():
    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.__init__(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_root(root):
    if(not inspect.ismodule(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(reload_me) > 0):
    tmp = reload_me.pop()

    for x in inspect.getmembers(tmp[1]):
    if(inspect.ismodule(x[1])):
    if ( '__file__' in dir ( x[1] ) ):
    if((tmp[1], getattr(tmp[1], x[0])) not in reloaded):
    reload_me.append((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.reverse()
    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.append( 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
    Emmanuel, Apr 21, 2004
    #1
    1. Advertising

  2. Emmanuel

    Emmanuel Guest

    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.values():
    > 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.__init__(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_root(root):
    > if(not inspect.ismodule(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(reload_me) > 0):
    > tmp = reload_me.pop()
    >
    > for x in inspect.getmembers(tmp[1]):
    > if(inspect.ismodule(x[1])):
    > if ( '__file__' in dir ( x[1] ) ):
    > if((tmp[1], getattr(tmp[1], x[0])) not in reloaded):
    > reload_me.append((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.reverse()
    > 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.append( pair[1])
    > ----------------------------------------------------
    >
    > This code solves my current problems, but I can imagine some possible
    > issue with two levels hierarchies.


    euh... Can I ???
    Emmanuel, Apr 22, 2004
    #2
    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. Andrzej Wegrzyn
    Replies:
    2
    Views:
    721
    John Saunders
    Aug 18, 2003
  2. Joshua Beall
    Replies:
    26
    Views:
    220,303
    AndreiKaa
    Jun 29, 2007
  3. gen_tricomi
    Replies:
    2
    Views:
    735
    gen_tricomi
    May 23, 2006
  4. Stefan Mueller
    Replies:
    4
    Views:
    293
    Stefan Mueller
    Nov 5, 2005
  5. Hrvoje Vencl

    Reload all included javascript src

    Hrvoje Vencl, May 17, 2007, in forum: Javascript
    Replies:
    3
    Views:
    106
    Hrvoje Vencl
    May 21, 2007
Loading...

Share This Page