def test_table_remove(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [1, 2]]) table.remove({'python': 3, 'rules': 4}) self.assertEquals(table[:], [[1, 2], [5, 6], [1, 2]]) table.remove([1, 2]) self.assertEquals(table[:], [[5, 6], [1, 2]])
def test_table_extend_should_add_nothing_if_an_exception_is_raised(self): table = Table(headers=['spam', 'eggs']) table.append(['hello', 'world']) with self.assertRaises(ValueError): table.extend([['python', 'rules'], ['answer', 42], [1, 2, 3]]) self.assertEquals(table[0], ['hello', 'world']) self.assertEquals(len(table), 1)
def test_table_append_column_should_raise_ValueError_when_wrong_size(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) with self.assertRaises(ValueError): table.append_column("new column", [3, 5, 7, 8]) self.assertEquals(table.headers, ["python", "rules"]) self.assertEquals(table[:], [[1, 2], [3, 4], [5, 6]])
def test_append_column_should_raise_ValueError_when_column_already_exists(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) with self.assertRaises(ValueError): table.append_column("python", [3, 5, 7]) self.assertEquals(table.headers, ["python", "rules"]) self.assertEquals(table[:], [[1, 2], [3, 4], [5, 6]])
def test_table_remove(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6], [1, 2]]) table.remove({"python": 3, "rules": 4}) self.assertEquals(table[:], [[1, 2], [5, 6], [1, 2]]) table.remove([1, 2]) self.assertEquals(table[:], [[5, 6], [1, 2]])
def test_table_append_column_should_raise_ValueError_when_wrong_size(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) with self.assertRaises(ValueError): table.append_column('new column', [3, 5, 7, 8]) self.assertEquals(table.headers, ['python', 'rules']) self.assertEquals(table[:], [[1, 2], [3, 4], [5, 6]])
def test_table_extend_should_add_nothing_if_an_exception_is_raised(self): table = Table(headers=["spam", "eggs"]) table.append(["hello", "world"]) with self.assertRaises(ValueError): table.extend([["python", "rules"], ["answer", 42], [1, 2, 3]]) self.assertEquals(table[0], ["hello", "world"]) self.assertEquals(len(table), 1)
def test_table_should_be_a_sequence(self): table = Table(headers=["spam", "eggs"]) table.extend([["python", "rules"], ["answer", 42]]) items = [] for item in table: items.append(item) self.assertEquals(items[0], ["python", "rules"]) self.assertEquals(items[1], ["answer", 42])
def test_append_column_should_raise_ValueError_when_column_already_exists( self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) with self.assertRaises(ValueError): table.append_column('python', [3, 5, 7]) self.assertEquals(table.headers, ['python', 'rules']) self.assertEquals(table[:], [[1, 2], [3, 4], [5, 6]])
def test_table_insert(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2]]) table.insert(0, [4, 2]) table.insert(1, {'python': 9, 'rules': 9}) self.assertEquals(table[0], [4, 2]) self.assertEquals(table[1], [9, 9]) self.assertEquals(len(table), 7)
def test_append_column_should_decode_strings(self): table = Table(headers=['python', 'rules'], input_encoding='iso-8859-1') table.extend([[1, 2], [3, 4]]) table.append_column( 'new column', [u'Álvaro'.encode('iso-8859-1'), u'Píton'.encode('iso-8859-1')]) self.assertEquals(table.headers, ['python', 'rules', 'new column']) self.assertEquals(table[:], [[1, 2, u'Álvaro'], [3, 4, u'Píton']])
def test_table_should_be_a_sequence(self): table = Table(headers=['spam', 'eggs']) table.extend([['python', 'rules'], ['answer', 42]]) items = [] for item in table: items.append(item) self.assertEquals(items[0], ['python', 'rules']) self.assertEquals(items[1], ['answer', 42])
def test_table_insert(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2]]) table.insert(0, [4, 2]) table.insert(1, {"python": 9, "rules": 9}) self.assertEquals(table[0], [4, 2]) self.assertEquals(table[1], [9, 9]) self.assertEquals(len(table), 7)
def test_table_set_item_should_work_for_column(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) table["python"] = [2, 4, 6] self.assertEquals(table[:], [[2, 2], [4, 4], [6, 6]]) with self.assertRaises(KeyError): should_raise_exception = table["not-found"] with self.assertRaises(ValueError): table["rules"] = [1, 2, 3, 4]
def test_table_set_item_should_work_for_column(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) table['python'] = [2, 4, 6] self.assertEquals(table[:], [[2, 2], [4, 4], [6, 6]]) with self.assertRaises(KeyError): should_raise_exception = table['not-found'] with self.assertRaises(ValueError): table['rules'] = [1, 2, 3, 4]
def test_table_index(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2], [9, 0]]) self.assertEquals(table.index([1, 2]), 0) self.assertEquals(table.index({'python': 1, 'rules': 2}), 0) self.assertEquals(table.index([5, 6]), 2) self.assertEquals(table.index([1, 2], 1), 4) with self.assertRaises(ValueError): non_ecxiste = table.index([1, 9]) with self.assertRaises(ValueError): not_found = table.index([1, 2], 1, 3)
def test_table_index(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2], [9, 0]]) self.assertEquals(table.index([1, 2]), 0) self.assertEquals(table.index({"python": 1, "rules": 2}), 0) self.assertEquals(table.index([5, 6]), 2) self.assertEquals(table.index([1, 2], 1), 4) with self.assertRaises(ValueError): non_ecxiste = table.index([1, 9]) with self.assertRaises(ValueError): not_found = table.index([1, 2], 1, 3)
def test_table_set_item_should_work_for_rows(self): table = Table(headers=["python", "rules"]) table.append([1, 2]) table[0] = [4, 2] self.assertEquals(table[:], [[4, 2]]) table.extend([[3, 4], [5, 6]]) table[1:] = [[7, 8], [9, 0]] self.assertEquals(table[:], [[4, 2], [7, 8], [9, 0]]) table[-3:] = [[5, 5], [7, 7], [9, 9]] self.assertEquals(table[:], [[5, 5], [7, 7], [9, 9]]) with self.assertRaises(ValueError): table[1] = [1, 2, 3] with self.assertRaises(ValueError): table[(1, 2)] = [1, 2]
def test_table_set_item_should_work_for_rows(self): table = Table(headers=['python', 'rules']) table.append([1, 2]) table[0] = [4, 2] self.assertEquals(table[:], [[4, 2]]) table.extend([[3, 4], [5, 6]]) table[1:] = [[7, 8], [9, 0]] self.assertEquals(table[:], [[4, 2], [7, 8], [9, 0]]) table[-3:] = [[5, 5], [7, 7], [9, 9]] self.assertEquals(table[:], [[5, 5], [7, 7], [9, 9]]) with self.assertRaises(ValueError): table[1] = [1, 2, 3] with self.assertRaises(ValueError): table[(1, 2)] = [1, 2]
def test_ordering_ascending(self): table = Table(headers=["spam"]) table.extend([[5], [3], [7], [10]]) table.order_by("spam", "ascending") table_2 = Table(headers=["spam"]) table_2.extend([[5], [3], [7], [10]]) table_2.order_by("spam", "asc") table_3 = Table(headers=["spam"]) table_3.extend([[5], [3], [7], [10]]) table_3.order_by("spam", "ASCENDING") table_4 = Table(headers=["spam"]) table_4.extend([[5], [3], [7], [10]]) table_4.order_by("spam", "ASC") expected_output = dedent( """ +------+ | spam | +------+ | 3 | | 5 | | 7 | | 10 | +------+ """ ).strip() self.assertEqual(str(table), expected_output) self.assertEqual(str(table_2), expected_output)
def test_ordering_ascending(self): table = Table(headers=['spam']) table.extend([[5], [3], [7], [10]]) table.order_by('spam', 'ascending') table_2 = Table(headers=['spam']) table_2.extend([[5], [3], [7], [10]]) table_2.order_by('spam', 'asc') table_3 = Table(headers=['spam']) table_3.extend([[5], [3], [7], [10]]) table_3.order_by('spam', 'ASCENDING') table_4 = Table(headers=['spam']) table_4.extend([[5], [3], [7], [10]]) table_4.order_by('spam', 'ASC') expected_output = dedent(''' +------+ | spam | +------+ | 3 | | 5 | | 7 | | 10 | +------+ ''').strip() self.assertEqual(str(table), expected_output) self.assertEqual(str(table_2), expected_output)
def test_order_by_method_should_order_ascending_by_default(self): table = Table(headers=['spam']) table.extend([[5], [3], [7], [10]]) table.order_by('spam') expected_output = dedent(''' +------+ | spam | +------+ | 3 | | 5 | | 7 | | 10 | +------+ ''').strip() self.assertEqual(str(table), expected_output)
def test_vertical_histogram(self): seed(1234) # Setting the seed to get repeatable results numbers = normal(size=1000) my_table = Table(headers=['values']) my_table.extend([[value] for value in numbers]) output = my_table.write('histogram', column='values', height=5, orientation='vertical', bins=10) expected = dedent(''' 265 | || ||| |||| |||||| -3.56 2.76 ''').strip() self.assertEquals(output, expected)
def test_append_column_should_accept_a_function_to_create_new_column(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4]]) table.append_column("new column", lambda row: row[0] + row[1]) self.assertEquals(table.headers, ["python", "rules", "new column"]) self.assertEquals(table[:], [[1, 2, 3], [3, 4, 7]]) del table["new column"] table.append_column("other column", lambda row: row[0] + row[1], position=0) self.assertEquals(table.headers, ["other column", "python", "rules"]) self.assertEquals(table[:], [[3, 1, 2], [7, 3, 4]]) del table["other column"] table.append_column("third column", lambda r: r["python"] * r["rules"], row_as_dict=True) self.assertEquals(table.headers, ["python", "rules", "third column"]) self.assertEquals(table[:], [[1, 2, 2], [3, 4, 12]])
def test_order_by_method_should_order_ascending_by_default(self): table = Table(headers=["spam"]) table.extend([[5], [3], [7], [10]]) table.order_by("spam") expected_output = dedent( """ +------+ | spam | +------+ | 3 | | 5 | | 7 | | 10 | +------+ """ ).strip() self.assertEqual(str(table), expected_output)
def test_append_column_should_accept_a_function_to_create_new_column(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4]]) table.append_column('new column', lambda row: row[0] + row[1]) self.assertEquals(table.headers, ['python', 'rules', 'new column']) self.assertEquals(table[:], [[1, 2, 3], [3, 4, 7]]) del table['new column'] table.append_column('other column', lambda row: row[0] + row[1], position=0) self.assertEquals(table.headers, ['other column', 'python', 'rules']) self.assertEquals(table[:], [[3, 1, 2], [7, 3, 4]]) del table['other column'] table.append_column('third column', lambda r: r['python'] * r['rules'], row_as_dict=True) self.assertEquals(table.headers, ['python', 'rules', 'third column']) self.assertEquals(table[:], [[1, 2, 2], [3, 4, 12]])
def test_horizontal_histogram(self): seed(1234) # Setting the seed to get repeatable results numbers = normal(size=1000) my_table = Table(headers=['values']) my_table.extend([[value] for value in numbers]) output = my_table.write('histogram', column='values', height=15, bins=10, orientation='horizontal') expected = dedent('''\ 265 -3.56: -2.93: -2.30: || -1.67: |||| -1.03: |||||||||| -0.40: ||||||||||||||| 0.23 : |||||||||||| 0.87 : |||||| 1.50 : ||| 2.13 :''') self.assertEquals(output, expected)
def test_append_column_should_be_inserted_in_any_position(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4]]) table.append_column('new column', [3, 5], position=0) self.assertEquals(table.headers, ['new column', 'python', 'rules']) self.assertEquals(table[:], [[3, 1, 2], [5, 3, 4]])
def test_table_get_item(self): table = Table(headers=["spam", "eggs"]) table.extend([["python", "rules"], ["answer", 42]]) self.assertEquals(table[0], ["python", "rules"]) self.assertEquals(table[1], ["answer", 42])
def test_table_append_column_should_work_as_item_assignment(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) table['new column'] = [3, 5, 7] self.assertEquals(table.headers, ['python', 'rules', 'new column']) self.assertEquals(table[:], [[1, 2, 3], [3, 4, 5], [5, 6, 7]])
def test_table_del_item_should_work_with_rows(self): table = Table(headers=["spam", "eggs"]) table.extend([["python", "rules"], ["answer", 42]]) del table[0] self.assertEquals(table[0], ["answer", 42])
def test_table_reverse(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) table.reverse() self.assertEquals(table[:], [[5, 6], [3, 4], [1, 2]])
def test_table_pop(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) self.assertEquals(table.pop(), [5, 6]) self.assertEquals(table.pop(0), [1, 2]) self.assertEquals(len(table), 1)
def test_table_append_column_should_change_headers_and_rows(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) table.append_column("new column", [3, 5, 7]) self.assertEquals(table.headers, ["python", "rules", "new column"]) self.assertEquals(table[:], [[1, 2, 3], [3, 4, 5], [5, 6, 7]])
def test_table_extend(self): table = Table(headers=['spam', 'eggs']) table.extend([['python', 'rules'], ['answer', 42], {'eggs': 123}]) self.assertEquals(table[0], ['python', 'rules']) self.assertEquals(table[1], ['answer', 42]) self.assertEquals(table[2], [None, 123])
def test_table_append_column_should_work_as_item_assignment(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) table["new column"] = [3, 5, 7] self.assertEquals(table.headers, ["python", "rules", "new column"]) self.assertEquals(table[:], [[1, 2, 3], [3, 4, 5], [5, 6, 7]])
def test_append_column_should_decode_strings(self): table = Table(headers=["python", "rules"], input_encoding="iso-8859-1") table.extend([[1, 2], [3, 4]]) table.append_column("new column", [u"Álvaro".encode("iso-8859-1"), u"Píton".encode("iso-8859-1")]) self.assertEquals(table.headers, ["python", "rules", "new column"]) self.assertEquals(table[:], [[1, 2, u"Álvaro"], [3, 4, u"Píton"]])
def test_append_column_should_be_inserted_in_any_position(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4]]) table.append_column("new column", [3, 5], position=0) self.assertEquals(table.headers, ["new column", "python", "rules"]) self.assertEquals(table[:], [[3, 1, 2], [5, 3, 4]])
def test_table_pop(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) self.assertEquals(table.pop(), [5, 6]) self.assertEquals(table.pop(0), [1, 2]) self.assertEquals(len(table), 1)
def test_table_get_item(self): table = Table(headers=['spam', 'eggs']) table.extend([['python', 'rules'], ['answer', 42]]) self.assertEquals(table[0], ['python', 'rules']) self.assertEquals(table[1], ['answer', 42])
def test_table_count(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2]]) self.assertEquals(table.count([1, 2]), 2)
def test_table_get_item_should_raise_IndexError_when_row_not_found(self): table = Table(headers=['spam', 'eggs']) table.extend([['python', 'rules'], ['answer', 42]]) with self.assertRaises(IndexError): table[2]
def test_del_slice(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]) del table[1:3] self.assertEquals(table[:], [[1, 2], [7, 8], [9, 0]])
def test_table_del_item_should_work_with_rows(self): table = Table(headers=['spam', 'eggs']) table.extend([['python', 'rules'], ['answer', 42]]) del table[0] self.assertEquals(table[0], ['answer', 42])
def test_len_table_should_return_len_of_rows(self): table = Table(headers=["spam", "eggs"]) table.extend([["python", "rules"], ["answer", 42]]) self.assertEquals(len(table), 2)
def test_len_table_should_return_len_of_rows(self): table = Table(headers=['spam', 'eggs']) table.extend([['python', 'rules'], ['answer', 42]]) self.assertEquals(len(table), 2)
def test_table_append_column_should_change_headers_and_rows(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) table.append_column('new column', [3, 5, 7]) self.assertEquals(table.headers, ['python', 'rules', 'new column']) self.assertEquals(table[:], [[1, 2, 3], [3, 4, 5], [5, 6, 7]])
def test_table_reverse(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) table.reverse() self.assertEquals(table[:], [[5, 6], [3, 4], [1, 2]])
def test_table_get_item_should_raise_IndexError_when_row_not_found(self): table = Table(headers=["spam", "eggs"]) table.extend([["python", "rules"], ["answer", 42]]) with self.assertRaises(IndexError): table[2]
def test_del_slice(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]) del table[1:3] self.assertEquals(table[:], [[1, 2], [7, 8], [9, 0]])
def test_table_extend(self): table = Table(headers=["spam", "eggs"]) table.extend([["python", "rules"], ["answer", 42], {"eggs": 123}]) self.assertEquals(table[0], ["python", "rules"]) self.assertEquals(table[1], ["answer", 42]) self.assertEquals(table[2], [None, 123])
def test_table_count(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2]]) self.assertEquals(table.count([1, 2]), 2)
#!/usr/bin/env python # coding: utf-8 # title = Creating Histograms # There is a plugin called ``histogram`` that is shipped by default with # ``outputty`` - it can create histograms of your table's columns (using # ``numpy``). The output will be the histogram represented as text. from numpy.random import normal from numpy.random import seed from outputty import Table seed(1234) distribution = normal(size=1000) my_table = Table(headers=["numbers"]) my_table.extend([[value] for value in distribution]) print "Vertical:" print my_table.write("histogram", "numbers", "vertical", bins=10, height=7) print print "Horizontal:" print my_table.write("histogram", "numbers", "horizontal", bins=10, height=7, character="#")
#!/usr/bin/env python # coding: utf-8 # title = Creating Histograms #There is a plugin called ``histogram`` that is shipped by default with #``outputty`` - it can create histograms of your table's columns (using #``numpy``). The output will be the histogram represented as text. from numpy.random import normal from numpy.random import seed from outputty import Table seed(1234) distribution = normal(size=1000) my_table = Table(headers=['numbers']) my_table.extend([[value] for value in distribution]) print 'Vertical:' print my_table.write('histogram', 'numbers', 'vertical', bins=10, height=7) print print 'Horizontal:' print my_table.write('histogram', 'numbers', 'horizontal', bins=10, height=7, character='#')