Exemplo n.º 1
0
 def test_nones_always_valid(self):
     t = Table([
         ('A', int),
         ('B', float),
         ('C', int),
     ])
     t.append([1, 2.2, None])
Exemplo n.º 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))
Exemplo n.º 3
0
 def test_joined_table_repr(self):
     p = Table([('Owner Id', int), 'Pokemon', ('Level', int)])
     o = Table([('Owner Id', int), ('Owner Name', str)])
     j = p.left_join(keys=('Owner Id',), other=o)
     self.assertEquals(
         repr(j),
         "| Owner Id (int) | Pokemon | Level (int) | Owner Name (str) |"
     )
Exemplo n.º 4
0
 def test_append_invalid_row2(self):
     t = Table([
         ('A', int),
         ('B', float),
         ('C', int),
     ])
     with self.assertRaises(InvalidData):
         t.append([1, 2.2, 'hello'])
Exemplo n.º 5
0
 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')])
Exemplo n.º 6
0
    def test_append_invalid_row(self):
        t = Table([
            ('A', int),
            ('B', float),
            ('C', str),
        ])

        with self.assertRaises(InvalidData):
            t.append([2, 2.2, int])
Exemplo n.º 7
0
 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       |"
                      ])
                      )
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
 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       |"
                      ])
                      )
Exemplo n.º 10
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)
                     ))

        )
Exemplo n.º 11
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],
            ])
Exemplo n.º 12
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)
Exemplo n.º 13
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
Exemplo n.º 14
0
 def setUp(self):
     self.t = Table([
         ('A', 'i'),
         ('B', 'u'),
         ('C', 'f'),
         ('D', int)
     ])
Exemplo n.º 15
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]
Exemplo n.º 16
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
                )
Exemplo n.º 17
0
    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'),
        ])
Exemplo n.º 18
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']
        )
Exemplo n.º 19
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
        )
Exemplo n.º 20
0
    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'),
        ])
Exemplo n.º 21
0
    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'),
        ])
Exemplo n.º 22
0
    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'],
        ])
Exemplo n.º 23
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']
        )
Exemplo n.º 24
0
    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'],
        ])
Exemplo n.º 25
0
    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']
        ])
Exemplo n.º 26
0
    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'],
        ])
Exemplo n.º 27
0
    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'),
        ])
Exemplo n.º 28
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))
Exemplo n.º 29
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
        )
Exemplo n.º 30
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']
        )