Example #1
0
    def test_where_preserves_rows(self):
        table = Table(self.rows, self.column_types, self.column_names)
        table2 = table.where(lambda r: r['one'] == 1)
        table3 = table2.where(lambda r: r['one'] == 1)

        self.assertIsNot(table._data[0], table2._data[0])
        self.assertIs(table2._data[0], table3._data[0])
Example #2
0
    def test_where_preserves_rows(self):
        table = Table(self.rows, self.column_types, self.column_names)
        table2 = table.where(lambda r: r['one'] == 1)
        table3 = table2.where(lambda r: r['one'] == 1)

        self.assertIsNot(table._data[0], table2._data[0])
        self.assertIs(table2._data[0], table3._data[0])
Example #3
0
    def test_where(self):
        table = Table(self.rows, self.column_types, self.column_names)

        new_table = table.where(lambda r: r['one'] in (2, None))

        self.assertIsNot(new_table, table)
        self.assertEqual(len(new_table.rows), 2)
        self.assertSequenceEqual(new_table.rows[0], (2, 3, 'b'))
        self.assertSequenceEqual(new_table.columns['one'], (2, None))
Example #4
0
    def test_where(self):
        table = Table(self.rows, self.column_types, self.column_names)

        new_table = table.where(lambda r: r['one'] in (2, None))

        self.assertIsNot(new_table, table)
        self.assertEqual(len(new_table.rows), 2)
        self.assertSequenceEqual(new_table.rows[0], (2, 3, 'b'))
        self.assertSequenceEqual(new_table.columns['one'], (2, None))
Example #5
0
with open('examples/realdata/Datagov_FY10_EDU_recp_by_State.csv') as f:
    # Skip headers
    next(f)
    next(f)
    next(f)

    rows = list(csv.reader(f))

# Trim cruft off end
rows = rows[:-2]

# Create the table
table = Table(rows, COLUMN_TYPES, COLUMN_NAMES)

# Remove Phillipines and Puerto Rico
states = table.where(lambda r: r['state_abbr'] not in ('PR', 'PH'))

# Sum total of all states
print('Total of all states: %i' % states.columns['total'].sum())

# Sort state total, descending
order_by_total_desc = states.order_by('total', reverse=True)

# Grab just the top 5 states
top_five = order_by_total_desc.rows[:5]

for i, row in enumerate(top_five):
    print('# %i: %s %i' % (i, row['state'], row['total']))

with open('sorted.csv', 'w') as f:
    writer = csv.writer(f)