def test_unequal_tables(self): A = table_literal(""" | A (int) | B (int) | | 1 | 2 | """) B = table_literal(""" | A (int) | B (int) | | 2 | 3 | """) with self.assertRaises(AssertionError): self.assertTablesEqual(A, B)
def test_assert_equal_any_order(self): A = table_literal(""" | A (int) | B (int) | D (float) | | 1 | 2 | 1.1 | | 1 | 2 | 2.2 | """) B = table_literal(""" | A (int) | B (int) | D (float) | | 1 | 2 | 2.2 | | 1 | 2 | 1.1 | """) self.assertTablesEqualAnyOrder(A, B)
def test_assert_equal_any_order(self): A = table_literal(""" | A (int) | B (int) | D (float) | | 1 | 2 | 1.1 | | 1 | 2 | 9.9 | """) B = table_literal(""" | A (int) | B (int) | D (float) | | 1 | 2 | 2.2 | | 1 | 2 | 1.1 | """) with self.assertRaises(AssertionError): self.assertTablesEqualAnyOrder(A, B)
def test_inconsitent_column_types(self): A = table_literal(""" | A (int) | B (int) | D (float) | | 1 | 2 | 2.2 | """) B = table_literal(""" | A (int) | B (int) | D (str) | """) with self.assertRaises(AssertionError) as ae: self.assertTablesEqual(A, B) self.assertTrue( "Column types are different" in ae.exception.args[0] )
def simple_aggregate(self): expected = table_literal(""" | Pokemon (str) | Attack Type (str) | Count (int) | | Pikachu | Normal | 4 | | Pikachu | Electric | 3 | | Pikachu | Fairy | 2 | """) agg = self.t.aggregate( keys=('Pokemon', 'Attack Type'), aggregations=[ ('Count', int, lambda t:len(t)) ] ) self.assertEquals( agg.column_names, ['Pokemon', 'Attack Type', 'Count']) self.assertEquals( agg.column_types, [str, str, int] ) self.assertTablesEqualAnyOrder( agg, expected )
def test_error_unequal_lengths(self): A = table_literal(""" | A (int) | B (int) | | 1 | 2 | """) B = table_literal(""" | A (int) | B (int) | """) with self.assertRaises(AssertionError) as ae: self.assertTablesEqual(A, B) self.assertTrue( "Table lengths are different" in ae.exception.args[0] )
def test_row_can_be_accessed_like_a_tuple(self): t = table_literal(""" | A (int) | B (str) | | 3 | hello | """) r = t[0] self.assertEquals(r[0], 3) self.assertEquals(r[1], 'hello')
def test_row_can_be_accessed_by_names(self): t = table_literal(""" | A (int) | B (str) | | 3 | hello | """) r = t[0] self.assertEquals(r['A'], 3) self.assertEquals(r['B'], 'hello')
def test_row_keyerror(self): t = table_literal(""" | A (int) | B (str) | | 3 | hello | """) r = t[0] with self.assertRaises(KeyError): r['X']
def test_row_attributerror(self): t = table_literal(""" | A (int) | B (str) | | 3 | hello | """) r = t[0] with self.assertRaises(AttributeError): r.X
def test_foo(self): t0 = table_literal( """ | A (int) | B (int) | C (str) | | 1 | 2 | 3 | | 2 | 3 | 4 | """ ) t1 = table_literal( """ | A (int) | B (int) | C (str) | | 1 | 2 | 3 | | 999 | 3 | 4 | """ ) self.assertTablesEqual(t0, t1)
def test_table_literal_columns_can_have_whitespace(self): t = table_literal(""" | Attack Type (str) | Special Def (int) | | Fire | 2 | """) self.assertEquals( t.column_names, ['Attack Type', 'Special Def'] )
def test_table_literal_one_row(self): t = table_literal(""" | A | B | C | D | | 1 | 2 | 3 | 4 | """, default_type=int) self.assertEquals( t.column_names, ['A', 'B', 'C', 'D'] )
def test_equal_tables(self): A = table_literal(""" | A (int) | B (int) | | 1 | 2 | """) self.assertTablesEqual(A, A) self.assertTablesEquals(A, A) with self.assertRaises(AssertionError): self.assertTablesNotEquals(A, A)
def test_values(self): t = table_literal(""" | A (i) | B (str) | | 3 | hello | """) r = t[0] self.assertEquals( r.values(), [3, 'hello'] )
def test_keys_on_array_columns(self): t = table_literal(""" | A (int) | B (str) | | 3 | hello | """) r = t[0] self.assertEquals( r.keys(), ('A', 'B') )
def test_row_can_be_cast_to_tuple(self): t = table_literal(""" | A (int) | B (str) | | 3 | hello | """) r = t[0] self.assertEquals( tuple(r), (3, "hello") )
def test_keys_on_regular_columns(self): t = table_literal(""" | A (i) | B (f) | | 3 | 2.2 | """) r = t[0] self.assertEquals( r.keys(), ('A', 'B') )
def test_whole_of_normalized_table(self): tn = self.t.normalize({"B": 1.0}) expected = table_literal( """ | A (int) | B (float) | C (str) | | 1 | 0 | x | | 2 | 0.5 | y | | 3 | 1.0 | z | """ )
def test_items(self): t = table_literal(""" | A (i) | B (str) | | 3 | hello | """) r = t[0] self.assertEquals( list(r.items()), [('A', 3), ('B', 'hello')] )
def test_detect_first_dfferent_row(self): A = table_literal(""" | A (int) | B (int) | D (float) | | 1 | 2 | 2.2 | | 1 | 2 | 2.1 | """) B = table_literal(""" | A (int) | B (int) | D (float) | | 1 | 2 | 2.2 | | 1 | 2 | 2.3 | """) with self.assertRaises(AssertionError) as ae: self.assertTablesEqual(A, B) self.assertTrue( "Differences at row 1" in ae.exception.args[0], ae.exception.args[0] )
def setUp(self): self.t = table_literal(""" | Attack (str) | Pokemon (str) | Level Obtained (int) | Attack Type (str) | | Thunder Shock | Pikachu | 1 | Electric | | Tackle | Pikachu | 1 | Normal | | Tail Whip | Pikachu | 1 | Normal | | Growl | Pikachu | 5 | Normal | | Quick Attack | Pikachu | 10 | Normal | | Thunder Wave | Pikachu | 13 | Electric | | Electro Ball | Pikachu | 18 | Electric | | Charm | Pikachu | 0 | Fairy | | Sweet Kiss | Pikachu | 0 | Fairy | """)
def test_whole_of_normalized_table_100(self): tn = self.t.normalize({"B": 100}) expected = table_literal( """ | A (int) | B (float) | C (str) | | 1 | 0 | x | | 2 | 50 | y | | 3 | 100 | z | """ ) self.assertTablesEqual(tn, expected)
def test_table_literal_one_row_with_types(self): t = table_literal(""" | A (int) | B (float) | C (bool) | D (int) | | 1 | 2.2 | True | 4 | """) self.assertEquals( t.column_names, ['A', 'B', 'C', 'D'] ) self.assertEquals( t[0], (1, 2.2, True, 4) )
def test_expand_of_normalized_table(self): tn = self.t.normalize({"B": 1.0}).expand( name="D", col_type=float, input_columns=["A", "B"], fn=lambda A, B: A * B ) expected = table_literal( """ | A (int) | B (float) | C (str) | D (float) | | 1 | 0 | x | 0.0 | | 2 | 0.5 | y | 1.0 | | 3 | 1.0 | z | 3.0 | """ ) self.assertTablesEqual(tn, expected)
def test_inner_join_simple(self): j = self.pokedex.inner_join( keys=('pokemon',), other=self.types ) expected = table_literal(""" | pokemon (str) | owner (str) | level (i) | type (str) | | Charmander | Ash | 12 | Fire | | Togepi | Misty | 5 | Fairy | | Onyx | Brock | 22 | Rock | | Meowth | Team Rocket | 22 | Normal | """) self.assertTablesEqual( j.copy(), expected )
def test_get_key_and_subtable(self): agg = self.t.aggregate( keys=('Pokemon', 'Attack Type'), aggregations=[ ('Count', int, lambda t:len(t)) ] ) gen = agg._iter_subtables() expected = table_literal(""" | Attack (str) | Pokemon (str) | Level Obtained (int) | Attack Type (str) | | Tackle | Pikachu | 1 | Normal | | Tail Whip | Pikachu | 1 | Normal | | Growl | Pikachu | 5 | Normal | | Quick Attack | Pikachu | 10 | Normal | """ ) for (k, st) in gen: if k == ('Pikachu', 'Normal'): st = st.copy() self.assertTablesEqualAnyOrder(st, expected)
def test_table_literal_one_row(self): t = table_literal(""" | A | B | C | | 1 | 2 | 3 | """, default_type=int) self.assertIsInstance(t, Table)
from toytable import table_literal pokedex = table_literal(""" | Pokemon (str) | Level (int) | Owner (str) | Type (str) | | Pikchu | 12 | Ash Ketchum | Electric | | Bulbasaur | 16 | Ash Ketchum | Grass | | Charmander | 19 | Ash Ketchum | Fire | | Raichu | 23 | Lt. Surge | Electric | """) print pokedex.column_names print pokedex.column_types print list(pokedex.Pokemon)
from toytable import table_literal, Table types = Table([('Type Id', 'i'), ('Type', str)]) types.append([1, 'water']) types.append([2, 'bug']) t = table_literal(""" | Pokedex Number (i) | Pokemon (str) | Type Id (i) | | 7 | Squirtle | 1 | | 8 | Wartortle | 1 | | 9 | Blastoise | 1 | | 10 | Caterpie | 2 | | 11 | Metapod | 2 | | 12 | Butterfree | 2 | """) pokedex = t.left_join( keys=('Type Id',), other=types ) print pokedex table0 = table_literal(""" | foo (c) | bar (i) | baz (f) | | x | 2 | 2.2 | """) table1 = Table([('foo', 'c'), ('bar', 'i'), ('baz', 'f')]) table1.append(['x', 2, 2.2]) assert table0 == table1