Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Created October 9, 2008 21:55
Show Gist options
  • Save inklesspen/15914 to your computer and use it in GitHub Desktop.
Save inklesspen/15914 to your computer and use it in GitHub Desktop.
import types
def build_tree(start_point=None):
if start_point is not None and type(start_point) is not types.ModuleType:
print "start_point is not None (meaning use globals) and is not a module"
check = [start_point]
checked = set()
tree = {}
while len(check) > 0:
current = check.pop(0)
if current is None:
d = globals()
else:
print "Popping %s" % current.__name__
d = current.__dict__
current_key = "globals()" if current is None else current.__name__
for k, v in d.iteritems():
if type(v) is types.ModuleType:
if current_key not in tree:
tree[current_key] = set()
tree[current_key].add(v.__name__)
if v not in checked:
check.append(v)
print "Appending %s" % v.__name__
checked.add(current)
return tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment