本文整理汇总了Python中journalism.Table.compute方法的典型用法代码示例。如果您正苦于以下问题:Python Table.compute方法的具体用法?Python Table.compute怎么用?Python Table.compute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类journalism.Table
的用法示例。
在下文中一共展示了Table.compute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_compute_creates_rows
# 需要导入模块: from journalism import Table [as 别名]
# 或者: from journalism.Table import compute [as 别名]
def test_compute_creates_rows(self):
table = Table(self.rows, self.column_types, self.column_names)
table2 = table.compute('new2', self.number_type, lambda r: r['one'])
table3 = table2.compute('new3', self.number_type, lambda r: r['one'])
self.assertIsNot(table._data[0], table2._data[0])
self.assertNotEqual(table._data[0], table2._data[0])
self.assertIsNot(table2._data[0], table3._data[0])
self.assertNotEqual(table2._data[0], table3._data[0])
self.assertSequenceEqual(table._data[0], (1, 4, 'a'))
示例2: TestTableCompute
# 需要导入模块: from journalism import Table [as 别名]
# 或者: from journalism.Table import compute [as 别名]
class TestTableCompute(unittest.TestCase):
def setUp(self):
self.rows = (
('a', 2, 3, 4),
(None, 3, 5, None),
('a', 2, 4, None),
('b', 3, 4, None)
)
self.number_type = NumberType()
self.text_type = TextType()
self.column_types = (self.text_type, self.number_type, self.number_type, self.number_type)
self.column_names = ('one', 'two', 'three', 'four')
self.table = Table(self.rows, self.column_types, self.column_names)
def test_compute(self):
new_table = self.table.compute('test', self.number_type, lambda r: r['two'] + r['three'])
self.assertIsNot(new_table, self.table)
self.assertEqual(len(new_table.rows), 4)
self.assertEqual(len(new_table.columns), 5)
self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 5))
self.assertSequenceEqual(new_table.columns['test'], (5, 8, 6, 7))
def test_percent_change(self):
new_table = self.table.percent_change('two', 'three', 'test')
self.assertIsNot(new_table, self.table)
self.assertEqual(len(new_table.rows), 4)
self.assertEqual(len(new_table.columns), 5)
to_one_place = lambda d: d.quantize(Decimal('0.1'))
self.assertSequenceEqual(new_table.rows[0], ('a', Decimal('2'), Decimal('3'), Decimal('4'), Decimal('50.0')))
self.assertEqual(to_one_place(new_table.columns['test'][0]), Decimal('50.0'))
self.assertEqual(to_one_place(new_table.columns['test'][1]), Decimal('66.7'))
self.assertEqual(to_one_place(new_table.columns['test'][2]), Decimal('100.0'))
self.assertEqual(to_one_place(new_table.columns['test'][3]), Decimal('33.3'))
def test_rank(self):
new_table = self.table.rank(lambda r: r['two'], 'rank')
self.assertEqual(len(new_table.rows), 4)
self.assertEqual(len(new_table.columns), 5)
self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 1))
self.assertSequenceEqual(new_table.rows[1], (None, 3, 5, None, 3))
self.assertSequenceEqual(new_table.rows[2], ('a', 2, 4, None, 1))
self.assertSequenceEqual(new_table.rows[3], ('b', 3, 4, None, 3))
self.assertSequenceEqual(new_table.columns['rank'], (1, 3, 1, 3))
def test_rank2(self):
new_table = self.table.rank(lambda r: r['one'], 'rank')
self.assertEqual(len(new_table.rows), 4)
self.assertEqual(len(new_table.columns), 5)
self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 1))
self.assertSequenceEqual(new_table.rows[1], (None, 3, 5, None, 4))
self.assertSequenceEqual(new_table.rows[2], ('a', 2, 4, None, 1))
self.assertSequenceEqual(new_table.rows[3], ('b', 3, 4, None, 3))
self.assertSequenceEqual(new_table.columns['rank'], (1, 4, 1, 3))
def test_rank_column_name(self):
new_table = self.table.rank('two', 'rank')
self.assertEqual(len(new_table.rows), 4)
self.assertEqual(len(new_table.columns), 5)
self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 1))
self.assertSequenceEqual(new_table.rows[1], (None, 3, 5, None, 3))
self.assertSequenceEqual(new_table.rows[2], ('a', 2, 4, None, 1))
self.assertSequenceEqual(new_table.rows[3], ('b', 3, 4, None, 3))
self.assertSequenceEqual(new_table.columns['rank'], (1, 3, 1, 3))