def testCreateTableFromData(self): """ Test suite for createTbaleFRomData method """ data_names = [ 'intList', 'floatList', 'charList', 'stringList', 'booleanList', 'timeList' ] data_list = [[1, 2, None], [1., 2., None], ['A', 'B', None], [u'one', u'two', None], [True, False, None], [datetime.utcnow(), datetime.utcnow(), datetime.utcnow()]] with self.subTest(msg="createTableFromData with lists"): tab = createTableFromData(data_list, columns=data_names) print("tableFromList = {}\n".format(TableTools.html(tab))) data_dict = {} for nm, da in zip(data_names, data_list): data_dict[nm] = da with self.subTest(msg="createTableFromData with dict"): tab = createTableFromData(data_dict, columns=data_names) print("tableFromDict = {}\n".format(TableTools.html(tab)))
def testListColumnVersion(self): """ Test for behavior when one of the data frame columns contains tuples or lists """ def1 = { ('a', 'b'): { ('A', 'B'): 1, ('A', 'C'): 2 }, ('a', 'a'): { ('A', 'C'): 3, ('A', 'B'): 4 }, ('a', 'c'): { ('A', 'B'): 5, ('A', 'C'): 6 }, ('b', 'a'): { ('A', 'C'): 7, ('A', 'B'): 8 }, ('b', 'b'): { ('A', 'D'): 9, ('A', 'B'): 10 } } dataframe1 = pandas.DataFrame(def1) table1 = dataFrameToTable(dataframe1) print("dataframe1 = \n{}".format(dataframe1)) print("table1 = {}\n".format(TableTools.html(table1))) def2 = { 'one': [(1, 2), (2, 3), (3, ), (4, 5, 6, 7)], 'two': [(4, 5), (6, 5, 3), (7, 6), (8, 7)], 'thing': [None, None, None, None] } dataframe2 = pandas.DataFrame(def2) table2 = dataFrameToTable(dataframe2, convertUnknownToString=True) print("dataframe2 = \n{}".format(dataframe2)) print("table2 = {}\n".format(TableTools.html(table2))) def3 = { 'one': [[1, 2], [2, 3], [3, 4], [4, 5, 6, 7]], 'two': [[4, 5], [6, 5], [7, 6], [8, 7]], 'thing': [None, None, None, None] } dataframe3 = pandas.DataFrame(def3) table3 = dataFrameToTable(dataframe3, convertUnknownToString=True) print("dataframe3 = \n{}".format(dataframe3)) print("table3 = {}\n".format(TableTools.html(table3)))
def testBreakingCases(self): """ Testing some cases observed to fail previously """ tab1, tab2, tab3 = None, None, None with self.subTest(msg="charCol with list"): tab1 = TableTools.newTable( TableTools.charCol('tab1', ['A', 'B', 'C', 'D'])) print("tab1 = \n{}".format(TableTools.html(tab1))) with self.subTest(msg="charCol with string"): tab2 = TableTools.newTable(TableTools.charCol('tab2', 'EFGH')) print("tab2 = \n{}".format(TableTools.html(tab2))) with self.subTest(msg="col with single string"): tab3 = TableTools.newTable(TableTools.col('tab3', 'EFGH')) print("tab3 = \n{}".format(TableTools.html(tab3))) del tab1, tab2, tab3
def test_column(self): t = TableTools.emptyTable(10).view( "I=ii", "J=(ii * 2)").update("K = vectorized_func(I, J)") html_output = TableTools.html(t) self.assertIn("<td>9</td>", html_output)
def test_pyobj_field_access(self): t = TableTools.emptyTable(10) t2 = t.update("SYM = `AAPL-` + (String)pyobj.name", "PRICE = i * 1000").where("PRICE > (int)pyobj.price + 100") html_output = TableTools.html(t2) self.assertIn("AAPL-GOOG", html_output) self.assertIn("2000", html_output)
def testTableBasics(self): """ Test cases for table creation, and a few other basic table methods: diff(), html(), show(), showCommaDelimited(), showWithIndex(), string(), roundDecimalColumns(), roundDecimalColumnsExcept(), merge(), mergeSorted() """ tab, tab2, tab3, tab4, tab5, tab6 = None, None, None, None, None, None with self.subTest(msg="emptyTable(long)"): tab = TableTools.emptyTable(3) # set some cols which aren't dumb tab = tab.update("intCol=(int)i", "fltCol=(float)i*0.5", "dblCol=(double)i*0.3") with self.subTest(msg="newTable(TableDefinition)"): # assuming the first test passed... tab3 = TableTools.newTable(tab.getDefinition()) # Essentially table to string methods with self.subTest(msg="html test"): print("html rendering = \n{}".format(TableTools.html(tab))) with self.subTest(msg="show(Table, *cols)"): print("show =") TableTools.show(tab, "intCol", "dblCol") with self.subTest(msg="show(Table, 2, *cols)"): print("show & row limit =") TableTools.show(tab, 2, "intCol", "dblCol") with self.subTest(msg="showCommaDelimited(Table, *cols)"): print("showCommaDelimited =") TableTools.showCommaDelimited(tab, "intCol", "dblCol") with self.subTest(msg="showCommaDelimited(Table, 2, *cols)"): print("showCommaDelimited & row limit =") TableTools.showCommaDelimited(tab, 2, "intCol", "dblCol") with self.subTest(msg="showWithIndex(Table, *cols)"): print("showWithIndex =") TableTools.showWithIndex(tab, "intCol", "dblCol") with self.subTest(msg="showWithIndex(Table, 2, *cols)"): print("showWithIndex & row limit =") TableTools.showWithIndex(tab, 2, "intCol", "dblCol") with self.subTest(msg="string(Table, *cols)"): print("string =\n {}".format( TableTools.string(tab, "intCol", "dblCol"))) with self.subTest(msg="string(Table, 2, *cols)"): print("string & row limit =\n {}".format( TableTools.string(tab, 2, "intCol", "dblCol"))) with self.subTest(msg="roundDecimalColumns"): tab4 = TableTools.roundDecimalColumns(tab) with self.subTest(msg="roundDecimalColumns(*cols)"): tab5 = TableTools.roundDecimalColumns(tab, "fltCol", "dblCol") with self.subTest(msg="roundDecimalColumnsExcept(*cols)"): tab6 = TableTools.roundDecimalColumns(tab, "fltCol") with self.subTest(msg="diff test of a table with itself"): print("diff output of table with itself = \n{}".format( TableTools.diff(tab, tab, 3))) with self.subTest( msg="diff test of a table with rounded version of itself"): print("diff output of table with rounded version of itself = \n{}". format(TableTools.diff(tab, tab4, 3))) with self.subTest(msg="merge(*tables)"): tab4 = TableTools.merge(tab, tab) with self.subTest(msg="merge([tables])"): tab4 = TableTools.merge([tab, tab]) with self.subTest(msg="mergeSorted(col, [tables])"): tab4 = TableTools.mergeSorted("intCol", [tab, tab]) with self.subTest(msg="merge(col, *tables)"): tab4 = TableTools.mergeSorted("intCol", tab, tab) del tab, tab2, tab3, tab4, tab5, tab6
def testColumnSource(self): """ Test cases for colSource(), objColSource() methods & associated newTable() method """ # type inference does not work for list and dicts (i.e. not converted to java collections & maps) colSources = [] names = [] mapSources = {} with self.subTest(msg="colSource with byte"): key, val = "byte", TableTools.colSource(self.nByteArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with short"): key, val = "short", TableTools.colSource(self.nShortArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with int"): key, val = "int", TableTools.colSource(self.nIntArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with long"): key, val = "long", TableTools.colSource(self.nLongArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with float"): key, val = "float", TableTools.colSource(self.nFloatArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with double"): key, val = "double", TableTools.colSource(self.nDoubleArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with char"): key, val = "char", TableTools.colSource(self.nCharArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with string"): key, val = "string", TableTools.colSource(self.nStringArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with boolean"): key, val = "boolean", TableTools.colSource(self.nBooleanArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="colSource with time"): key, val = "time", TableTools.colSource(self.nTimeArray) names.append(key) colSources.append(val) mapSources[key] = val with self.subTest(msg="newTable with name and column source list"): self.assertGreater(len(colSources), 0) # make sure that we even do something useful print("table from [names], [sources] = \n{}".format( TableTools.html(TableTools.newTable(3, names, colSources)))) with self.subTest(msg="newTable with {name: column source} map"): self.assertGreater(len(mapSources), 0) # make sure that we even do something useful print("table from [names : sources] = \n{}".format( TableTools.html(TableTools.newTable(3, mapSources)))) del names, colSources, mapSources names, colSources = [], [] with self.subTest(msg="colSource with int list"): key, val = "intList", TableTools.colSource(self.intList) names.append(key) colSources.append(val) with self.subTest(msg="colSource with float list"): key, val = "floatList", TableTools.colSource(self.floatList) names.append(key) colSources.append(val) with self.subTest(msg="colSource with string list"): key, val = "strList", TableTools.colSource(self.stringList) names.append(key) colSources.append(val) with self.subTest(msg="colSource with char list"): key, val = "charList", TableTools.colSource(self.charList) names.append(key) colSources.append(val) with self.subTest(msg="colSource with boolean list"): key, val = "boolList", TableTools.colSource(self.booleanList) names.append(key) colSources.append(val) with self.subTest(msg="colSource with time list"): key, val = "time", TableTools.colSource(self.timeList) names.append(key) colSources.append(val) print("table from colSource with lists = \n{}".format( TableTools.html(TableTools.newTable(3, names, colSources)))) del names, colSources names, colSources = [], [] with self.subTest(msg="objColSource with string"): key, val = "string", TableTools.objColSource(self.nStringArray) names.append(key) colSources.append(val) with self.subTest(msg="objColSource with boolean"): key, val = "boolean", TableTools.objColSource(self.nBooleanArray) names.append(key) colSources.append(val) with self.subTest(msg="objColSource with time"): key, val = "time", TableTools.objColSource(self.nTimeArray) names.append(key) colSources.append(val) # NOTE: this one is kinda dumb...probably not what anyone wants with self.subTest(msg="objColSource with double primitive"): key, val = "double", TableTools.objColSource(self.nDoubleArray) names.append(key) colSources.append(val) print("table from objColSource = \n{}".format( TableTools.html(TableTools.newTable(3, names, colSources)))) del names, colSources names, colSources = [], [] with self.subTest(msg="objColSource with string list"): key, val = "string", TableTools.objColSource(self.stringList) names.append(key) colSources.append(val) with self.subTest(msg="objColSource with boolean list"): key, val = "boolean", TableTools.objColSource(self.booleanList) names.append(key) colSources.append(val) with self.subTest(msg="objColSource with time list"): key, val = "time", TableTools.objColSource(self.timeList) names.append(key) colSources.append(val) # NOTE: this one is kinda dumb...probably not what anyone wants with self.subTest(msg="objColSource with float list"): key, val = "double", TableTools.objColSource(self.floatList) names.append(key) colSources.append(val) print("table from objColSource with lists = \n{}".format( TableTools.html(TableTools.newTable(3, names, colSources)))) del names, colSources
def testColumnHolder(self): """ Test cases for <primitive>Col() methods & associated newTable() method """ holders = [] junk, tab = None, None with self.subTest(msg="byteCol"): holders.append(TableTools.byteCol("byteCol", self.nByteArray)) with self.subTest(msg="shortCol"): holders.append(TableTools.shortCol("shortCol", self.nShortArray)) with self.subTest(msg="intCol"): holders.append(TableTools.intCol("intCol", self.nIntArray)) with self.subTest(msg="longCol"): holders.append(TableTools.longCol("longCol", self.nLongArray)) with self.subTest(msg="floatCol"): holders.append(TableTools.floatCol("floatCol", self.floatList)) with self.subTest(msg="doubleCol"): holders.append(TableTools.doubleCol("doubleCol", self.nDoubleArray)) with self.subTest(msg="charCol"): holders.append(TableTools.charCol("charCol", self.nCharArray)) with self.subTest(msg="newTable with column holders"): self.assertGreater(len(holders), 0) # make sure that we even do something useful tab = TableTools.newTable(*holders) print("tab =\n{}".format(TableTools.html(tab))) del holders, tab holders = [] with self.subTest(msg="byteCol with list"): holders.append(TableTools.byteCol("byteList", self.intList)) with self.subTest(msg="shortCol with list"): holders.append(TableTools.shortCol("shortList", self.intList)) with self.subTest(msg="intCol with list"): holders.append(TableTools.intCol("intList", self.intList)) with self.subTest(msg="longCol with list"): holders.append(TableTools.longCol("longList", self.intList)) with self.subTest(msg="floatCol with list"): holders.append(TableTools.doubleCol("floatList", self.floatList)) with self.subTest(msg="doubleCol with list"): holders.append(TableTools.doubleCol("doubleList", self.floatList)) with self.subTest(msg="charCol with list"): holders.append(TableTools.charCol("charList", self.charList)) print('prim col from list = \n{}'.format( TableTools.html(TableTools.newTable(*holders)))) del holders holders = [] with self.subTest(msg="col with string array"): holders.append(TableTools.col("stringCol", self.nStringArray)) with self.subTest(msg="col with boolean array"): holders.append(TableTools.col("booleanCol", self.nBooleanArray)) with self.subTest(msg="col with time array"): holders.append(TableTools.col("timeCol", self.nTimeArray)) print('obj col = \n{}'.format( TableTools.html(TableTools.newTable(*holders)))) del holders holders = [] with self.subTest(msg="col with int list"): holders.append(TableTools.col("intList2", self.intList)) with self.subTest(msg="col with double list"): holders.append(TableTools.col("doubleList2", self.floatList)) with self.subTest(msg="col with char list"): holders.append(TableTools.col("stringList", self.stringList)) with self.subTest(msg="col with char list"): holders.append(TableTools.col("charList2", self.charList)) with self.subTest(msg="col with boolean list"): holders.append(TableTools.col("booleanList", self.booleanList)) with self.subTest(msg="col with time list"): holders.append(TableTools.col("timeList", self.timeList)) print('col from list = \n{}'.format( TableTools.html(TableTools.newTable(*holders)))) del holders holders = [] with self.subTest(msg="col with byte"): holders.append(TableTools.col("byteCol", self.nByteArray)) with self.subTest(msg="col with short"): holders.append(TableTools.col("shortCol", self.nShortArray)) with self.subTest(msg="col with int"): holders.append(TableTools.col("intCol", self.nIntArray)) with self.subTest(msg="col with long"): holders.append(TableTools.col("longCol", self.nLongArray)) with self.subTest(msg="col with float"): holders.append(TableTools.col("floatCol", self.nFloatArray)) with self.subTest(msg="col with double"): holders.append(TableTools.col("doubleCol", self.nDoubleArray)) with self.subTest(msg="col with char"): holders.append(TableTools.col("charCol", self.nCharArray)) print("primitive from col =\n{}".format( TableTools.html(TableTools.newTable(*holders)))) del holders
def test_filter(self): t = TableTools.emptyTable(10).view( "I=ii", "J=(ii * 2)").where("vectorized_func(I, J)") html_output = TableTools.html(t) self.assertIn("<td>5</td><td>10</td>", html_output)