Skip to content

Instantly share code, notes, and snippets.

@akaptur
Created September 29, 2014 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaptur/940d92818e202b94364e to your computer and use it in GitHub Desktop.
Save akaptur/940d92818e202b94364e to your computer and use it in GitHub Desktop.
def write_block(names, next_name, f):
for depth, func_name in enumerate(names):
f.write("\n" + " " * depth + "def %s():" % func_name)
f.write("\n" + " " * (depth + 1) + "%s()" % next_name)
for depth, func_name in reversed(list(enumerate(names[1:]))):
f.write("\n" + " " * (depth + 1) + "%s()" % func_name)
f.write("\n") # for readability ;)
def write_nested_functions(total_functions, max_nesting):
with open('/usr/share/dict/words','r') as name_file:
fn_names = [line.strip() for line in name_file.readlines()]
first_name = fn_names[0]
last_name = fn_names[total_functions]
fn_names = iter(fn_names)
with open("empty_nest.py", 'w') as f:
names = take(fn_names, max_nesting)
for _ in range(total_functions // max_nesting):
next_names = take(fn_names, max_nesting)
write_block(names, next_names[0], f)
names = next_names
f.write("def %s():\n print 'yay'\n" % last_name)
f.write("%s()" % first_name)
def take(seq, count):
return [next(seq) for _ in range(count)]
if __name__ == '__main__':
write_nested_functions(1000, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment