Instantly share code, notes, and snippets.

# yanolab/html_fibo.patch Created Aug 1, 2011

What would you like to do?
apply patch to html_fibo.py
 --- html_fibo.orig 2011-08-01 12:51:29.063696436 +0900 +++ html_fibo.py 2011-08-01 12:50:58.703267469 +0900 @@ -2,6 +2,8 @@ The most complicate ever way to produce an HTML list of fibonacci numbers """ +import gc + def fibo(): a, b = 1, 1 while True: @@ -26,8 +28,10 @@ tag = HtmlTag(f, 4, 'li') yield n tag = None + gc.collect() finally: tag = None + gc.collect() f.write('\n') @@ -37,6 +41,8 @@ f.write('%d' % n) if n > 100: break + gc.collect() + f.close() def main(): write_file()
 """ The most complicate ever way to produce an HTML list of fibonacci numbers """ import gc def fibo(): a, b = 1, 1 while True: yield a a, b = b, a+b class HtmlTag(object): def __init__(self, f, indent, tag): self.f = f self.tag = tag self.f.write(' ' * indent) self.f.write('<%s>' % tag) def __del__(self): self.f.write('\n' % self.tag) def html_fibo(f): f.write('
\n') try: for n in fibo(): tag = HtmlTag(f, 4, 'li') yield n tag = None gc.collect() finally: tag = None gc.collect() f.write('
\n') def write_file(): f = open('fibo.txt', 'w') for n in html_fibo(f): f.write('%d' % n) if n > 100: break gc.collect() f.close() def main(): write_file() content = open('fibo.txt').read() print content if __name__ == '__main__': main()