Example #1
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
                )
Example #2
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))
Example #3
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)
                     ))

        )
Example #4
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],
            ])
Example #5
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)
Example #6
0
def table_literal(repr_string, default_type=str):
    """Create a toytable.Table object from a multi-line
    string expression. The input format is exactly the
    same as the Table class's repr format::

        >>> 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
        ['Pokemon', 'Level', 'Owner', 'Type']
        >>> print pokedex.column_types
        [<type 'str'>, <type 'int'>, <type 'str'>, <type 'str'>]
        >>> print list(pokedex.Pokemon)
        ['Pikchu', 'Bulbasaur', 'Charmander', 'Raichu']

    Since table literal expressions are strings all columns
    require a type to be explicitly specified. All untyped columns
    are presumed to be strings.

    The type column can be any importable object or function
    capable of building the contents of the column from the 
    string values of each element in the column.

    :param repr_string: table definition
    :type repr_string: str
    :param default_type: optional type to apply to columns where no type is given
    :type default_type: type
    """
    rows_iter = clean_rows(repr_string)
    column_names_and_types = [col_tuple_to_schema_item(parse_column_string(h), default_type=str)
                              for h in next(rows_iter)  ]

    t = Table(column_names_and_types)
    fns = [c.fn_from_string() for c in t._columns]

    def process_row(lst_row):
            for ty, val in zip(fns, lst_row):
                yield ty(val)

    data = (list(process_row(row)) for row in rows_iter)
    t.extend(data)

    return t
Example #7
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
        )
Example #8
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):
            t[3]
Example #9
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']
        )
Example #10
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
        )
Example #11
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))
Example #12
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']
        )
Example #13
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'),
        )
Example #14
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']
        )
Example #15
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
        )
Example #16
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)
Example #17
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 = '<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
        )
Example #18
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]
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',)]
Example #20
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)
Example #21
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)
        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',),
        other=right