[code.view]

[top] / python / PyMOTW / heapq / heapq_showtree.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import math
     from cStringIO import StringIO
     
     def show_tree(tree, total_width=36, fill=' '):
         """Pretty-print a tree."""
         output = StringIO()
         last_row = -1
         for i, n in enumerate(tree):
             if i:
                 row = int(math.floor(math.log(i+1, 2)))
             else:
                 row = 0
             if row != last_row:
                 output.write('\n')
             columns = 2**row
             col_width = int(math.floor((total_width * 1.0) / columns))
             output.write(str(n).center(col_width, fill))
             last_row = row
         print output.getvalue()
         print '-' * total_width
         print
         return
     

[top] / python / PyMOTW / heapq / heapq_showtree.py

contact | logmethods.com