def test_repr_joined_table(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [99, 'Mew', 43], # Cannot be joined ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) expected = [ "| Owner Id (int) | Pokemon | Level (int) | Name (str) |", "| 1 | Pikachu | 18 | Ash Ketchum |", "| 99 | Mew | 43 | None |" ] for resultrow, expectedrow in zip( repr(j).split('\n'), expected): self.assertEqual( resultrow, expectedrow )
def test_nones_always_valid(self): t = Table([ ('A', int), ('B', float), ('C', int), ]) t.append([1, 2.2, None])
def test_append_invalid_row2(self): t = Table([ ('A', int), ('B', float), ('C', int), ]) with self.assertRaises(InvalidData): t.append([1, 2.2, 'hello'])
def test_append_row(self): t = Table([ ('A', int), ('B', float), ('C', str), ]) t.append([1, 2.2, 'hello']) self.assertEqual(list(t), [(1, 2.2, 'hello')])
def test_append_invalid_row(self): t = Table([ ('A', int), ('B', float), ('C', str), ]) with self.assertRaises(InvalidData): t.append([2, 2.2, int])
def test_repr_on_expand_const(self): t = Table([('A', int), ('B', int)]) t.append([1, 2]) e = t.expand_const('C', 3, int) self.assertEqual(repr(e), "\n".join([ "| A (int) | B (int) | C (int) |", "| 1 | 2 | 3 |" ]) )
def test_repr_on_expands(self): t = Table([('A', int), ('B', int)]) t.append([1, 2]) e = t.expand('C', ['A', 'B'], lambda *args: sum(args), int) self.assertEqual(repr(e), "\n".join([ "| A (int) | B (int) | C (int) |", "| 1 | 2 | 3 |" ]) )
def test_empty_table_not_equal_when_schemas_same_but_data_different(self): s = [ ('A', int), ('B', float), ('C', int), ] t0 = Table(s) t1 = Table(s) t0.append([2, 2.22, 1]) self.assertNotEqual(t0, t1)
def test_column_descriptions_on_join(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) self.assertEqual( ['Owner Id (int)', 'Pokemon', 'Level (int)', 'Name (str)'], j._column_descriptions )
class TestArrayTable(unittest.TestCase): def setUp(self): self.t = Table([ ('A', 'i'), ('B', 'u'), ('C', 'f'), ('D', int) ]) def test_array_columns(self): self.assertIsInstance(self.t._get_column('A'), ArrayColumn) self.assertIsInstance(self.t._get_column('D'), Column) def test_append(self): self.t.append( (3, u'h', 2.2, 9) ) def test_repr_column(self): self.t.append( (3, u'h', 2.5, 9) ) expected = '\n'.join([ '| A (i) | B (u) | C (f) | D (int) |', '| 3 | h | 2.5 | 9 |' ]) self.assertEquals( self.t._get_column('A').type, 'i' ) self.assertEquals( self.t._get_column('C').type, 'f' ) self.assertEquals( repr(self.t), expected )
def test_repr_bigger_broken_join_with_project(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [1, 'Bulbasaur', 22], [1, 'Charmander', 12], [3, 'Togepi', 5], [1, 'Starmie', 44], [9, 'Mew', 99], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) o.append([2, 'Brock']) o.append([3, 'Misty']) j = p.left_join( keys=('Owner Id',), other=o ) j2 = j.project('Pokemon', 'Level', 'Name') print(repr(j2))
def test_joined_table_repr_one_row(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) self.assertEquals( j[0], (1, 'Pikachu', 18, 'Ash Ketchum') ) self.assertEquals( list(j)[0], (1, 'Pikachu', 18, 'Ash Ketchum'), )
def test_incomplete_join(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [2, 'Blastoise', 22], [3, 'Weedle', 4], ]) o = Table([('Owner Id', int), ('Owner Name', str)]) o.append([1, 'Ash Ketchum']) o.append([2, 'Brock']) o.append([2, 'Misty']) j = p.left_join( keys=('Owner Id',), other=o ) self.assertEquals( j.column_names, ['Owner Id', 'Pokemon', 'Level', 'Owner Name'] )
def test_get_maximum_column_widths_for_join(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) expected = [ len(j._get_column('Owner Id').description), len(j._get_column('Pokemon').description), len(j._get_column('Level').description), len('Ash Ketchum'), ] widths = j._get_column_widths() self.assertEqual( expected, widths )
STRINGS1 = ['Pikachu', 'Charmander', 'Bulbasaur', 'Oshawatt'] NUMBERS1 = [-1, 0, 2, 3] ENDLESS = itertools.cycle(STRINGS1) right = Table([('B', str), ('V', int)]) right.extend(zip(STRINGS1, NUMBERS1)) t = Table([('A', int), ('B', str), ('C', str)]) for i in range(100000): rnd = random.choice(STRINGS1) cyc = next(ENDLESS) t.append([i, rnd, cyc]) @do_cprofile def main(): j = t.left_join( ('B',), other=right ) foo = list(j) if __name__ == '__main__': main()
class TestIndex(unittest.TestCase): def setUp(self): self.s = ['A', 'B', 'C'] self.t = Table(self.s) self.t.extend([ (1, 1.1, 'hello'), (2, 2.2, 'goodbye'), (3, 3.3, 'yaloo'), (4, 4.4, 'fnuu'), (5, 6.4, 'Animal Crossing'), ]) def test_indexes_must_have_at_least_one_col(self): with self.assertRaises(InvalidIndex): self.t.add_index([]) def test_can_only_index_columns_which_exist(self): with self.assertRaises(InvalidIndex): self.t.add_index(['ZQx9']) def test_there_can_be_only_one(self): i = self.t.add_index( cols=('A', 'C') ) j = self.t.add_index( cols=('A', 'C') ) self.assertTrue(i is j) def test_indexes_have_reprs(self): i = self.t.add_index( cols=('A', 'C') ) expected_str = 'A,C' expected_repr = '<toytable.index.Index %s>' % expected_str self.assertEquals(str(i), expected_str) self.assertEquals(repr(i), expected_repr) def test_create_and_destroy_index(self): """Verify that indexes can be both created, detected and removed. For the correct plural of index see: http://grammarist.com/usage/indexes-indices/ """ self.assertEquals(len(self.t.indexes), 0) i = self.t.add_index( cols=('A', 'C') ) self.assertTrue(('A', 'C') in self.t.indexes) self.assertIn(i, self.t._listeners) # Indexes are automatically deleted when references # are destroyed i = None self.assertFalse('my_first_index' in self.t.indexes) self.assertFalse(i in self.t._listeners) def test_indexes_can_be_hashed(self): i = self.t.add_index( cols=('A', 'C') ) self.assertIsInstance(hash(i), int) def test_indexes_start_out_empty(self): i = self.t.add_index( cols=('A', 'C') ) self.assertEquals(len(i), 0) def test_indexes_can_receive_events(self): i = self.t.add_index( cols=('A', 'C') ) i.notify( op='do_nothing!', pos=0, ) def test_adding_to_a_table_adds_to_indexes(self): i = self.t.add_index( cols=('A', 'C') ) self.t.append((6, 7.4, 'Starfox Adventures')) self.assertEquals(len(i), 1) def test_indexes_can_be_reindexed(self): i = self.t.add_index( cols=('C') ) i.reindex() self.assertEquals(len(i), len(self.t)) def test_indexes_can_be_used_to_look_things_up(self): i = self.t.add_index( cols=('C') ) i.reindex() self.assertEquals( i[('fnuu',)][0], self.t[3] ) def test_indexes_can_be_used_to_locate_a_record(self): i = self.t.add_index( cols=('C') ) i.reindex() self.assertEquals( i.index(('fnuu',))[0], 3 )
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
from toytable import Table p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [1, 'Bulbasaur', 22], [1, 'Charmander', 12], [3, 'Togepi', 5], [1, 'Starmie', 44], [9, 'Mew', 99], ]) print p o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) o.append([2, 'Brock']) o.append([3, 'Misty']) print o j = p.left_join( keys=('Owner Id',), other=o ) print j j2 = j.project('Pokemon', 'Level', 'Name') print j2 restricted = j2.restrict(['Name'], lambda n: n == 'Ash Ketchum') print restricted sliced = j2[1:-1] print sliced j3 = j2.copy() i = j3.add_index(('Pokemon',)).reindex() print i[('Pikachu',)]