Created
November 9, 2017 04:57
-
-
Save MrNocTV/66d8006a15448281eeeb1aabe4588007 to your computer and use it in GitHub Desktop.
Script use reload to reload all submodules inside a module.
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
import types | |
from imp import reload | |
def status(module): | |
print('reloading ' + module.__name__) | |
def try_reload(module): #FAIL (sometimes in 3.3) | |
try: | |
reload(module) | |
except: | |
print('FAILED: %s' % module) | |
def transitive_reload(module, visited): | |
if not module in visited: | |
status(module) | |
try_reload(module) | |
visited.add(module) | |
for attr_obj in module.__dict__.values(): | |
if type(attr_obj) == types.ModuleType: | |
transitive_reload(attr_obj, visited) | |
def reload_all(*args): | |
visited = set() | |
for arg in args: | |
if type(arg) == types.ModuleType: | |
transitive_reload(arg, visited) | |
def tester(reloader, module_name): | |
import importlib, sys | |
if len(sys.argv) > 1: | |
module_name = sys.argv[1] | |
module = importlib.import_module(module_name) | |
reloader(module) | |
if __name__ == '__main__': | |
tester(reload_all, 'reloadall') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment