def test_table_align(self): table = y.format_table(self.data, template=t_template, align="lclr") self.assertEqual( table, """\ .---------------------------------------------------------------------. | exp 0 | exp 1 | exp 2 | exp 3 | exp 4 | exp 5 | exp 6 | exp 7 | |=====================================================================| | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 3 | 9 | 27 | 81 | 243 | 729 | 2187 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 4 | 16 | 64 | 256 | 1024 | 4096 | 16384 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 5 | 25 | 125 | 625 | 3125 | 15625 | 78125 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 6 | 36 | 216 | 1296 | 7776 | 46656 | 279936 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 7 | 49 | 343 | 2401 | 16807 | 117649 | 823543 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 8 | 64 | 512 | 4096 | 32768 | 262144 | 2097152 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 9 | 81 | 729 | 6561 | 59049 | 531441 | 4782969 | |-------+-------+-------+-------+-------+--------+---------+----------| | 1 | 10 | 100 | 1000 | 10000 | 100000 | 1000000 | 10000000 | .---------------------------------------------------------------------.""")
def test_template_align_comma_b(self): """Test of stored template.""" with self.assertRaises(ValueError) as context: table = y.format_table(data3, "minimal", align="cr*,n,c") self.assertEqual( str(context.exception), "more than 2 comma-separated align strings: 'cr*,n,c'")
def test_template_line_too_long(self): with self.assertRaises(ValueError) as ectx: table = y.format_table(data=data3, align=3, template=t_line_too_long, cell_pad=None) self.assertEqual(ectx.exception.args, ("template line 4 must be 7 chars long, but is 8", ))
def test_cell_pad_too_long_seq(self): with self.assertRaises(ValueError) as ectx: table = y.format_table(data=data3, align="c*,", template=t_columns, cell_pad=[1, 1, 1, 4]) self.assertEqual(ectx.exception.args, ("cell_pad is not a sequence len 1 or 2, but 4", ))
def test_tformat1(self): table = y.format_table(data=data2, template=tformat1, align="crr") self.assertEqual( table, """\ & | False True -------+-------------- False | False False True | False True""")
def test_cell_pad_non_int_seq(self): with self.assertRaises(ValueError) as ectx: table = y.format_table(data=data3, align="c*,", template=t_columns, cell_pad=["", ""]) self.assertEqual(ectx.exception.args, ("cell_pad is not a sequence of int, but ['', '']", ))
def test_example_data(self): table = y.format_table(cell_pad=[0, 1]) self.assertEqual( table, """\ (O:O) col 1 col 2 col 3 row 1 cell 12 cell 13 cell 14 row 2 cell 22 cell 23 cell 24 row 3 cell 32 cell 33 cell 34""")
def test_template_too_few_lines(self): with self.assertRaises(ValueError) as ectx: table = y.format_table(data=data3, align=3, template=t_too_few_lines, cell_pad=None) self.assertEqual(ectx.exception.args, ("template must have 7 lines, not 6", ))
def test_align_wrong_type(self): with self.assertRaises(ValueError) as ectx: table = y.format_table(data=data3, align=3, template=t_columns, cell_pad=None) self.assertEqual(ectx.exception.args, ( "align must be a string of one or two comma-separated fields, but is 3", ))
def test_cell_pad_wrong_type(self): with self.assertRaises(ValueError) as ectx: table = y.format_table(data=data3, align="c*,", template=t_columns, cell_pad="") self.assertEqual( ectx.exception.args, ("cell_pad is not None or int or seq of 2 ints: ''", ))
def test_template_minimal(self): """Test of template "minimal".""" table = y.format_table(data3, "minimal", align="cr*,") self.assertEqual( table, """\ * 10 100 1000 4 40 400 4000 27 270 2700 27000 3125 31250 312500 3125000 823543 8235430 82354300 823543000""")
def test_tformat0a(self): self.assertEqual( y.format_table(data=data2, template=tformat0, align="cc*", cell_pad=[0]), """\ & False True ----------------- False False False True False True""")
def test_tformat2(self): table = y.format_table(data=data2, template=tformat2, align="cll,") self.assertEqual( table, r"""/-----------------------\ | & : False | True | |=======:===============| | False : False | False | |-------:---------------+ | True : False | True | \-----------------------/""")
def test_columns(self): table = y.format_table(data=data3, align="c*,", template=t_columns) self.assertEqual( table, """\ * | 10 | 100 | 1000 ========================================= 4 | 40 | 400 | 4000 27 | 270 | 2700 | 27000 3125 | 31250 | 312500 | 3125000 823543 | 8235430 | 82354300 | 823543000""")
def test_template_markdown(self): """Test of template "markdown".""" table = y.format_table(data3, "markdown", align="cr*,") self.assertEqual( table, """\ | * | 10 | 100 | 1000 | |--------|---------|----------|-----------| | 4 | 40 | 400 | 4000 | | 27 | 270 | 2700 | 27000 | | 3125 | 31250 | 312500 | 3125000 | | 823543 | 8235430 | 82354300 | 823543000 |""")
def test_template_columns(self): """Test of template "columns".""" table = y.format_table(data3, "columns", align="cr*,") self.assertEqual( table, """\ * | 10 | 100 | 1000 --------|---------|----------|----------- 4 | 40 | 400 | 4000 27 | 270 | 2700 | 27000 3125 | 31250 | 312500 | 3125000 823543 | 8235430 | 82354300 | 823543000""")
def test_columns_d(self): """Test fully blank template lines.""" table = y.format_table(data=data3, align="cr*,", template=t_columns_d) self.assertEqual( table, """\ * 10 100 1000 4 40 400 4000 27 270 2700 27000 3125 31250 312500 3125000 823543 8235430 82354300 823543000""")
def test_tformat0b(self): table = y.format_table(data=data2, template=tformat0, align="cc*", cell_pad=0) self.assertEqual( table, """\ & False True ----------------- False False False True False True""")
def test_columns_c(self): """Test fully blank template lines.""" table = y.format_table(data=data3, align="c*,", template=t_columns_c) self.assertEqual( table, """\ * | 10 | 100 | 1000 ========================================= 4 | 40 | 400 | 4000 27 | 270 | 2700 | 27000 3125 | 31250 | 312500 | 3125000 823543 | 8235430 | 82354300 | 823543000""")
def test_template_align_comma_a(self): """Test of stored template.""" y.table.store_template("stored 1", t_storethis) table = y.format_table(data3, "stored 1", align="cr*,n") self.assertEqual( table, """\ * | 10 | 100 | 1000 4 | 40 | 400 | 4000 27 | 270 | 2700 | 27000 3125 | 31250 | 312500 | 3125000 823543 | 8235430 | 82354300 | 823543000""") self.assertTrue("stored 1" in y.table.template_names()) y.table.remove_template("stored 1")
def test_columns_c_indent(self): """Test fully blank template lines.""" table = y.format_table(data=data3, align="c*,", template=t_columns_c, indent="huhu") self.assertEqual( table, """\ huhu huhu * | 10 | 100 | 1000 huhu========================================= huhu 4 | 40 | 400 | 4000 huhu 27 | 270 | 2700 | 27000 huhu 3125 | 31250 | 312500 | 3125000 huhu 823543 | 8235430 | 82354300 | 823543000""")
def test_abc(self): table = y.format_table(data=data3, align="c*,", template=t_abc) self.assertEqual( table, """\ A--------v---------.----------.-----------B | * : 10 $ 100 $ 1000 ! >========+=========,==========,===========< | 4 : 40 $ 400 $ 4000 ! @--------*---------+----------+-----------/ | 27 : 270 $ 2700 $ 27000 ! @--------*---------+----------+-----------/ | 3125 : 31250 $ 312500 $ 3125000 ! @--------*---------+----------+-----------/ | 823543 : 8235430 $ 82354300 $ 823543000 ! C________^_________∆__________∆___________D""")
def test_template_full(self): """Test of template "full".""" table = y.format_table(data3, "full", align="cr*,") self.assertEqual( table, """\ .-----------------------------------------. | * | 10 | 100 | 1000 | |=========================================| | 4 | 40 | 400 | 4000 | |--------+---------+----------+-----------| | 27 | 270 | 2700 | 27000 | |--------+---------+----------+-----------| | 3125 | 31250 | 312500 | 3125000 | |--------+---------+----------+-----------| | 823543 | 8235430 | 82354300 | 823543000 | .-----------------------------------------.""")
def test_template_box(self): """Test of template "box".""" table = y.format_table(data3, "box", align="cr*,") self.assertEqual( table, """\ ╒════════╦═════════╤══════════╤═══════════╕ │ * ║ 10 │ 100 │ 1000 │ ╞════════╬═════════╪══════════╪═══════════╡ │ 4 ║ 40 │ 400 │ 4000 │ ├────────╫─────────┼──────────┼───────────┤ │ 27 ║ 270 │ 2700 │ 27000 │ ├────────╫─────────┼──────────┼───────────┤ │ 3125 ║ 31250 │ 312500 │ 3125000 │ ├────────╫─────────┼──────────┼───────────┤ │ 823543 ║ 8235430 │ 82354300 │ 823543000 │ ╘════════╩═════════╧══════════╧═══════════╛""")
def test_unknown_template(self): with self.assertRaises(KeyError) as ctx: table = y.format_table(template_name="dunno") self.assertEqual(ctx.exception.args[0], "not a valid template name: 'dunno'")
# multiplication table as in issue #23 import copy import jpylib as y datadim = 11 data = [["*", *range(datadim)]] for i in range(datadim): data.append([i, *range(datadim)]) for j in range(datadim): i_ = i + 1 j_ = j + 1 data[i_][j_] = i * j # print(f"data[{i_}][{j_}] = {data[i_][j_]}") # for i in range(datadim): # print(i, ":", data[i]) template = """ 0000000 0 | 0 0 0-+-0-0 0 | 0 0 0000000 0 | 0 0 0000000""" print(y.format_table(data=data, align="rn*,n*", indent=" ", template=template))
#!/usr/bin/env python3 # print the table templates with examples import jpylib as y for name in y.table.template_names(): print() example = y.format_table(template_name=name, align="c*,") print("{}\n{}\n\n{}".format(name, len(name) * "-", example)) print()
def test_invalid_align(self): with self.assertRaises(ValueError): result = y.format_table(data=self.data, align="lc*lr")