示例#1
0
 def test_repr(self):
     t = Table([('A', int), 'B', ('C', float)])
     t.extend([
         [1, "hello", 1.1],
         [2, "goodbye", 2.2],
     ])
     print(repr(t))
示例#2
0
    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
                )
示例#3
0
class TestProject(unittest.TestCase):
    def setUp(self):
        self.s = [
            ('A', int),
            ('B', float),
            ('C', str),
        ]
        self.t = Table(self.s)

        self.t.extend([
            [1, 1.1, 'hello'],
            [2, 2.2, 'yello'],
        ])

    def test_simple_project(self):
        t = self.t.project(['A', 'C'])
        self.assertEqual(t.schema, [('A', int), ('C', str)])
        self.assertEqual(t[0], (1, 'hello'))

    def test_anti_project(self):
        t = self.t.anti_project(['B'])
        self.assertEqual(t.schema, [('A', int), ('C', str)])
        self.assertEqual(t[0], (1, 'hello'))

    def test_anti_project_multiple_arguments(self):
        t = self.t.anti_project('A', 'B')
        self.assertEqual(t.schema, [('C', str)])
        self.assertEqual(t[0], ('hello', ))

    def test_expand_const_on_project(self):
        t0 = self.t.project(['C'])
        t1 = t0.expand_const('D', 0)
        self.assertEqual(list(t1.D), [0] * len(self.t))
示例#4
0
class TestRestrict(unittest.TestCase):
    def setUp(self):
        self.s = ['A', 'B', 'C']
        self.t = Table(self.s)

        self.t.extend([
            (1, True, 'charmander'),
            (2, False, 'bulbasaur'),
            (3, True, 'squirtle'),
            (4, True, 'pikachu'),
            (5, False, 'butterfree'),
        ])

    def test_restrict_with_materialize(self):
        r = self.t.restrict(col_names=('B'), fn=lambda b: b)

        # We also need to be able to do this
        # operation without a materilize!
        self.assertEquals(list(r.copy().A), [1, 3, 4])

    def test_restrict_without_materialize(self):
        r = self.t.restrict(col_names=('B'), fn=lambda b: b)

        # No copy operation
        self.assertEquals(list(r.A), [1, 3, 4])

    def test_that_restricted_columns_are_consistent(self):
        r = self.t.restrict(col_names=('B'), fn=lambda b: b)

        self.assertEquals(len(r), len(r.A))

        self.assertEquals(list(r), list(zip(list(r.A), list(r.B), list(r.C))))
示例#5
0
class TestProject(unittest.TestCase):
    def setUp(self):
        self.s = [("A", int), ("B", float), ("C", str)]
        self.t = Table(self.s)

        self.t.extend([[1, 1.1, "hello"], [2, 2.2, "yello"]])

    def test_simple_project(self):
        t = self.t.project(["A", "C"])
        self.assertEqual(t.schema, [("A", int), ("C", str)])
        self.assertEqual(t[0], (1, "hello"))

    def test_anti_project(self):
        t = self.t.anti_project(["B"])
        self.assertEqual(t.schema, [("A", int), ("C", str)])
        self.assertEqual(t[0], (1, "hello"))

    def test_anti_project_multiple_arguments(self):
        t = self.t.anti_project("A", "B")
        self.assertEqual(t.schema, [("C", str)])
        self.assertEqual(t[0], ("hello",))

    def test_expand_const_on_project(self):
        t0 = self.t.project(["C"])
        t1 = t0.expand_const("D", 0)
        self.assertEqual(list(t1.D), [0] * len(self.t))
示例#6
0
class TestRename(unittest.TestCase):
    def setUp(self):
        self.s = ['pkmn', 'lvl', 'ownr']
        self.t = Table(self.s)

        self.t.extend([
            ('Pikachu', 12, 'ash'),
            ('Squirtle', 18, 'ash'),
            ('Starmie', 4, 'misty'),
        ])

    def test_basic_rename(self):
        t = self.t.rename(old_names=self.t.column_names,
                          new_names=['Pokemon', 'Level', 'Owner'])

        self.assertEqual(t._rename_dict, {
            'pkmn': 'Pokemon',
            'lvl': 'Level',
            'ownr': 'Owner'
        })

        self.assertEquals(t.column_names, ['Pokemon', 'Level', 'Owner'])

    def test_get_renamed_column(self):
        t = self.t.rename(old_names=self.t.column_names,
                          new_names=['Pokemon', 'Level', 'Owner'])
        p = t._get_column('Pokemon')
        self.assertEqual(list(p), ['Pikachu', 'Squirtle', 'Starmie'])
示例#7
0
    def test_extend_rows(self):
        t = Table([
            ('A', int),
            ('B', float),
            ('C', str),
        ])
        t.extend([[1, 1.1, 'hello'], [2, 2.2, 'goodbye']])

        self.assertEqual(len(t), 2)
示例#8
0
class TestRestrict(unittest.TestCase):

    def setUp(self):
        self.s = ['A', 'B', 'C']
        self.t = Table(self.s)

        self.t.extend([
            (1, True, 'charmander'),
            (2, False, 'bulbasaur'),
            (3, True, 'squirtle'),
            (4, True, 'pikachu'),
            (5, False, 'butterfree'),
        ])

    def test_restrict_with_materialize(self):
        r = self.t.restrict(
            col_names=('B'),
            fn=lambda b: b
        )

        # We also need to be able to do this
        # operation without a materilize!
        self.assertEquals(
            list(r.copy().A),
            [1, 3, 4]
        )

    def test_restrict_without_materialize(self):
        r = self.t.restrict(
            col_names=('B'),
            fn=lambda b: b
        )

        # No copy operation
        self.assertEquals(
            list(r.A),
            [1, 3, 4]
        )

    def test_that_restricted_columns_are_consistent(self):
        r = self.t.restrict(
            col_names=('B'),
            fn=lambda b: b
        )

        self.assertEquals(len(r), len(r.A))

        self.assertEquals(
            list(r),
            list(zip(list(r.A),
                     list(r.B),
                     list(r.C)
                     ))

        )
示例#9
0
    def test_extend_invalid_rows(self):
        t = Table([
            ('A', int),
            ('B', float),
            ('C', str),
        ])

        with self.assertRaises(InvalidData):
            t.extend([
                [1, 1.1, 0],
            ])
示例#10
0
    def test_extend_invalid_rows(self):
        t = Table([
            ('A', int),
            ('B', float),
            ('C', str),
        ])

        with self.assertRaises(InvalidData):
            t.extend([
                [1, 1.1, 0],
            ])
示例#11
0
    def test_extend_rows(self):
        t = Table([
            ('A', int),
            ('B', float),
            ('C', str),
        ])
        t.extend([
            [1, 1.1, 'hello'],
            [2, 2.2, 'goodbye']
        ])

        self.assertEqual(len(t), 2)
示例#12
0
    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
        )
示例#13
0
class TestTableRowAccess(unittest.TestCase):

    def setUp(self):
        self.s = [
            ('A', int),
            ('B', float),
            ('C', str),
        ]
        self.t = Table(self.s)

        self.t.extend([
            (1, 1.1, 'hello'),
            (2, 2.2, 'goodbye'),
            (3, 3.3, 'yaloo'),
            (4, 4.4, 'fnuu'),
        ])

    def test_basic_slice(self):
        t = self.t[0:2:]
        self.assertEqual(len(t), 2)
        self.assertEqual(t.schema, self.s)

    def test_get_row(self):
        self.assertEqual(
            self.t[0],
            (1, 1.1, 'hello')
        )

    def test_get_sliced_row(self):
        t = self.t[1:]
        self.assertEqual(
            t[0],
            (2, 2.2, 'goodbye'),
        )

    def test_get_sliced_row_reverse(self):
        t = self.t[::-1]
        self.assertEqual(
            t[0],
            (4, 4.4, 'fnuu'),
        )

    def test_get_sliced_row_indexerror(self):
        t = self.t[1:]
        with self.assertRaises(IndexError):
            assert t[3]
示例#14
0
class TestJoinWithNonMatchingKeys(unittest.TestCase):

    def setUp(self):
        self.pokedex = Table(['pokemon', 'owner', 'level'])
        self.pokedex.extend([
            ['Charmander', 'Ash', 12],
            ['Pikachu', 'Ash', 15],
            ['Squirtle', 'Ash', 19],
            ['Starmie', 'Misty', 19],
            ['Togepi', 'Misty', 5],
            ['Onyx', 'Brock', 22],
            ['Meowth', 'Team Rocket', 22],
        ])

        self.types = Table(['pkmn', 'type'])
        self.types.extend([
            ['Togepi', 'Fairy'],
            ['Onyx', 'Rock'],
            ['Meowth', 'Normal'],
            ['Pikachu', 'Electric'],
            ['Squirtle', 'Water'],
            ['Starmie', 'Water'],
            ['Charmander', 'Fire'],
        ])

    def test_basic_join(self):

        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types,
            other_keys=('pkmn',),
        )

        self.assertIsInstance(t, JoinTable)

        self.assertEquals(
            t._key_columns,
            [t.pokemon, ]
        )

        self.assertEquals(
            t.column_names,
            ['pokemon', 'owner', 'level', 'type']
        )
