示例#1
0
    def test_unequal_tables(self):
        A = table_literal("""
            | A (int) | B (int) |
            | 1       | 2       |
        """)

        B = table_literal("""
            | A (int) | B (int) |
            | 2       | 3       |
        """)

        with self.assertRaises(AssertionError):
            self.assertTablesEqual(A, B)
    def test_foo(self):
        t0 = table_literal("""
            | A (int) | B (int) | C (str) |
            | 1       | 2       | 3       |
            | 2       | 3       | 4       |
        """)

        t1 = table_literal("""
            | A (int) | B (int) | C (str) |
            | 1       | 2       | 3       |
            | 999     | 3       | 4       |
        """)

        self.assertTablesEqual(t0, t1)
示例#3
0
    def test_assert_equal_any_order(self):
        A = table_literal("""
            | A (int) | B (int) | D (float) |
            | 1       | 2       | 1.1       |
            | 1       | 2       | 2.2       |
        """)

        B = table_literal("""
            | A (int) | B (int) | D (float) |
            | 1       | 2       | 2.2       |
            | 1       | 2       | 1.1       |
        """)

        self.assertTablesEqualAnyOrder(A, B)
示例#4
0
    def test_error_unequal_lengths(self):
        A = table_literal("""
            | A (int) | B (int) |
            | 1       | 2       |
        """)

        B = table_literal("""
            | A (int) | B (int) |
        """)

        with self.assertRaises(AssertionError) as ae:
            self.assertTablesEqual(A, B)

        self.assertTrue("Table lengths are different" in ae.exception.args[0])
示例#5
0
    def test_inconsistent_column_types(self):
        A = table_literal("""
            | A (int) | B (int) | D (float) |
            | 1       | 2       | 2.2       |
        """)

        B = table_literal("""
            | A (int) | B (int) | D (str) |
        """)

        with self.assertRaises(AssertionError) as ae:
            self.assertTablesEqual(A, B)

        self.assertTrue("Column types are different" in ae.exception.args[0])
    def test_foo(self):
        t0 = table_literal("""
            | A (int) | B (int) | C (str) |
            | 1       | 2       | 3       |
            | 2       | 3       | 4       |
        """)

        t1 = table_literal("""
            | A (int) | B (int) | C (str) |
            | 1       | 2       | 3       |
            | 999     | 3       | 4       |
        """)

        self.assertTablesEqual(t0, t1)
示例#7
0
    def test_custom_error_message(self):
        A = table_literal("""
            | A (int) | B (int) |
            | 1       | 2       |
        """)

        B = table_literal("""
            | A (int) | B (int) | D (str) |
        """)

        with self.assertRaises(AssertionError) as ae:
            self.assertTablesEqual(A, B, msg="fod_fan_fo")

        self.assertEqual("fod_fan_fo", ae.exception.args[0])
示例#8
0
    def test_assert_equal_any_order(self):
        A = table_literal("""
            | A (int) | B (int) | D (float) |
            | 1       | 2       | 1.1       |
            | 1       | 2       | 9.9       |
        """)

        B = table_literal("""
            | A (int) | B (int) | D (float) |
            | 1       | 2       | 2.2       |
            | 1       | 2       | 1.1       |
        """)

        with self.assertRaises(AssertionError):
            self.assertTablesEqualAnyOrder(A, B)
示例#9
0
 def test_table_literal_one_row(self):
     t = table_literal("""
     | A | B | C |
     | 1 | 2 | 3 |
     """,
                       default_type=int)
     self.assertIsInstance(t, Table)
示例#10
0
 def test_values(self):
     t = table_literal("""
         | A (i) | B (str) |
         | 3     |   hello |
     """)
     r = t[0]
     self.assertEquals(r.values(), [3, 'hello'])
示例#11
0
 def test_keys_on_array_columns(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3     |   hello   |
     """)
     r = t[0]
     self.assertEquals(r.keys(), ('A', 'B'))
示例#12
0
 def test_keys_on_regular_columns(self):
     t = table_literal("""
         | A (i) | B (f) |
         | 3     | 2.2   |
     """)
     r = t[0]
     self.assertEquals(r.keys(), ('A', 'B'))
示例#13
0
 def test_row_can_be_cast_to_tuple(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3       |   hello |
     """)
     r = t[0]
     self.assertEquals(tuple(r), (3, "hello"))
示例#14
0
    def test_table_literal_columns_can_have_whitespace(self):
        t = table_literal("""
        | Attack Type (str) | Special Def (int) |
        | Fire              | 2                 |
        """)

        self.assertEquals(t.column_names, ['Attack Type', 'Special Def'])
示例#15
0
    def simple_aggregate(self):

        expected = table_literal("""
            | Pokemon (str) | Attack Type (str) | Count (int) |
            | Pikachu       | Normal            | 4           |
            | Pikachu       | Electric          | 3           |
            | Pikachu       | Fairy             | 2           |
        """)

        agg = self.t.aggregate(
            keys=('Pokemon', 'Attack Type'),
            aggregations=[
                ('Count', int, lambda t:len(t))
            ]
        )

        self.assertEquals(
            agg.column_names,
            ['Pokemon',
             'Attack Type',
             'Count'])

        self.assertEquals(
            agg.column_types,
            [str, str, int]
        )

        self.assertTablesEqualAnyOrder(
            agg,
            expected
        )
示例#16
0
 def test_row_can_be_accessed_by_names(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3       |   hello |
     """)
     r = t[0]
     self.assertEquals(r['A'], 3)
     self.assertEquals(r['B'], 'hello')
示例#17
0
    def test_items(self):
        t = table_literal("""
            | A (i) | B (str) |
            | 3     |   hello |
        """)
        r = t[0]

        self.assertEquals(list(r.items()), [('A', 3), ('B', 'hello')])
示例#18
0
    def test_table_literal_one_row(self):
        t = table_literal("""
        | A | B | C | D |
        | 1 | 2 | 3 | 4 |
        """,
                          default_type=int)

        self.assertEquals(t.column_names, ['A', 'B', 'C', 'D'])
示例#19
0
 def test_row_can_be_accessed_like_a_tuple(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3       |   hello |
     """)
     r = t[0]
     self.assertEquals(r[0], 3)
     self.assertEquals(r[1], 'hello')
示例#20
0
    def test_table_literal_one_row_with_types(self):
        t = table_literal("""
        | A (int) | B (float) | C (bool) | D (int) |
        | 1       | 2.2       | True     | 4       |
        """)
        self.assertEquals(t.column_names, ['A', 'B', 'C', 'D'])

        self.assertEquals(t[0], (1, 2.2, True, 4))
示例#21
0
 def test_row_can_be_accessed_like_a_tuple(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3       |   hello |
     """)
     r = t[0]
     self.assertEquals(r[0], 3)
     self.assertEquals(r[1], 'hello')
示例#22
0
 def test_row_can_be_accessed_by_names(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3       |   hello |
     """)
     r = t[0]
     self.assertEquals(r['A'], 3)
     self.assertEquals(r['B'], 'hello')
示例#23
0
    def test_row_keyerror(self):
        t = table_literal("""
            | A (int) | B (str) |
            | 3       |   hello |
        """)
        r = t[0]

        with self.assertRaises(KeyError):
            r['X']
示例#24
0
    def test_detect_first_different_row(self):
        A = table_literal("""
            | A (int) | B (int) | D (float) |
            | 1       | 2       | 2.2       |
            | 1       | 2       | 2.1       |
        """)

        B = table_literal("""
            | A (int) | B (int) | D (float) |
            | 1       | 2       | 2.2       |
            | 1       | 2       | 2.3       |
        """)

        with self.assertRaises(AssertionError) as ae:
            self.assertTablesEqual(A, B)

        self.assertTrue("Differences at row 1" in ae.exception.args[0],
                        ae.exception.args[0])
示例#25
0
    def test_row_attributerror(self):
        t = table_literal("""
            | A (int) | B (str) |
            | 3       |   hello |
        """)
        r = t[0]

        with self.assertRaises(AttributeError):
            r.X
示例#26
0
    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       |
        """)
示例#27
0
    def test_row_keyerror(self):
        t = table_literal("""
            | A (int) | B (str) |
            | 3       |   hello |
        """)
        r = t[0]

        with self.assertRaises(KeyError):
            r['X']
示例#28
0
    def test_row_attributerror(self):
        t = table_literal("""
            | A (int) | B (str) |
            | 3       |   hello |
        """)
        r = t[0]

        with self.assertRaises(AttributeError):
            r.X
示例#29
0
    def test_table_literal_one_row(self):
        t = table_literal("""
        | A | B | C | D |
        | 1 | 2 | 3 | 4 |
        """, default_type=int)

        self.assertEquals(
            t.column_names,
            ['A', 'B', 'C', 'D']
        )
示例#30
0
    def test_equal_tables(self):
        A = table_literal("""
            | A (int) | B (int) |
            | 1       | 2       |
        """)
        self.assertTablesEqual(A, A)
        self.assertTablesEquals(A, A)

        with self.assertRaises(AssertionError):
            self.assertTablesNotEquals(A, A)
示例#31
0
    def test_table_literal_columns_can_have_whitespace(self):
        t = table_literal("""
        | Attack Type (str) | Special Def (int) |
        | Fire              | 2                 |
        """)

        self.assertEquals(
            t.column_names,
            ['Attack Type', 'Special Def']
        )
示例#32
0
 def test_keys_on_regular_columns(self):
     t = table_literal("""
         | A (i) | B (f) |
         | 3     | 2.2   |
     """)
     r = t[0]
     self.assertEquals(
         r.keys(),
         ('A', 'B')
     )
示例#33
0
 def test_values(self):
     t = table_literal("""
         | A (i) | B (str) |
         | 3     |   hello |
     """)
     r = t[0]
     self.assertEquals(
         r.values(),
         [3, 'hello']
     )
示例#34
0
 def test_keys_on_array_columns(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3     |   hello   |
     """)
     r = t[0]
     self.assertEquals(
         r.keys(),
         ('A', 'B')
     )
示例#35
0
 def test_row_can_be_cast_to_tuple(self):
     t = table_literal("""
         | A (int) | B (str) |
         | 3       |   hello |
     """)
     r = t[0]
     self.assertEquals(
         tuple(r),
         (3, "hello")
     )
示例#36
0
    def test_items(self):
        t = table_literal("""
            | A (i) | B (str) |
            | 3     |   hello |
        """)
        r = t[0]

        self.assertEquals(
            list(r.items()),
            [('A', 3), ('B', 'hello')]
        )
示例#37
0
    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)
示例#38
0
 def setUp(self):
     self.t = table_literal("""
         | Attack (str)  | Pokemon (str)  | Level Obtained (int) | Attack Type (str) |
         | Thunder Shock | Pikachu        | 1                    | Electric          |
         | Tackle        | Pikachu        | 1                    | Normal            |
         | Tail Whip     | Pikachu        | 1                    | Normal            |
         | Growl         | Pikachu        | 5                    | Normal            |
         | Quick Attack  | Pikachu        | 10                   | Normal            |
         | Thunder Wave  | Pikachu        | 13                   | Electric          |
         | Electro Ball  | Pikachu        | 18                   | Electric          |
         | Charm         | Pikachu        | 0                    | Fairy             |
         | Sweet Kiss    | Pikachu        | 0                    | Fairy             |
     """)
示例#39
0
 def setUp(self):
     self.t = table_literal("""
         | Attack (str)  | Pokemon (str)  | Level Obtained (int) | Attack Type (str) |
         | Thunder Shock | Pikachu        | 1                    | Electric          |
         | Tackle        | Pikachu        | 1                    | Normal            |
         | Tail Whip     | Pikachu        | 1                    | Normal            |
         | Growl         | Pikachu        | 5                    | Normal            |
         | Quick Attack  | Pikachu        | 10                   | Normal            |
         | Thunder Wave  | Pikachu        | 13                   | Electric          |
         | Electro Ball  | Pikachu        | 18                   | Electric          |
         | Charm         | Pikachu        | 0                    | Fairy             |
         | Sweet Kiss    | Pikachu        | 0                    | Fairy             |
     """)
