""" reloadall3.py: transitively reload nested modules (explicit stack) """ import types from imp import reload # from required in 3.X from reloadall import status, tryreload, tester def transitive_reload(modules, visited): while modules: next = modules.pop() # Delete next item at end status(next) # Reload this, push attrs tryreload(next) visited.add(next) modules.extend(x for x in next.__dict__.values() if type(x) == types.ModuleType and x not in visited) def reload_all(*modules): transitive_reload(list(modules), set()) if __name__ == '__main__': tester(reload_all, 'reloadall3') # Test code: reload myself?
# -*- coding: utf-8 -*- """ reloadall3.py: 중첩된 모듈들을 이행적으로 리로드(명시적 스택) """ import types from imp import reload # from은 3.X에서는 필수 from reloadall import status, tryreload, tester def transitive_reload(modules, visited): while modules: next = modules.pop() # 끝에서 next 항목 삭제 status(next) # 이것을 리로드, 속성 밀어 넣기 tryreload(next) visited.add(next) modules.extend(x for x in next.__dict__.values() if type(x) == types.ModuleType and x not in visited) def reload_all(*modules): transitive_reload(list(modules), set()) if __name__ == '__main__': tester(reload_all, 'reloadall3') # 테스트 코드: 나 자신을 리로드?
""" reloadall2.py: transitively reload nested modules (alternative coding) """ import types from importlib import reload from reloadall import status, tryreload, tester def transitive_reload(objects, visited): for obj in objects: if type(obj) == types.ModuleType and obj not in visited: status(obj) tryreload(obj) # Reload this, recur to attrs visited.add(obj) transitive_reload(obj.__dict__.values(), visited) def reload_all(*args): transitive_reload(args, set()) if __name__ == '__main__': tester(reload_all, 'reloadall2') # Test code: reload myself
import types from imp import reload from reloadall import status, tryreload, tester def transitive_reload(objects, visited): for obj in objects: if type(obj) == types.ModuleType and obj not in visited: status(obj) tryreload(obj) visited.add(obj) transitive_reload(obj.__dict__.values(), visited) def reload_all(*args): transitive_reload(args, set()) if __name__ == '__main__': tester(reload_all, 'reloadall2')
""" reloadall3.py: transitively reload nested modules (nested stack) """ import types from reloadall import status, tryreload, tester def transitive_reload(modules, visited): while modules: nextmod = modules.pop() status(nextmod) tryreload(nextmod) visited.add(nextmod) #print('VISITED:', visited) modules.extend(x for x in nextmod.__dict__.values() if type(x) == types.ModuleType and x not in visited and x not in modules) #print('MODULES;', modules) def reload_all(*modules): transitive_reload(list(modules), set()) if __name__ == '__main__': tester(reload_all, 'reloadall3')