def test_row_append_combinations(self): first_tbl = [ Tabel(), Tabel({ 'a': 2, 'b': 3 }), Tabel({ 'a': "ff", 'b': "ffr" }) ] seccond_tbl = [{ 'a': 22, 'b': 13 }, [22, 33], (44, 55), np.array([33, 33]), { 'a': "ded", 'b': "fr" }, ["frd", "werf"], ("efds", "dew"), np.array(["frde", "frtg"])] for ftbl, stbl in product(first_tbl, seccond_tbl): ftbl = deepcopy(ftbl) fl = len(ftbl) ftbl.row_append(stbl) assert ftbl.valid assert len(ftbl) == fl + 1
def test_inner_int_join(self): tbl = Tabel({"a": list(range(4)), "b": ['a', 'b'] * 2}) tbl_b = Tabel({"a": list(range(2, 6)), "c": ['d', 'e'] * 2}) tbl_j = tbl.join(tbl_b, "a") assert "c_r" in tbl_j.columns assert tbl_j[0] == (2, 'a', 'd') assert len(tbl_j) == 2 assert np.all(tbl[0] == (0, 'a'))
def test_inner_str_join(self): tbl = Tabel({"a": list(map(str, range(4))), "b": ['a', 'b'] * 2}) tbl_b = Tabel({"a": list(map(str, range(4))), "c": ['d', 'e'] * 2}) tbl_j = tbl.join(tbl_b, "a", jointype="inner") assert "c_r" in tbl_j.columns assert tbl_j[0] == ('0', 'a', 'd') assert len(tbl_j) == 4 assert np.all(tbl[0] == ('0', 'a'))
def test_outer_join(self): tbl = Tabel({"a":list(range(4)), "b": ['a','b'] *2}) tbl_b = Tabel({"a":list(range(2,6)), "c": [1,2] *2, "d":[1.1,2.2]*2}) tbl_j = tbl.join(tbl_b, "a", jointype='outer') naneq(tbl_j[tbl_j['a_l']==0,'c_r'], Tabel.join_fill_value['integer']) naneq(tbl_j[tbl_j['a_l']==0, 'd_r'], Tabel.join_fill_value['float']) naneq(tbl_j[tbl_j['a_r']==4, 'b_l'], Tabel.join_fill_value['string']) assert "c_r" in tbl_j.columns assert len(tbl_j) == 6
def test_pandas_b_and_f(self): data = {'a':[12]*6, 'b':np.arange(6), 'c':list(map(str, range(6)))} df = pd.DataFrame(data) tbl = Tabel(df) for k in data.keys(): assert np.all(tbl.dict['a'] == data['a']) tbl = Tabel(data) df = pd.DataFrame(tbl.dict) for k in data.keys(): assert np.all(df['a'] == data['a'])
def test_group_by(self): tbl = Tabel({'a':[10,20,30, 40]*3, 'b':["100","200"]*6, 'c':[100,200]*6}) tbl_g = tbl.group_by(['b','a'], [(np.sum, 'a'), (first, 'c')]) idx = np.argsort(tbl_g['a_sum']) assert np.all(tbl_g[idx, 'a_sum'] == np.array([30, 60, 90, 120])) assert np.all(tbl['a'] == [10,20,30, 40]*3) assert np.all(tbl_g[idx, 'c_first'] == np.array([100, 200, 100, 200])) tbl_g = tbl.group_by(['b','a'], []) idx = np.argsort(tbl_g['a']) assert np.all(tbl_g[idx, 'a'] == [10,20,30,40])
def test_dtype_properties(self): tbl = Tabel({ "a": list(range(2, 6)), "c": [u'1', u'2'] * 2, "d": [1.1, 2.2] * 2 }) assert tbl.dtype == np.dtype([('a', '<i8'), ('c', '<U1'), ('d', '<f8')]) tbl_n = tbl.astype([np.float, np.int16, np.dtype('<U10')]) assert tbl_n.dtype == np.dtype([('a', '<f8'), ('c', '<i2'), ('d', '<U10')])
def test_append_combinations(self): first_tbl = [Tabel(), Tabel({'a':2,'b':3}), Tabel({'a':"ff",'b':"ffr"})] seccond_tbl = [Tabel({'a':22,'b':13}), pd.DataFrame({'a':[22],'b':[13]}), Tabel({'a':"ff",'b':"ffr"}), pd.DataFrame({'a':["jdw"],'b':["okdw"]}), ] for ftbl, stbl in product(first_tbl, seccond_tbl): ftbl = deepcopy(ftbl) fl = len(ftbl) ftbl.append(stbl) assert ftbl.valid assert len(ftbl) == fl + 1
def test_transpose(self, tbls): for tbl in tbls: if len(tbl) == 0: continue lrec = [tbl[i] for i in range(len(tbl))] tbl_n = Tabel(T(lrec)) for i, column in enumerate(tbl.columns): assert np.all(tbl[column] == tbl_n[str(i)]) lrec = [list(tbl[i]) for i in range(len(tbl))] tbl_n = Tabel(T(lrec)) for i, column in enumerate(tbl.columns): assert np.all(tbl[column] == tbl_n[str(i)])
def test_del_row_slice(self): tbl1 = Tabel({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl2 = Tabel({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) del tbl1[1:3] assert len(tbl1) == 2 assert tbl1[0] == tbl2[0] assert tbl1[1] == tbl2[3]
def test_del_col(self): tbl1 = Tabel({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl2 = Tabel({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) del tbl1['b'] assert tbl1.columns == ['a', 'c'] assert all(tbl1['a'] == tbl2['a']) assert all(tbl1['c'] == tbl2['c'])
def test_add_column(self): tbl = Tabel({"a":list(range(2,6)), "b": ['1','2'] *2, "c":[1.1,2.2]*2}) tbl["d"] = np.array([True]*len(tbl)) tbl["e"] = 100 tbl["f"] = "Blanc" assert len(tbl.columns) == 6 assert np.all([len(c)==len(tbl) for c in tbl.data])
def delay(self,dt): self.twis.connect_to_server("192.168.4.1") self.twis.start_looping=True self.twis.bind(status=self.on_twis_status) self.twis.print_data=self.on_twis_data_masuk self.list_request=[ self.his_cmd, self.main_cmd, self.act_cmd, self.oxi_cmd, self.main_cmd, self.act_cmd, self.oxi_cmd, self.main_cmd, self.cur_cmd, self.main_cmd, ] self.chart=Grafik() self.tabel=Tabel() self.Wg={"DashBoard":self.dash, "Chart":self.chart, "List":self.tabel, "Clear_DTC":self.clear_dtc } self.G=self.dash.G Clock.schedule_once(self.delay2,1)
def test_set_row(self): tbl = Tabel({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl[0, :] = (12, "12", 12.12) tbl[1, ['a', 'b']] = [12, "12"] tbl[2] = (13, "13", 13.13) tbl[3, np.array([True, False, True])] = (14, 14.14)
def test_del_row_int(self): tbl1 = Tabel({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl2 = Tabel({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) del tbl1[0] assert len(tbl1) == 3 for i in range(len(tbl1)): assert tbl1[i] == tbl2[i + 1] del tbl1[-1] assert len(tbl1) == 2 assert tbl1[0] == tbl2[1] assert tbl1[1] == tbl2[2]
def test_sort(self): tbl = Tabel({'aa': ['b', 'g', 'd'], 'b': list(range(3))}) tbl.sort('aa') assert tbl[0] == ('b', 0) assert tbl[1] == ('d', 2) tbl.sort(['aa', 'b']) assert tbl[1] == ('g', 1)
def test_replace_column(self): tbl = Tabel({"a":list(range(2,6)), "b": ['1','2'] *2, "c":[1.1,2.2]*2}) tbl[:,"a"] = np.array([1.4,2.4]*2) assert tbl[0,'a'] == 1 tbl["a"] = np.array([1.1,2.2]*2) assert tbl['a'].dtype == np.float tbl[:,"b"] = np.arange(4) assert tbl[0, "b"] == "0" tbl["b"] = np.arange(4) assert tbl[0, "b"] == 0 tbl[:,"c"] = np.arange(4) assert tbl["c"].dtype == np.float tbl["c"] = np.arange(4) assert tbl["c"].dtype == np.int
def print_table(inventory, order=None): items = [] count = [] if order == None: for i in inventory: items.append(i) count.append(inventory.get(i)) # if order == "count,desc": else: inventory_desc = sorted( inventory.items(), key=lambda t: t[1], reverse=(True if order == "count,desc" else False)) for j in inventory_desc: items.append(j[0]) count.append(j[1]) # elif order == "count,asc": # inventory_asc = sorted( # inventory.items(), key=lambda t: t[1]) # for j in inventory_asc: # items.append(j[0]) # count.append(j[1]) tbl = Tabel([items, count], columns=["Item name", "Count"]) print(tbl)
def tbls(): tbls = [ Tabel({ 'Name': ["John", "Joe", "Jane"], 'Height': [1.82, 1.65, 2.15], 'Married': [False, False, True] }), Tabel([ np.array(["John", "Joe", "Jane"]), np.array([1.82, 1.65, 2.15]), np.array([False, False, True]) ], columns=["Name", "Height", "Married"]), Tabel([["John", "Joe", "Jane"], [1.82, 1.65, 2.15], [False, False, True]], columns=["Name", "Height", "Married"]), Tabel(), Tabel([1, 2, 3]), Tabel({ 'a': range(10), 'b': range(10) }), Tabel({ 'a': range(10), 'b': list(map(str, range(10))) }), Tabel([range(10), range(10), range(10)]), Tabel(np.array([np.arange(10), np.arange(10, 20)])), Tabel({ 'a': 12, 'b': 13 }), Tabel(pd.DataFrame({ 'a': [12] * 6, 'b': np.arange(6), 'c': range(6) })), Tabel({ 'a': [3, 4, 5], 'b': 1 }), Tabel({ 'a': 1, 'b': [3, 4, 5] }), Tabel({ 'aa': [1, 1, 1], 'b': [3, 4, 5] }), ] return tbls
def test_del_col(self): tbl1 = Tabel({"a":list(range(2,6)), "b": ['1','2'] *2, "c":[1.1,2.2]*2})