示例#40
0
 def test_get_key_and_subtable(self):
     agg = self.t.aggregate(keys=('Pokemon', 'Attack Type'),
                            aggregations=[('Count', int, lambda t: len(t))])
     gen = agg._iter_subtables()
     expected = table_literal("""
         | Attack (str)  | Pokemon (str)  | Level Obtained (int) | Attack Type (str) |
         | Tackle        | Pikachu        | 1                    | Normal            |
         | Tail Whip     | Pikachu        | 1                    | Normal            |
         | Growl         | Pikachu        | 5                    | Normal            |
         | Quick Attack  | Pikachu        | 10                   | Normal            |
         """)
     for (k, st) in gen:
         if k == ('Pikachu', 'Normal'):
             st = st.copy()
             self.assertTablesEqualAnyOrder(st, expected)
示例#41
0
    def test_table_literal_one_row_with_types(self):
        t = table_literal("""
        | A (int) | B (float) | C (bool) | D (int) |
        | 1       | 2.2       | True     | 4       |
        """)
        self.assertEquals(
            t.column_names,
            ['A', 'B', 'C', 'D']
        )

        self.assertEquals(
            t[0],
            (1, 2.2, True, 4)

        )
示例#42
0
    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)
示例#43
0
    def simple_aggregate(self):

        expected = table_literal("""
            | Pokemon (str) | Attack Type (str) | Count (int) |
            | Pikachu       | Normal            | 4           |
            | Pikachu       | Electric          | 3           |
            | Pikachu       | Fairy             | 2           |
        """)

        agg = self.t.aggregate(keys=('Pokemon', 'Attack Type'),
                               aggregations=[('Count', int, lambda t: len(t))])

        self.assertEquals(agg.column_names,
                          ['Pokemon', 'Attack Type', 'Count'])

        self.assertEquals(agg.column_types, [str, str, int])

        self.assertTablesEqualAnyOrder(agg, expected)
示例#44
0
    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
        )
示例#45
0
 def test_get_key_and_subtable(self):
     agg = self.t.aggregate(
         keys=('Pokemon', 'Attack Type'),
         aggregations=[
             ('Count', int, lambda t:len(t))
         ]
     )
     gen = agg._iter_subtables()
     expected = table_literal("""
         | Attack (str)  | Pokemon (str)  | Level Obtained (int) | Attack Type (str) |
         | Tackle        | Pikachu        | 1                    | Normal            |
         | Tail Whip     | Pikachu        | 1                    | Normal            |
         | Growl         | Pikachu        | 5                    | Normal            |
         | Quick Attack  | Pikachu        | 10                   | Normal            |
         """
     )
     for (k, st) in gen:
         if k == ('Pikachu', 'Normal'):
             st = st.copy()
             self.assertTablesEqualAnyOrder(st, expected)
示例#46
0
from eztable 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 (b) | bar (i) | baz (f) |
| 12      | 2       | 2.2     |
""")

table1 = Table([('foo', 'b'), ('bar', 'i'), ('baz', 'f')])
table1.append([12, 2, 2.2])

assert table0 == table1

table1.append([13, 4, 2.2])
from eztable import table_literal
pokedex = table_literal("""
    | Owner Id (int) | Pokemon    | Level (int) |
    | 1              | Pikachu    | 18          |
    | 1              | Bulbasaur  | 22          |
    | 1              | Charmander | 12          |
    | 3              | Togepi     | 5           |
    | 1              | Starmie    | 44          |
    | 9              | Mew        | 99          |
    """)
print(pokedex)
from eztable 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 (b) | bar (i) | baz (f) |
| 12      | 2       | 2.2     |
""")

table1 = Table([('foo', 'b'), ('bar', 'i'), ('baz', 'f')])
table1.append([12, 2, 2.2])

assert table0 == table1
示例#49
0
 def test_table_literal_one_row(self):
     t = table_literal("""
     | A | B | C |
     | 1 | 2 | 3 |
     """, default_type=int)
     self.assertIsInstance(t, Table)