예제 #1
0
	def test_csv01(self):
		mydata = [[11,12],[21,22]]
		myheaders = [ "Column 1", "Column 2" ]
		mystubs = [ "Row 1", "Row 2" ]
		tbl = SimpleTable(mydata, myheaders, mystubs, title="Title")
		actual = '%s' % tbl.as_csv().strip()
		desired = """
         Title         
     ,Column 1,Column 2
Row 1,11      ,12      
Row 2,21      ,22      
""".strip()
		self.assertEqual(actual, desired)
예제 #2
0
    def test_csv01(self):
        mydata = [[11, 12], [21, 22]]
        myheaders = ["Column 1", "Column 2"]
        mystubs = ["Row 1", "Row 2"]
        tbl = SimpleTable(mydata, myheaders, mystubs, title="Title")
        actual = '%s' % tbl.as_csv().strip()
        desired = """
         Title         
     ,Column 1,Column 2
Row 1,11      ,12      
Row 2,21      ,22      
""".strip()
        self.assertEqual(actual, desired)
예제 #3
0
def _df_to_simpletable(df,
                       align='r',
                       float_format="%.4f",
                       header=True,
                       index=True,
                       table_dec_above='-',
                       table_dec_below=None,
                       header_dec_below='-',
                       pad_col=0,
                       pad_index=0):
    dat = df.copy()
    dat = dat.applymap(lambda x: _formatter(x, float_format))
    if header:
        headers = [str(x) for x in dat.columns.tolist()]
    else:
        headers = None
    if index:
        stubs = [str(x) + int(pad_index) * ' ' for x in dat.index.tolist()]
    else:
        dat.ix[:, 0] = [str(x) + int(pad_index) * ' ' for x in dat.ix[:, 0]]
        stubs = None
    st = SimpleTable(np.array(dat),
                     headers=headers,
                     stubs=stubs,
                     ltx_fmt=fmt_latex,
                     txt_fmt=fmt_txt)
    st.output_formats['latex']['data_aligns'] = align
    st.output_formats['txt']['data_aligns'] = align
    st.output_formats['txt']['table_dec_above'] = table_dec_above
    st.output_formats['txt']['table_dec_below'] = table_dec_below
    st.output_formats['txt']['header_dec_below'] = header_dec_below
    st.output_formats['txt']['colsep'] = ' ' * int(pad_col + 1)
    return st
예제 #4
0
def _df_to_simpletable(
    df,
    align="r",
    float_format="%.4f",
    header=True,
    index=True,
    table_dec_above="-",
    table_dec_below=None,
    header_dec_below="-",
    pad_col=0,
    pad_index=0,
):
    dat = df.copy()
    dat = dat.applymap(lambda x: _formatter(x, float_format))
    if header:
        headers = [str(x) for x in dat.columns.tolist()]
    else:
        headers = None
    if index:
        stubs = [str(x) + int(pad_index) * " " for x in dat.index.tolist()]
    else:
        dat.ix[:, 0] = [str(x) + int(pad_index) * " " for x in dat.ix[:, 0]]
        stubs = None
    st = SimpleTable(np.array(dat), headers=headers, stubs=stubs, ltx_fmt=fmt_latex, txt_fmt=fmt_txt)
    st.output_formats["latex"]["data_aligns"] = align
    st.output_formats["txt"]["data_aligns"] = align
    st.output_formats["txt"]["table_dec_above"] = table_dec_above
    st.output_formats["txt"]["table_dec_below"] = table_dec_below
    st.output_formats["txt"]["header_dec_below"] = header_dec_below
    st.output_formats["txt"]["colsep"] = " " * int(pad_col + 1)
    return st
예제 #5
0
	def test_customlabel(self):
		"""Limited test of custom custom labeling"""
		if has_numpy:
			tbl = SimpleTable(table1data, test1header, test1stubs, txt_fmt=txt_fmt1)
			tbl[1][1].data = np.nan
			tbl.label_cells(custom_labeller)
			print([[c.datatype for c in row] for row in tbl])
			desired = """
*****************************
*       * header1 * header2 *
*****************************
* stub1 *    --   *       1 *
* stub2 *    2.00 *       3 *
*****************************
"""
			actual = '\n%s\n' % tbl.as_text(missing='--')
			print(actual)
			print(desired)
			self.assertEqual(actual, desired)
예제 #6
0
    def test_customlabel(self):
        """Limited test of custom custom labeling"""
        if has_numpy:
            tbl = SimpleTable(table1data,
                              test1header,
                              test1stubs,
                              txt_fmt=txt_fmt1)
            tbl[1][1].data = np.nan
            tbl.label_cells(custom_labeller)
            print([[c.datatype for c in row] for row in tbl])
            desired = """
*****************************
*       * header1 * header2 *
*****************************
* stub1 *    --   *       1 *
* stub2 *    2.00 *       3 *
*****************************
"""
            actual = '\n%s\n' % tbl.as_text(missing='--')
            print(actual)
            print(desired)
            self.assertEqual(actual, desired)
예제 #7
0
                title_align='r',
                header_align='r',
                data_aligns="r",
                stubs_align="l",
                fmt='txt')
cell0data = 0.0000
cell1data = 1
row0data = [cell0data, cell1data]
row1data = [2, 3.333]
table1data = [row0data, row1data]
test1stubs = ('stub1', 'stub2')
test1header = ('header1', 'header2')
#test1header = ('header1\nheader1a', 'header2\nheader2a')
tbl = SimpleTable(table1data,
                  test1header,
                  test1stubs,
                  txt_fmt=txt_fmt1,
                  ltx_fmt=ltx_fmt1,
                  html_fmt=html_fmt1)


def custom_labeller(cell):
    if cell.data is np.nan:
        return 'missing'


class test_Cell(unittest.TestCase):
    def test_celldata(self):
        celldata = cell0data, cell1data, row1data[0], row1data[1]
        cells = [
            Cell(datum, datatype=i % 2) for i, datum in enumerate(celldata)
        ]