Ejemplo n.º 1
0
    def columnByColumn(self, columnList):
        """Given a list of columns, this will return a list of padded, evenly sized rows."""
        tableDimensions = sizeGen(columnList)
        columnNumber = tableDimensions[0]
        rowNumber = tableDimensions[1][0]
        print(rowNumber)
        default = [self.default]
        # In theory we could modify the existing list in place, but because
        # of the strings used for column wide values, we need to create a new list (alternatively we could insert new lists in place).

        newColList = []
        # We need to pad any columns that are not the same size to be of the same size.
        # To do this, we use the default value.
        for column in columnList:
            if isinstance(column, str):
                # This is a column wide value so we create a list with only this value across the whole thing.
                tempList = [column] * (rowNumber)
            elif len(column) < rowNumber:
                # This particular column is a bit on the small side, so we need to pad it.
                tempList = column + default * (rowNumber - len(column))
            else:
                tempList = list(column)
            newColList.append(tempList)

        # Now we take our list of columns and turn it into a list of rows.
        # http://stackoverflow.com/q/6473679
        rowList = list(zip(*newColList))
        return rowList
Ejemplo n.º 2
0
    def rowByRow(self, rowList):
        """Given a list of rows, this will return a list of padded, evenly sized rows."""
        tableDimensions = sizeGen(rowList)
        rowNumber = tableDimensions[0]
        columnNumber = tableDimensions[1][0]
        default = [self.default]

        newRowList = []

        # We need to pad any rows that are not the same size to be of the same size.
        # To do this, we use the default value.
        for row in rowList:
            if isinstance(row, str):
                # This is a row wide value so we create a list with only this value across the whole thing.
                tempList = [row] * (columnNumber)
            elif len(row) < columnNumber:
                # This particular row is a bit on the small side, so we need to pad it.
                tempList = row + default * (columnNumber - len(row))
            else:
                tempList = list(row)
            newRowList.append(tempList)

        return newRowList