Пример #1
0
    def test_group_by_group_name(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by('one', key_name='test')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(tableset._key_name, 'test')
Пример #2
0
    def test_group_by_group_name(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by('one', key_name='test')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(tableset._key_name, 'test')
Пример #3
0
    def test_group_by_function_group_name(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by(lambda r: r['three'] < 5, key_name='test')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(tableset._key_name, 'test')
Пример #4
0
    def test_group_by_function_group_name(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by(lambda r: r['three'] < 5, key_name='test')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(tableset._key_name, 'test')
Пример #5
0
    def test_group_by_function(self):
        table = Table(self.rows, self.columns)

        new_tables = table.group_by(lambda r: r['three'] < 5)

        self.assertIsInstance(new_tables, TableSet)
        self.assertEqual(len(new_tables), 2)

        self.assertIn('True', new_tables.keys())
        self.assertIn('False', new_tables.keys())

        self.assertSequenceEqual(new_tables['True'].columns['one'], ('a', 'a', 'b'))
        self.assertSequenceEqual(new_tables['False'].columns['one'], (None,))
Пример #6
0
    def test_group_by_function(self):
        table = Table(self.rows, self.columns)

        new_tables = table.group_by(lambda r: r['three'] < 5)

        self.assertIsInstance(new_tables, TableSet)
        self.assertEqual(len(new_tables), 2)

        self.assertIn('True', new_tables.keys())
        self.assertIn('False', new_tables.keys())

        self.assertSequenceEqual(new_tables['True'].columns['one'],
                                 ('a', 'a', 'b'))
        self.assertSequenceEqual(new_tables['False'].columns['one'], (None, ))
Пример #7
0
    def test_group_by_function(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by(lambda r: r['three'] < 5)

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(len(tableset), 2)
        self.assertEqual(tableset._key_name, 'group')

        self.assertIn('True', tableset.keys())
        self.assertIn('False', tableset.keys())

        self.assertSequenceEqual(tableset['True'].columns['one'], ('a', 'a', 'b'))
        self.assertSequenceEqual(tableset['False'].columns['one'], (None,))
Пример #8
0
    def test_group_by_function(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by(lambda r: r['three'] < 5, key_type=Boolean())

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(len(tableset), 2)
        self.assertEqual(tableset.key_name, 'group')

        self.assertIn(True, tableset.keys())
        self.assertIn(False, tableset.keys())

        self.assertSequenceEqual(tableset[True].columns['one'], ('a', 'a', 'b'))
        self.assertSequenceEqual(tableset[False].columns['one'], (None,))
Пример #9
0
    def test_group_by_key_type(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by('two', key_type=Text())

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(tableset.key_name, 'two')
        self.assertIsInstance(tableset.key_type, Text)

        self.assertIn('2', tableset.keys())
        self.assertIn('3', tableset.keys())

        self.assertSequenceEqual(tableset['2'].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(tableset['3'].columns['one'], (None, 'b'))
Пример #10
0
    def test_group_by_function(self):
        table = Table(self.rows, self.column_names, self.column_types)

        tableset = table.group_by(lambda r: r['three'] < 5, key_type=Boolean())

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(len(tableset), 2)
        self.assertEqual(tableset.key_name, 'group')

        self.assertIn(True, tableset.keys())
        self.assertIn(False, tableset.keys())

        self.assertSequenceEqual(tableset[True].columns['one'], ('a', 'a', 'b'))
        self.assertSequenceEqual(tableset[False].columns['one'], (None,))
Пример #11
0
    def test_group_by_key_type(self):
        table = Table(self.rows, self.column_names, self.column_types)

        tableset = table.group_by('two', key_type=Text())

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(tableset.key_name, 'two')
        self.assertIsInstance(tableset.key_type, Text)

        self.assertIn('2', tableset.keys())
        self.assertIn('3', tableset.keys())

        self.assertSequenceEqual(tableset['2'].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(tableset['3'].columns['one'], (None, 'b'))
Пример #12
0
    def test_group_by_key_name(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by('one', key_name='test')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(tableset.key_name, 'test')
        self.assertIsInstance(tableset.key_type, Text)

        self.assertIn('a', tableset.keys())
        self.assertIn('b', tableset.keys())
        self.assertIn(None, tableset.keys())

        self.assertSequenceEqual(tableset['a'].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(tableset['b'].columns['one'], ('b',))
Пример #13
0
    def test_group_by_number(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by('two')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(len(tableset), 2)
        self.assertEqual(tableset.key_name, 'two')
        self.assertIsInstance(tableset.key_type, Number)

        self.assertIn(Decimal('2'), tableset.keys())
        self.assertIn(Decimal('3'), tableset.keys())

        self.assertSequenceEqual(tableset[Decimal('2')].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(tableset[Decimal('3')].columns['one'], (None, 'b'))
Пример #14
0
    def test_group_by(self):
        table = Table(self.rows, self.columns)

        new_tables = table.group_by('one')

        self.assertIsInstance(new_tables, TableSet)
        self.assertEqual(len(new_tables), 3)

        self.assertIn('a', new_tables.keys())
        self.assertIn('b', new_tables.keys())
        self.assertIn('None', new_tables.keys())

        self.assertSequenceEqual(new_tables['a'].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(new_tables['b'].columns['one'], ('b', ))
        self.assertSequenceEqual(new_tables['None'].columns['one'], (None, ))
Пример #15
0
    def test_group_by(self):
        table = Table(self.rows, self.columns)

        new_tables = table.group_by('one')

        self.assertIsInstance(new_tables, TableSet)
        self.assertEqual(len(new_tables), 3)

        self.assertIn('a', new_tables.keys())
        self.assertIn('b', new_tables.keys())
        self.assertIn('None', new_tables.keys())

        self.assertSequenceEqual(new_tables['a'].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(new_tables['b'].columns['one'], ('b',))
        self.assertSequenceEqual(new_tables['None'].columns['one'], (None,))
Пример #16
0
    def test_group_by_number(self):
        table = Table(self.rows, self.column_names, self.column_types)

        tableset = table.group_by('two')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(len(tableset), 2)
        self.assertEqual(tableset.key_name, 'two')
        self.assertIsInstance(tableset.key_type, Number)

        self.assertIn(Decimal('2'), tableset.keys())
        self.assertIn(Decimal('3'), tableset.keys())

        self.assertSequenceEqual(tableset[Decimal('2')].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(tableset[Decimal('3')].columns['one'], (None, 'b'))
Пример #17
0
    def test_group_by(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by('one')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(len(tableset), 3)
        self.assertEqual(tableset._key_name, 'one')

        self.assertIn('a', tableset.keys())
        self.assertIn('b', tableset.keys())
        self.assertIn('None', tableset.keys())

        self.assertSequenceEqual(tableset['a'].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(tableset['b'].columns['one'], ('b', ))
        self.assertSequenceEqual(tableset['None'].columns['one'], (None, ))
Пример #18
0
    def test_group_by(self):
        table = Table(self.rows, self.columns)

        tableset = table.group_by('one')

        self.assertIsInstance(tableset, TableSet)
        self.assertEqual(len(tableset), 3)
        self.assertEqual(tableset._key_name, 'one')

        self.assertIn('a', tableset.keys())
        self.assertIn('b', tableset.keys())
        self.assertIn('None', tableset.keys())

        self.assertSequenceEqual(tableset['a'].columns['one'], ('a', 'a'))
        self.assertSequenceEqual(tableset['b'].columns['one'], ('b',))
        self.assertSequenceEqual(tableset['None'].columns['one'], (None,))
Пример #19
0
    def test_group_by_bad_column(self):
        table = Table(self.rows, self.column_names, self.column_types)

        with self.assertRaises(KeyError):
            table.group_by('bad')
Пример #20
0
    reader = csv.reader(f)

    # Skip header
    next(f)

    # Create the table
    table = Table(reader, COLUMNS)

# Filter to counties containing Kansas City
kansas_city = table.where(lambda r: r['county'] in ('JACKSON', 'CLAY', 'CASS', 'PLATTE'))

# Sum total_cost of four counties
print('Total for Kansas City area: %i' % kansas_city.columns['total_cost'].aggregate(Sum()))

# Group by county
counties = table.group_by('county')

# Aggregate totals for all counties
totals = counties.aggregate([
    ('total_cost', Sum(), 'total_cost_sum')
])

totals = totals.order_by('total_cost_sum', reverse=True).rows[:5]

print('Five most spendy counties:')

for i, row in enumerate(totals):
    text = '# {}: {}, ${:,}'.format(i + 1, row['group'], row['total_cost_sum'])
    print(text)

# Get the five most recent purchases
Пример #21
0
    def test_group_by_bad_column(self):
        table = Table(self.rows, self.columns)

        with self.assertRaises(ColumnDoesNotExistError):
            table.group_by('bad')
Пример #22
0
    def test_group_by_bad_column(self):
        table = Table(self.rows, self.columns)

        with self.assertRaises(KeyError):
            table.group_by('bad')