示例#15
0
class TestInnerJoin(unittest.TestCase, TableTestMixin):

    def setUp(self):
        self.pokedex = Table([('pokemon', str), ('owner', str), ('level', 'i')])
        self.pokedex.extend([
            ['Charmander', 'Ash', 12],
            ['Pikachu', 'Ash', 15],
            ['Squirtle', 'Ash', 19],
            ['Starmie', 'Misty', 19],
            ['Togepi', 'Misty', 5],
            ['Onyx', 'Brock', 22],
            ['Meowth', 'Team Rocket', 22],
            ['Mew', None, 1],
            ['Mewtwo', None, 1],
        ])

        self.types = Table([('pokemon', str), ('type', str)])
        self.types.extend([
            ['Togepi', 'Fairy'],
            ['Onyx', 'Rock'],
            ['Meowth', 'Normal'],
            ['Charmander', 'Fire'],
            ['Snorlax', 'Normal']
        ])

    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
        )
示例#16
0
 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))
示例#17
0
    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'),
        )
示例#18
0
class TestRename(unittest.TestCase):

    def setUp(self):
        self.s = ['pkmn', 'lvl', 'ownr']
        self.t = Table(self.s)

        self.t.extend([
            ('Pikachu', 12, 'ash'),
            ('Squirtle', 18, 'ash'),
            ('Starmie', 4, 'misty'),
        ])

    def test_basic_rename(self):
        t = self.t.rename(
            old_names=self.t.column_names,
            new_names=['Pokemon', 'Level', 'Owner']
        )

        self.assertEqual(
            t._rename_dict,
            {'pkmn': 'Pokemon', 'lvl': 'Level', 'ownr': 'Owner'}

        )

        self.assertEquals(
            t.column_names,
            ['Pokemon', 'Level', 'Owner']
        )

    def test_get_renamed_column(self):
        t = self.t.rename(
            old_names=self.t.column_names,
            new_names=['Pokemon', 'Level', 'Owner']
        )
        p = t._get_column('Pokemon')
        self.assertEqual(
            list(p),
            ['Pikachu', 'Squirtle', 'Starmie']
        )
示例#19
0
    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']
        )
示例#20
0
    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
        )
示例#21
0
from eztable import Table


t = Table([
    ('A', int),
    ('B', float),
    ('C', str),
])

t.extend([
    (1, 1.1, 'hello'),
    (2, 2.2, 'goodbye'),
    (3, 3.3, 'yaloo'),
    (4, 4.4, 'fnuu'),
])

print(t[::])
示例#22
0
class TestJoin(unittest.TestCase):

    def setUp(self):
        self.pokedex = Table(['pokemon', 'owner', ('level', 'i')])
        self.pokedex.extend([
            ['Charmander', 'Ash', 12],
            ['Pikachu', 'Ash', 15],
            ['Squirtle', 'Ash', 19],
            ['Starmie', 'Misty', 19],
            ['Togepi', 'Misty', 5],
            ['Onyx', 'Brock', 22],
            ['Meowth', 'Team Rocket', 22],
            ['Mew', None, 99],
        ])

        self.types = Table(['pokemon', 'type'])
        self.types.extend([
            ['Togepi', 'Fairy'],
            ['Onyx', 'Rock'],
            ['Meowth', 'Normal'],
            ['Pikachu', 'Electric'],
            ['Squirtle', 'Water'],
            ['Starmie', 'Water'],
            ['Charmander', 'Fire'],
        ])

    def test_join_interface(self):
        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types
        )
        self.assertEquals(
            t._key_columns,
            [t.pokemon, ]
        )
        self.assertEquals(
            t.column_names,
            ['pokemon', 'owner', 'level', 'type']
        )

    def test_join_schema(self):
        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types
        )
        expected = [
            ('pokemon', object),
            ('owner', object),
            ('level', 'i'),
            ('type', object),
        ]
        self.assertEquals(
            t.schema,
            expected
        )

    def test_join_indeces_function(self):
        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types
        )
        self.assertEquals(
            list(t._join_indices_func()),
            [6, 3, 4, 5, 0, 1, 2, None]
        )

    def test_join_row(self):
        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types
        )
        self.assertEquals(
            t[0],
            ('Charmander', 'Ash', 12, 'Fire')
        )

    def test_join_iter(self):
        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types
        )
        tl = list(t)
        self.assertEquals(
            tl[1],
            ('Pikachu', 'Ash', 15, 'Electric')
        )

    def test_impossible_join(self):
        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types
        )
        self.assertEquals(
            t[7],
            ('Mew', None, 99, None)
        )

    def test_over_read(self):
        t = self.pokedex.left_join(
            keys=('pokemon',),
            other=self.types
        )
        with self.assertRaises(IndexError):
            t[99]
