Created
August 6, 2019 00:40
-
-
Save shwang/09bd0ffef8ef65af817cf977ce4698b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from importlib import reload | |
from types import ModuleType | |
def rreload(module): | |
"""Recursively reload a module and all its submodules. | |
Graph DFS strategy modified from | |
https://stackoverflow.com/questions/15506971/recursive-version-of-reload | |
""" | |
visited = set() | |
def visit(m): | |
if m in visited: | |
return | |
visited.add(m) | |
reload(module) | |
for attribute_name in dir(m): | |
attribute = getattr(m, attribute_name) | |
if type(attribute) is ModuleType: | |
visit(attribute) | |
visit(module) |
Line 14 is in the wrong place, we want to visit (and reload) child nodes before reloading the current node.
Incorporating @dtasev's changes, this is the correct algorithm:
def rreload(module):
"""Recursively reload a module and all its submodules.
Graph DFS strategy modified from
https://stackoverflow.com/questions/15506971/recursive-version-of-reload
"""
from types import ModuleType
visited = set()
def visit(m):
if m in visited:
return
visited.add(m)
for attribute_name in dir(m):
attribute = getattr(m, attribute_name)
if type(attribute) is ModuleType:
visit(attribute)
reload(m)
visit(module)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Had to change line 14 from
reload(module)
toreload(m)