def test_to_dict_should_handle_encodings_correctly(self): table = Table(headers=['spam', 'eggs', 'ham'], input_encoding='iso-8859-1', output_encoding='utf16') table.append([42, 3.14, 2.71]) table.append(['python', 'rules', 'yeh']) table.append([u'Álvaro'.encode('iso-8859-1'), '...', 'Justen']) table_dict = table.to_dict(key='spam', value='ham') expected = { 42: 2.71, u'python'.encode('utf16'): u'yeh'.encode('utf16'), u'Álvaro'.encode('utf16'): u'Justen'.encode('utf16') } self.assertEqual(table_dict, expected) table_dict_2 = table.to_dict() expected_2 = { u'spam'.encode('utf16'): [42, u'python'.encode('utf16'), u'Álvaro'.encode('utf16')], u'eggs'.encode('utf16'): [3.14, u'rules'.encode('utf16'), u'...'.encode('utf16')], u'ham'.encode('utf16'): [2.71, u'yeh'.encode('utf16'), u'Justen'.encode('utf16')] } self.assertEqual(table_dict_2, expected_2)
def test_to_dict_should_filter_some_columns(self): table = Table(headers=['spam', 'eggs', 'ham']) table.append([42, 3.14, 2.71]) table.append(['python', 'rules', 'yeh']) table_dict = table.to_dict(only=('eggs', 'ham')) expected = {'eggs': [3.14, 'rules'], 'ham': [2.71, 'yeh']} self.assertEqual(table_dict, expected)
def test_to_dict_should_filter_create_dict_from_values(self): table = Table(headers=['spam', 'eggs', 'ham']) table.append([42, 3.14, 2.71]) table.append(['python', 'rules', 'yeh']) table_dict = table.to_dict(key='spam', value='ham') expected = {42: 2.71, 'python': 'yeh'} self.assertEqual(table_dict, expected)
def test_to_dict_should_filter_create_dict_from_values(self): table = Table(headers=["spam", "eggs", "ham"]) table.append([42, 3.14, 2.71]) table.append(["python", "rules", "yeh"]) table_dict = table.to_dict(key="spam", value="ham") expected = {42: 2.71, "python": "yeh"} self.assertEqual(table_dict, expected)
def test_to_dict_should_create_a_dict_with_column_names_and_values(self): table = Table(headers=['spam', 'eggs']) table.append([42, 3.14]) table.append(['python', 'rules']) table_dict = table.to_dict() expected = {'spam': [42, 'python'], 'eggs': [3.14, 'rules']} self.assertEqual(table_dict, expected)
def test_to_dict_should_filter_some_columns(self): table = Table(headers=["spam", "eggs", "ham"]) table.append([42, 3.14, 2.71]) table.append(["python", "rules", "yeh"]) table_dict = table.to_dict(only=("eggs", "ham")) expected = {"eggs": [3.14, "rules"], "ham": [2.71, "yeh"]} self.assertEqual(table_dict, expected)
def test_to_dict_should_create_a_dict_with_column_names_and_values(self): table = Table(headers=["spam", "eggs"]) table.append([42, 3.14]) table.append(["python", "rules"]) table_dict = table.to_dict() expected = {"spam": [42, "python"], "eggs": [3.14, "rules"]} self.assertEqual(table_dict, expected)
def test_to_dict_should_handle_encodings_correctly(self): table = Table(headers=["spam", "eggs", "ham"], input_encoding="iso-8859-1", output_encoding="utf16") table.append([42, 3.14, 2.71]) table.append(["python", "rules", "yeh"]) table.append([u"Álvaro".encode("iso-8859-1"), "...", "Justen"]) table_dict = table.to_dict(key="spam", value="ham") expected = { 42: 2.71, u"python".encode("utf16"): u"yeh".encode("utf16"), u"Álvaro".encode("utf16"): u"Justen".encode("utf16"), } self.assertEqual(table_dict, expected) table_dict_2 = table.to_dict() expected_2 = { u"spam".encode("utf16"): [42, u"python".encode("utf16"), u"Álvaro".encode("utf16")], u"eggs".encode("utf16"): [3.14, u"rules".encode("utf16"), u"...".encode("utf16")], u"ham".encode("utf16"): [2.71, u"yeh".encode("utf16"), u"Justen".encode("utf16")], } self.assertEqual(table_dict_2, expected_2)
#!/usr/bin/env python # coding: utf-8 #A `Table` made with `dict`-like, `list`-like and `tuple`-like objects. For #example, this code: from outputty import Table my_table = Table(headers=['First Name', 'Last Name', 'Main Language']) my_table.append({'First Name': 'Álvaro', 'Last Name': 'Justen', 'Main Language': 'Python'}) my_table.append(('Flávio', 'Amieiro', 'Python')) rows = my_table.to_list_of_dicts() print rows[1]['First Name'] table_dict = my_table.to_dict() print table_dict table_dict_filtered = my_table.to_dict(only=['First Name', 'Last Name']) print table_dict_filtered other_table = Table(headers=['date', 'measure']) other_table.append(('2011-12-01', 21)) other_table.append(('2011-12-02', 42)) other_table.append(('2011-12-03', 3.14)) other_table.append(('2011-12-04', 2.71)) values_as_dict = other_table.to_dict(key='date', value='measure') print values_as_dict
# coding: utf-8 #A `Table` made with `dict`-like, `list`-like and `tuple`-like objects. For #example, this code: from outputty import Table my_table = Table(headers=['First Name', 'Last Name', 'Main Language']) my_table.append({ 'First Name': 'Álvaro', 'Last Name': 'Justen', 'Main Language': 'Python' }) my_table.append(('Flávio', 'Amieiro', 'Python')) rows = my_table.to_list_of_dicts() print rows[1]['First Name'] table_dict = my_table.to_dict() print table_dict table_dict_filtered = my_table.to_dict(only=['First Name', 'Last Name']) print table_dict_filtered other_table = Table(headers=['date', 'measure']) other_table.append(('2011-12-01', 21)) other_table.append(('2011-12-02', 42)) other_table.append(('2011-12-03', 3.14)) other_table.append(('2011-12-04', 2.71)) values_as_dict = other_table.to_dict(key='date', value='measure') print values_as_dict