示例#23
0
from eztable import Table
o = Table([('Owner Id', int), ('Name', str)])
o.extend([[1, 'Ash Ketchum'],
          [2, 'Brock'],
          [3, 'Misty']])
print(o)
示例#24
0
class TestExpandTable(unittest.TestCase):
    def setUp(self):
        self.s = [
            ('A', int),
            ('B', float),
            ('C', str),
        ]
        self.t = Table(self.s)

        self.t.extend([
            [1, 1.1, 'hello'],
            [2, 2.2, 'yello'],
        ])

    def test_expand_const(self):
        t = self.t.expand_const(name='D', value='X', type=str)

        self.assertEqual(t.column_names, ['A', 'B', 'C', 'D'])

        self.assertEqual(t[0], (1, 1.1, 'hello', 'X'))

    def test_expand_const_does_not_affect_length(self):
        t = self.t.expand_const(name='D', type=str, value='X')
        self.assertEqual(len(t), len(self.t))

    def test_simple_expand(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        self.assertEqual(list(t.D), [5, 5])

    def test_derived_columns_are_iterable(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        for _ in t.D:
            pass

    def test_derived_columns_can_be_printed(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        str(t.D)
        repr(t.D)

    def test_derived_columns_have_descriptions(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        self.assertEqual(t.D.description, "D (int)")

    def test_that_float_columns_have_descriptions(self):
        t = self.t.expand(name='D',
                          input_columns=['A', 'B', 'C'],
                          fn=lambda a, b, c: float(len(c) + a + b),
                          col_type=float)
        self.assertEqual(t.D.description, "D (float)")

    def test_simple_expand_and_materialize(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C) + 1).copy()

        expected = [
            (1, 1.1, 'hello', 6),
            (2, 2.2, 'yello', 6),
        ]

        self.assertEqual(list(t), expected)
        self.assertIsInstance(t, Table)

    def test_simple_expand_and_slice(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C) + 1)[1:]

        expected = [
            (2, 2.2, 'yello', 6),
        ]

        self.assertEqual(list(t), expected)
示例#25
0
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 = '<eztable.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)
示例#26
0
from eztable 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:4]
print(sliced)

j3 = j2.copy()
i = j3.add_index(('Pokemon', )).reindex()
print(i[('Pikachu', )])
示例#27
0
class TestExpandTable(unittest.TestCase):

    def setUp(self):
        self.s = [
            ('A', int),
            ('B', float),
            ('C', str),
        ]
        self.t = Table(self.s)

        self.t.extend([
            [1, 1.1, 'hello'],
            [2, 2.2, 'yello'],
        ])

    def test_expand_const(self):
        t = self.t.expand_const(
            name='D',
            value='X',
            type=str
        )

        self.assertEqual(
            t.column_names,
            ['A', 'B', 'C', 'D']
        )

        self.assertEqual(
            t[0],
            (1, 1.1, 'hello', 'X')
        )

    def test_expand_const_does_not_affect_length(self):
        t = self.t.expand_const(
            name='D',
            type=str,
            value='X'
        )
        self.assertEqual(len(t), len(self.t))

    def test_simple_expand(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        self.assertEqual(list(t.D), [5, 5])

    def test_derived_columns_are_iterable(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        for _ in t.D:
            pass

    def test_derived_columns_can_be_printed(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        str(t.D)
        repr(t.D)

    def test_derived_columns_have_descriptions(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        self.assertEqual(t.D.description, "D (int)")

    def test_that_float_columns_have_descriptions(self):
        t = self.t.expand(
            name='D',
            input_columns=['A', 'B', 'C'],
            fn=lambda a,b,c: float(len(c) + a + b),
            col_type=float
        )
        self.assertEqual(t.D.description, "D (float)")

    def test_simple_expand_and_materialize(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C) + 1
        ).copy()

        expected = [
            (1, 1.1, 'hello', 6),
            (2, 2.2, 'yello', 6),
        ]

        self.assertEqual(list(t), expected)
        self.assertIsInstance(t, Table)

    def test_simple_expand_and_slice(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C) + 1
        )[1:]

        expected = [
            (2, 2.2, 'yello', 6),
        ]

        self.assertEqual(list(t), expected)
示例#28
0
from eztable 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:4]
print(sliced)

j3 = j2.copy()
i = j3.add_index(('Pokemon',)).reindex()
print(i[('Pikachu',)])
示例#29
0
            result = func(*args, **kwargs)
            profile.disable()
            return result
        finally:
            profile.print_stats(sort='tottime')

    return profiled_func


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)
示例#30
0
from eztable import Table
o = Table([('Owner Id', int), ('Name', str)])
o.extend([[1, 'Ash Ketchum'], [2, 'Brock'], [3, 'Misty']])
print(o)
示例#31
0
class TestExport(unittest.TestCase, TableTestMixin):

    def setUp(self):
        self.s = [
            ('A', int),
            ('B', float),
            ('C', str),
        ]
        self.t = Table(self.s)

        self.t.extend([
            [1, 0.0, 'x'],
            [2, 5.0, 'y'],
            [3, 10.0, 'z'],
        ])

    def test_export_csv(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file)
        output_file.seek(0)
        reader = csv.DictReader(output_file)
        row = next(reader)
        self.assertEqual(row, {"A":"1", "B":"0.0", "C":"x"})

    def test_export_csv_with_dialect(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file, dialect="excel")
        output_file.seek(0)
        reader = csv.DictReader(output_file, dialect="excel")
        row = next(reader)
        self.assertEqual(row, {"A":"1", "B":"0.0", "C":"x"})

    def test_export_csv_with_dialect_types(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file, dialect="excel", descriptions=True)
        output_file.seek(0)
        reader = csv.DictReader(output_file, dialect="excel")

        self.assertEqual(next(reader), {"A (int)":"1", "B (float)":"0.0", "C (str)":"x"})

    def test_simple_expand(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        self.assertEqual(list(t.D), [1, 1, 1])


    def test_derived_columns_are_iterable(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        for _ in t.D:
            pass

    def test_derived_columns_can_be_printed(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        str(t.D)
        repr(t.D)

    def test_derived_columns_have_descriptions(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        self.assertEqual(t.D.description, "D (int)")

    def test_that_float_columns_have_descriptions(self):
        t = self.t.expand(
            name='D',
            input_columns=['A', 'B', 'C'],
            fn=lambda a,b,c: float(len(c) + a + b),
            col_type=float
        )
        self.assertEqual(t.D.description, "D (float)")

    def test_simple_expand_and_materialize(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C) + 1
        ).copy()

    def test_export_csv_with_dialect_tab(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file, dialect="excel-tab")
        output_file.seek(0)
        reader = csv.DictReader(output_file, dialect="excel-tab")
        row = next(reader)
        self.assertEqual(row, {"A":"1", "B":"0.0", "C":"x"})


    def test_simple_expand_and_slice(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C) + 1
        )[1:2]

        expected = [
            (2, 5.0, 'y', 2),
        ]

        self.assertEqual(list(t), expected)
示例#32
0
class TestExport(unittest.TestCase, TableTestMixin):
    def setUp(self):
        self.s = [
            ('A', int),
            ('B', float),
            ('C', str),
        ]
        self.t = Table(self.s)

        self.t.extend([
            [1, 0.0, 'x'],
            [2, 5.0, 'y'],
            [3, 10.0, 'z'],
        ])

    def test_export_csv(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file)
        output_file.seek(0)
        reader = csv.DictReader(output_file)
        row = next(reader)
        self.assertEqual(row, {"A": "1", "B": "0.0", "C": "x"})

    def test_export_csv_with_dialect(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file, dialect="excel")
        output_file.seek(0)
        reader = csv.DictReader(output_file, dialect="excel")
        row = next(reader)
        self.assertEqual(row, {"A": "1", "B": "0.0", "C": "x"})

    def test_export_csv_with_dialect_types(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file,
                      dialect="excel",
                      descriptions=True)
        output_file.seek(0)
        reader = csv.DictReader(output_file, dialect="excel")

        self.assertEqual(next(reader), {
            "A (int)": "1",
            "B (float)": "0.0",
            "C (str)": "x"
        })

    def test_simple_expand(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        self.assertEqual(list(t.D), [1, 1, 1])

    def test_derived_columns_are_iterable(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        for _ in t.D:
            pass

    def test_derived_columns_can_be_printed(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        str(t.D)
        repr(t.D)

    def test_derived_columns_have_descriptions(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C))
        self.assertEqual(t.D.description, "D (int)")

    def test_that_float_columns_have_descriptions(self):
        t = self.t.expand(name='D',
                          input_columns=['A', 'B', 'C'],
                          fn=lambda a, b, c: float(len(c) + a + b),
                          col_type=float)
        self.assertEqual(t.D.description, "D (float)")

    def test_simple_expand_and_materialize(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C) + 1).copy()

    def test_export_csv_with_dialect_tab(self):
        output_file = StringIO()
        self.t.to_csv(output_file=output_file, dialect="excel-tab")
        output_file.seek(0)
        reader = csv.DictReader(output_file, dialect="excel-tab")
        row = next(reader)
        self.assertEqual(row, {"A": "1", "B": "0.0", "C": "x"})

    def test_simple_expand_and_slice(self):
        t = self.t.expand(name='D',
                          col_type=int,
                          input_columns=['C'],
                          fn=lambda C: len(C) + 1)[1:2]

        expected = [
            (2, 5.0, 'y', 2),
        ]

        self.assertEqual(list(t), expected)
示例#33
0
class TestNormalize(unittest.TestCase, TableTestMixin):

    def setUp(self):
        self.s = [
            ('A', int),
            ('B', float),
            ('C', str),
        ]
        self.t = Table(self.s)

        self.t.extend([
            [1, 0.0, 'x'],
            [2, 5.0, 'y'],
            [3, 10.0, 'z'],
        ])

    def test_basic_normalize(self):
        t = self.t.normalize({"B":1.0})
        self.assertEqual(list(t.B), [0, 0.5, 1])

    def test_basic_standardization(self):
        t = self.t.standardize({"B":1.0})
        result = list(t.B)
        self.assertEqual(result[1], 0)
        self.assertEqual(result[0], result[2]*-1)

    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_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_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_simple_expand(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        self.assertEqual(list(t.D), [1,1,1])

    def test_derived_columns_are_iterable(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        for _ in t.D:
            pass

    def test_derived_columns_can_be_printed(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        str(t.D)
        repr(t.D)

    def test_derived_columns_have_descriptions(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C)
        )
        self.assertEqual(t.D.description, "D (int)")

    def test_that_float_columns_have_descriptions(self):
        t = self.t.expand(
            name='D',
            input_columns=['A', 'B', 'C'],
            fn=lambda a,b,c: float(len(c) + a + b),
            col_type=float
        )
        self.assertEqual(t.D.description, "D (float)")

    def test_simple_expand_and_materialize(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['A', 'C'],
            fn=lambda A, C: len(C) + A
        ).copy()

        expected = [
            (1, 0.0, 'x', 2),
            (2, 5.0, 'y', 3),
            (3, 10.0, 'z', 4),
        ]

        self.assertEqual(list(t), expected)
        self.assertIsInstance(t, Table)

    def test_simple_expand_and_slice(self):
        t = self.t.expand(
            name='D',
            col_type=int,
            input_columns=['C'],
            fn=lambda C: len(C) + 2
        )[1:2]

        expected = [
            (2, 5.0, 'y', 3),
        ]

        self.assertEqual(list(t), expected)
示例#34
0
from eztable import Table

t = Table([
    ('A', int),
    ('B', float),
    ('C', str),
])

t.extend([
    [1, 1.1, "x"],
])
from eztable import Table

t = Table([
    ('A', int),
    ('B', float),
    ('C', str),
])


t.extend([
    [1, 1.1, "x"],
])
示例#36
0
        try:
            profile.enable()
            result = func(*args, **kwargs)
            profile.disable()
            return result
        finally:
            profile.print_stats(sort='tottime')
    return profiled_func

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',),