mailr18698 - /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 08, 2013 - 14:43:
Author: bugman
Date: Fri Mar  8 14:43:46 2013
New Revision: 18698

URL: http://svn.gna.org/viewcvs/relax?rev=18698&view=rev
Log:
The table contents are now all converted to strings in 
lib.text.table.format_table().

This uses the _convert_to_string() private function.


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=18698&r1=18697&r2=18698&view=diff
==============================================================================
--- trunk/lib/text/table.py (original)
+++ trunk/lib/text/table.py Fri Mar  8 14:43:46 2013
@@ -25,6 +25,9 @@
 # Python module imports.
 from copy import deepcopy
 from textwrap import wrap
+
+# relax module imports.
+from check_types import is_float
 
 
 # Special variables.
@@ -46,6 +49,34 @@
 
     # Return the blank line.
     return prefix + ' '*width + postfix + "\n"
+
+
+def _convert_to_string(data=None):
+    """Convert all elements of the given data structures to strings in place.
+
+    @keyword data:      The headings or content to convert.
+    @type data:         list of lists of anything.
+    """
+
+    # Loop over the rows.
+    for i in range(len(data)):
+        # Loop over the columns.
+        for j in range(len(data[i])):
+            # None types.
+            if data[i][j] == None:
+                data[i][j] = ''
+
+            # Int types.
+            elif isinstance(data[i][j], int):
+                data[i][j] = "%i" % data[i][j]
+
+            # Float types.
+            elif is_float(data[i][j]):
+                data[i][j] = "%g" % data[i][j]
+
+            # All other non-string types.
+            elif not isinstance(data[i][j], str):
+                data[i][j] = "%s" % data[i][j]
 
 
 def _rule(width=None, prefix=' ', postfix=' '):
@@ -160,6 +191,14 @@
     num_cols = len(contents[0])
     num_head_rows = len(headings)
 
+    # Deepcopy so that modifications to the data are not seen.
+    headings = deepcopy(headings)
+    contents = deepcopy(contents)
+
+    # Convert all data to strings.
+    _convert_to_string(headings)
+    _convert_to_string(contents)
+
     # Initialise the pre-wrapping column widths.
     prewrap_widths = [0] * num_cols
 




Related Messages


Powered by MHonArc, Updated Fri Mar 08 15:00:02 2013