Welcome, guest | Sign In | My Account | Store | Cart
#!/bin/sh python

class TableFormatter:

    @staticmethod
    def MAX(x,y):
        return x if x > y else y

    def __init__(self, column_names=['column_1', 'column_2']):
        self.rowData = []
        self.dataAlignType = '<'
        self.headerAlignType = '<'
        self.column_names = column_names
        self.columns = len(column_names)
        self.column_size = len(max(column_names))
        self.__setAlignment = lambda x: '>' if x == 2 else ('^' if x == 1 else '<')
        self.dataPadding = True

    def setDataPadding(self, padding):
        """Allow padding of columns when data row has fewer elements than the maximum columns. This option is set by default"""
        self.dataPadding = True if padding else False
        
    def setDataAlignment(self, align = 0):
        """Set the text alignment, LEFT by default. Valid values are 0:LEFT 1:CENTER 2:RIGHT"""
        self.dataAlignType = self.__setAlignment(align % 3)

    def setHeaderAlignment(self, align = 0):
        """Set the text alignment, LEFT by default. Valid values are 0:LEFT 1:CENTER 2:RIGHT"""
        self.headerAlignType = self.__setAlignment(align % 3)
        
    def addDataColumns(self, data_row):
        data_size = len(data_row)
        if (data_size > self.columns) or (data_size < self.columns and not self.dataPadding):
            raise Exception('Requires {0} columns, found {1} as {2}'.
                            format(self.columns, len(data_row), data_row))
        elif self.dataPadding and data_size < self.columns:
            self.rowData.append(data_row + [None] * (self.columns - data_size))
        else:
            self.rowData.append(data_row)
        self.column_size= self.MAX(len(max(data_row)), self.column_size)
 
    def __str__(self):
        return self.createTable()
    
    def createTable(self):
        headerFormat = '{0:' + self.headerAlignType + '{width}}|'
        dataFormat = '{0:' + self.dataAlignType + '{width}}|'
        colhead = '-' * self.column_size
        header = '+'
        out = '|'
        for a in self.column_names:
            out = out + headerFormat.format(a, width=self.column_size)
            header = header + '{}+'.format(colhead)        
        out = header + '\n' + out + '\n' + header + '\n'
        for v in self.rowData:
            out = out + '|'
            for a in v:
                if a:
                    out = out + dataFormat.format(a, width=self.column_size)
                else:
                    out = out + dataFormat.format('-', width=self.column_size)
            out = out + '\n'
        out = out + header
        return out
  
if __name__ == "__main__":
    tf = TableFormatter(["column_1", "column_2", "column_3", "column_4", "column_5"])
    #tf.setDataPadding(True)
    #tf.setDataAlignment(10)
    #tf.setHeaderAlignment(0)
    tf.addDataColumns(['val1', 'value2', 'value 3'])
    tf.addDataColumns(['one is alone', 'two is a company', 'three is a crowd'])
    tf.addDataColumns(['test'])
    tf.addDataColumns(['test', 'value2', 'value 3', 'value2', 'value 3'])
    print tf.createTable()
    #print tf

Diff to Previous Revision

--- revision 1 2012-04-15 10:21:27
+++ revision 2 2012-04-16 03:34:25
@@ -1,54 +1,76 @@
+#!/bin/sh python
+
 class TableFormatter:
-   
+
     @staticmethod
     def MAX(x,y):
         return x if x > y else y
 
-    def __init__(self, column_names=['column_1', 'column_2'], init_rows=1):
+    def __init__(self, column_names=['column_1', 'column_2']):
+        self.rowData = []
+        self.dataAlignType = '<'
+        self.headerAlignType = '<'
         self.column_names = column_names
         self.columns = len(column_names)
-        self.rows = init_rows
-        self.rowData = []
-        self.current_row = 1
-        self.alignType = '<'
         self.column_size = len(max(column_names))
+        self.__setAlignment = lambda x: '>' if x == 2 else ('^' if x == 1 else '<')
+        self.dataPadding = True
 
-    def setAlignment(self, align = 0):
-        """ 0:LEFT 1:CENTER 2:RIGHT """
-        if align == 0:
-            self.alignType = '<'
-        elif align == 1:
-            self.alignType = '^'
-        elif align == 2:
-            self.alignType = '>'
+    def setDataPadding(self, padding):
+        """Allow padding of columns when data row has fewer elements than the maximum columns. This option is set by default"""
+        self.dataPadding = True if padding else False
+        
+    def setDataAlignment(self, align = 0):
+        """Set the text alignment, LEFT by default. Valid values are 0:LEFT 1:CENTER 2:RIGHT"""
+        self.dataAlignType = self.__setAlignment(align % 3)
+
+    def setHeaderAlignment(self, align = 0):
+        """Set the text alignment, LEFT by default. Valid values are 0:LEFT 1:CENTER 2:RIGHT"""
+        self.headerAlignType = self.__setAlignment(align % 3)
         
     def addDataColumns(self, data_row):
-        if len(data_row) != self.columns:
-            raise Exception('Requires {0} columns, found {1} as {2}'.format(self.columns, len(data_row), data_row))
-        self.rowData.append(data_row)
-        self.current_row = self.current_row + 1
+        data_size = len(data_row)
+        if (data_size > self.columns) or (data_size < self.columns and not self.dataPadding):
+            raise Exception('Requires {0} columns, found {1} as {2}'.
+                            format(self.columns, len(data_row), data_row))
+        elif self.dataPadding and data_size < self.columns:
+            self.rowData.append(data_row + [None] * (self.columns - data_size))
+        else:
+            self.rowData.append(data_row)
         self.column_size= self.MAX(len(max(data_row)), self.column_size)
  
     def __str__(self):
+        return self.createTable()
+    
+    def createTable(self):
+        headerFormat = '{0:' + self.headerAlignType + '{width}}|'
+        dataFormat = '{0:' + self.dataAlignType + '{width}}|'
         colhead = '-' * self.column_size
         header = '+'
-        strout = '{0:' + self.alignType + '{width}}|'
         out = '|'
         for a in self.column_names:
-            out = out + strout.format(a, width=self.column_size)
-            header = header + '{}+'.format(colhead)
+            out = out + headerFormat.format(a, width=self.column_size)
+            header = header + '{}+'.format(colhead)        
         out = header + '\n' + out + '\n' + header + '\n'
         for v in self.rowData:
             out = out + '|'
             for a in v:
-                out = out + strout.format(a, width=self.column_size)
+                if a:
+                    out = out + dataFormat.format(a, width=self.column_size)
+                else:
+                    out = out + dataFormat.format('-', width=self.column_size)
             out = out + '\n'
         out = out + header
         return out
   
 if __name__ == "__main__":
-    tf = TableFormatter(["column_1", "column_2", "column_3"])
+    tf = TableFormatter(["column_1", "column_2", "column_3", "column_4", "column_5"])
+    #tf.setDataPadding(True)
+    #tf.setDataAlignment(10)
+    #tf.setHeaderAlignment(0)
     tf.addDataColumns(['val1', 'value2', 'value 3'])
     tf.addDataColumns(['one is alone', 'two is a company', 'three is a crowd'])
-    tf.setAlignment(1)
-    print tf
+    tf.addDataColumns(['test'])
+    tf.addDataColumns(['test', 'value2', 'value 3', 'value2', 'value 3'])
+    print tf.createTable()
+    #print tf

History