mailr18673 - /trunk/lib/text/table.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on March 07, 2013 - 17:58:
Author: bugman
Date: Thu Mar  7 17:58:11 2013
New Revision: 18673

URL: http://svn.gna.org/viewcvs/relax?rev=18673&view=rev
Log:
Refactored the lib.text.table module.

The create_table() function is now called format_table() and the table_line() 
function has been made
private.  All references to the user function tables and the relax status 
object have been removed
and replaced by arguments to format_table().


Modified:
    trunk/lib/text/table.py

Modified: trunk/lib/text/table.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/lib/text/table.py?rev=18673&r1=18672&r2=18673&view=diff
==============================================================================
--- trunk/lib/text/table.py (original)
+++ trunk/lib/text/table.py Thu Mar  7 17:58:11 2013
@@ -26,47 +26,101 @@
 from copy import deepcopy
 from textwrap import wrap
 
-# relax module imports.
-import ansi
-import prompt.help
-from relax_string import strip_lead
-from status import Status; status = Status()
-from user_functions.data import Uf_tables; uf_tables = Uf_tables()
-
-
-def create_table(label):
+
+def _table_line(text=None, widths=None, bottom=False):
+    """Format a line of a table.
+
+    @keyword text:      The list of table elements.  If not given, an empty 
line will be be produced.
+    @type text:         list of str or None
+    @keyword widths:    The list of column widths for the table.
+    @type widths:       list of int
+    @keyword botton:    A flag which if True will cause a table bottom line 
to be produced.
+    @type bottom:       bool
+    @return:            The table line.
+    @rtype:             str
+    """
+
+    # Initialise.
+    if bottom:
+        line = " _"
+    else:
+        line = "  "
+
+    # Loop over the columns.
+    for i in range(len(widths)):
+        # The column separator.
+        if i > 0:
+            if bottom:
+                line += "___"
+            else:
+                line += "   "
+
+        # A bottom line.
+        if bottom:
+            line += "_" * widths[i]
+
+        # Empty line.
+        elif text == None:
+            line += " " * widths[i]
+
+        # The text.
+        else:
+            line += text[i]
+            line += " " * (widths[i] - len(text[i]))
+
+    # Close the line.
+    if bottom:
+        line += "_ \n"
+    else:
+        line += "  \n"
+
+    # Return the text.
+    return line
+
+
+def format_table(headings=None, contents=None, max_width=None, debug=False):
     """Format and return the table as text.
 
-    @param label:       The unique table label.
-    @type label:        str
+    @keyword headings:  The table header.
+    @type headings:     list of lists of str
+    @keyword contents:  The table contents.
+    @type contents:     list of lists of str
+    @keyword max_width: The maximum width of the table.
+    @type max_width:    int
+    @keyword debug:     A flag which if True will activate a number of 
debugging printouts.
+    @type debug:        bool
     @return:            The formatted table.
     @rtype:             str
     """
 
-    # Get the table.
-    table = uf_tables.get_table(label)
-
     # Initialise some variables.
     text = ''
-    num_rows = len(table.cells)
-    num_cols = len(table.headings)
+    num_rows = len(contents)
+    num_cols = len(contents[0])
+    num_head_rows = len(headings)
 
     # The column widths.
-    widths = []
-    for j in range(num_cols):
-        widths.append(len(table.headings[j]))
+    widths = [0] * num_cols
+    for i in range(num_head_rows):
+        for j in range(num_cols):
+            # The element is larger than the previous.
+            if len(headings[i][j]) > widths[j]:
+                widths.append(len(headings[i][j]))
     for i in range(num_rows):
         for j in range(num_cols):
             # The element is larger than the previous.
-            if len(table.cells[i][j]) > widths[j]:
-                widths[j] = len(table.cells[i][j])
+            if len(contents[i][j]) > widths[j]:
+                widths[j] = len(contents[i][j])
 
     # The free space for the text.
     used = 0
     used += 2    # Start of the table '  '.
     used += 2    # End of the table '  '.
     used += 3 * (num_cols - 1)   # Middle of the table '   '.
-    free_space = status.text_width - used
+    if max_width:
+        free_space = max_width - used
+    else:
+        free_space = 1e10
 
     # The maximal width for all cells.
     free_width = sum(widths)
@@ -74,7 +128,7 @@
     # Column wrapping.
     if free_width > free_space:
         # Debugging printouts.
-        if status.debug:
+        if debug:
             print
             print("Table column wrapping algorithm:")
             print("%-20s %s" % ("num_cols:", num_cols))
@@ -92,7 +146,7 @@
             ave_width = int(free_space_wrap / num_cols_wrap)
 
             # Debugging printout.
-            if status.debug:
+            if debug:
                 print("    %-20s %s" % ("ave_width:", ave_width))
 
             # Rescale.
@@ -109,7 +163,7 @@
                     col_wrap[i] = False
 
                     # Debugging printout.
-                    if status.debug:
+                    if debug:
                         print("        %-20s %s" % ("remove column:", i))
 
             # Done.
@@ -121,7 +175,7 @@
                 break
 
         # Debugging printouts.
-        if status.debug:
+        if debug:
             print("    %-20s %s" % ("widths:", widths))
             print("    %-20s %s" % ("new_widths:", new_widths))
             print("    %-20s %s" % ("num_cols:", num_cols))
@@ -141,13 +195,14 @@
     # The header.
     text += " " + "_" * (total_width - 2) + "\n"    # Top rule.
     text += table_line(widths=new_widths)    # Blank line.
-    text += table_line(text=table.headings, widths=new_widths)    # The 
headings.
+    for i in range(num_head_rows):
+        text += table_line(text=headings[i], widths=new_widths)    # The 
headings.
     text += table_line(widths=new_widths, bottom=True)    # Middle rule.
 
     # The table contents.
     for i in range(num_rows):
         # Column text, with wrapping.
-        col_text = [table.cells[i]]
+        col_text = [contents[i]]
         num_lines = 1
         for j in range(num_cols):
             if col_wrap[j]:
@@ -182,54 +237,3 @@
 
     # Return the table text.
     return text
-
-
-def table_line(text=None, widths=None, bottom=False):
-    """Format a line of a table.
-
-    @keyword text:      The list of table elements.  If not given, an empty 
line will be be produced.
-    @type text:         list of str or None
-    @keyword widths:    The list of column widths for the table.
-    @type widths:       list of int
-    @keyword botton:    A flag which if True will cause a table bottom line 
to be produced.
-    @type bottom:       bool
-    @return:            The table line.
-    @rtype:             str
-    """
-
-    # Initialise.
-    if bottom:
-        line = " _"
-    else:
-        line = "  "
-
-    # Loop over the columns.
-    for i in range(len(widths)):
-        # The column separator.
-        if i > 0:
-            if bottom:
-                line += "___"
-            else:
-                line += "   "
-
-        # A bottom line.
-        if bottom:
-            line += "_" * widths[i]
-
-        # Empty line.
-        elif text == None:
-            line += " " * widths[i]
-
-        # The text.
-        else:
-            line += text[i]
-            line += " " * (widths[i] - len(text[i]))
-
-    # Close the line.
-    if bottom:
-        line += "_ \n"
-    else:
-        line += "  \n"
-
-    # Return the text.
-    return line




Related Messages


Powered by MHonArc, Updated Thu Mar 07 18:00:02 2013