[Python] module maze

#!/usr/bin/env python

import new
from random import choice, randint
from string import ascii_lowercase

def randword(n=8):
    return ''.join([choice(ascii_lowercase) for _ in xrange(randint(1,n))])

__all_modules = []
def generate_module_maze(module, num=8, depth=0):
    if depth > 0:
        k = randint(1,num)
        for n in xrange(k):
            name = randword()
            new_module = new.module(name, '')
            __all_modules.append(new_module)
            setattr(module, name, new_module)
            generate_module_maze(new_module, num, depth-1)
        for n in xrange(num - k):
            old_module = choice(__all_modules)
            name = old_module.__name__
            setattr(module, name, old_module)

root = new.module('root')
generate_module_maze(root, depth=5)
choice(__all_modules).__doc__ = 'GOAL'
#def answer():
#    return [m for m in __all_modules if m.__doc__ == 'GOAL'][0]
del __all_modules

if __name__ == '__main__':
    import maze
    # Find the module which has docstring 'GOAL'
    print dir(maze.root)