Beispiel #1
0
 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_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]])
Beispiel #4
0
 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']])
Beispiel #5
0
 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_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]])
Beispiel #7
0
    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]])
Beispiel #8
0
 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]])
Beispiel #9
0
 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]])
Beispiel #10
0
#!/usr/bin/env python
# coding: utf-8
# title = Appending a column
#You can append a column in your `Table` object using the `append_column`
#method. You can pass new column's values or a function to generate the value
#based on row data. Let's see how it works - it's simple.

from outputty import Table


table = Table(headers=['Name', 'Creation Year'])
table.append(['Python', 1991])
table.append(['Unix', 1969])

#We have the values, so we'll append it:
table.append_column('Category', ['Programming Language', 'Operating System'])

#We can also generate the values:
table.append_column('Age', lambda row: 2012 - row[1]) #row is a list
#Our function can receive row as dict (with `row_as_dict` parameter) and we
#can insert the column where we want (with `position` parameter):
table.append_column('First Letter', lambda row: row['Name'][0],
                    row_as_dict=True, position=0) #row is dict
#...and the result:
print table
Beispiel #11
0
#!/usr/bin/env python
# coding: utf-8
# title = Appending a column
#You can append a column in your ``Table`` object using the ``append_column``
#method or just setting an item (``my_table['new-column'] = ...``). You can
#pass a list of values or a function to generate the values based on row data.
#Let's see how it works - it's quite simple.

from outputty import Table

table = Table(headers=['Name', 'Creation Year'])
table.append(['Python', 1991])
table.append(['Unix', 1969])

#We have the values, so we'll append it:
table.append_column('Category', ['Programming Language', 'Operating System'])
#Same effect for this line:
#table['Category'] = ['Programming Language', 'Operating System']

#We can also generate the values:
table.append_column('Age', lambda row: 2012 - row[1])  #row is a list
#Our function can receive row as dict (with `row_as_dict` parameter) and we
#can insert the column where we want (with `position` parameter):
table.append_column('First Letter',
                    lambda row: row['Name'][0],
                    row_as_dict=True,
                    position=0)  #row is dict
#...and the result:
print table
 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_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_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]])