Exemple #1
0
 def test_to_html_with_a_parameter_should_save_a_file(self):
     temp_fp = tempfile.NamedTemporaryFile(delete=False)
     temp_fp.close()
     my_table = Table(headers=['ham', 'spam', 'eggs'])
     my_table.append(['python', 'rules', '!'])
     my_table.append({'ham': 'spam', 'spam': 'eggs', 'eggs': 'ham'})
     my_table.write('html', temp_fp.name, css_classes=False)
     temp_fp = open(temp_fp.name)
     output = temp_fp.read()
     temp_fp.close()
     os.remove(temp_fp.name)
     expected = dedent('''
     <table>
       <thead>
         <tr>
           <th>ham</th>
           <th>spam</th>
           <th>eggs</th>
         </tr>
       </thead>
       <tbody>
         <tr>
           <td>python</td>
           <td>rules</td>
           <td>!</td>
         </tr>
         <tr>
           <td>spam</td>
           <td>eggs</td>
           <td>ham</td>
         </tr>
       </tbody>
     </table>
     ''').strip()
     self.assertEquals(output, expected)
Exemple #2
0
    def test_read_should_automatically_identify_data_types(self):
        self.connection.query('DROP TABLE ' + self.table)
        self.connection.commit()
        table = Table(headers=['spam', 'eggs', 'ham', 'monty', 'python'])
        numbers = range(1000)
        for i in numbers:
            table.append([
                i, i + 0.1, 'some string', '2012-01-01', '2011-12-31 23:59:59'
            ])
        table.write('mysql', self.connection_string)
        new_table = Table()
        new_table.read('mysql', self.connection_string)
        self.assertEquals(len(new_table), 1000)
        self.assertEquals(new_table.types['spam'], int)
        self.assertEquals(new_table.types['eggs'], float)
        self.assertEquals(new_table.types['ham'], str)
        self.assertEquals(new_table.types['monty'], datetime.date)
        self.assertEquals(new_table.types['python'], datetime.datetime)

        connection_string = '/'.join(self.connection_string.split('/')[:-1])
        other_table = Table()
        other_table.read('mysql',
                         connection_string,
                         query='SELECT spam, ham, python FROM ' + self.table)
        self.assertEquals(len(other_table), 1000)
        self.assertEquals(len(other_table.types), 3)
        self.assertEquals(other_table.types['spam'], int)
        self.assertEquals(other_table.types['ham'], str)
        self.assertEquals(other_table.types['python'], datetime.datetime)
    def test_should_save_data_into_text_file(self):
        temp_fp = tempfile.NamedTemporaryFile(delete=False)
        temp_fp.close()

        my_table = Table(headers=['ham', 'spam', 'eggs'])
        my_table.append({'ham': '', 'spam': '', 'eggs': ''})
        my_table.append({'ham': 1, 'spam': 2, 'eggs': 3})
        my_table.append({'ham': 11, 'spam': 22, 'eggs': 33})

        my_table.write('text', temp_fp.name)
        output = my_table.write('text')
        fp = open(temp_fp.name, 'r')
        contents = fp.read()
        fp.close()
        os.remove(temp_fp.name)
        self.assertEqual(contents, dedent('''
        +-----+------+------+
        | ham | spam | eggs |
        +-----+------+------+
        |     |      |      |
        |   1 |    2 |    3 |
        |  11 |   22 |   33 |
        +-----+------+------+
        ''').strip())
        self.assertEquals(contents, output)
Exemple #4
0
    def test_should_save_data_into_text_file(self):
        temp_fp = tempfile.NamedTemporaryFile(delete=False)
        temp_fp.close()

        my_table = Table(headers=['ham', 'spam', 'eggs'])
        my_table.append({'ham': '', 'spam': '', 'eggs': ''})
        my_table.append({'ham': 1, 'spam': 2, 'eggs': 3})
        my_table.append({'ham': 11, 'spam': 22, 'eggs': 33})

        my_table.write('text', temp_fp.name)
        output = my_table.write('text')
        fp = open(temp_fp.name, 'r')
        contents = fp.read()
        fp.close()
        os.remove(temp_fp.name)
        self.assertEqual(
            contents,
            dedent('''
        +-----+------+------+
        | ham | spam | eggs |
        +-----+------+------+
        |     |      |      |
        |   1 |    2 |    3 |
        |  11 |   22 |   33 |
        +-----+------+------+
        ''').strip())
        self.assertEquals(contents, output)
Exemple #5
0
 def test_write_should_create_table_even_if_only_headers_present(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['spam', 'eggs'])
     table.write('mysql', self.connection_string)
     self.cursor.execute('SELECT * FROM ' + self.table)
     cols = [x[0] for x in self.cursor.description]
     self.assertEquals(set(cols), set(['spam', 'eggs']))
Exemple #6
0
 def test_should_deal_correctly_with_quotes(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['eggs'])
     table.append(['spam"ham'])
     table.write('mysql', self.connection_string)
     table_2 = Table()
     table_2.read('mysql', self.connection_string)
     self.assertEquals(table_2[0][0], 'spam"ham')
 def test_write_csv_should_accept_filepointer(self):
     temp_fp = tempfile.NamedTemporaryFile()
     my_table = Table(headers=['Álvaro'])
     my_table.append(['Píton'])
     my_table.write('csv', temp_fp)
     expected = '"Álvaro"\n"Píton"\n'
     temp_fp.seek(0)
     output = temp_fp.read()
     temp_fp.close()
     self.assertEqual(output, expected)
Exemple #8
0
 def test_read_should_accept_order_by(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['number'])
     numbers = range(1000)
     for i in numbers:
         table.append([i])
     table.write('mysql', self.connection_string)
     new_table = Table()
     new_table.read('mysql', self.connection_string, order_by='number desc')
     self.assertEquals(new_table[:], [[x] for x in numbers[::-1]])
Exemple #9
0
 def test_None_should_be_saved_as_NULL_and_returned_as_None(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python'])
     table.append([1, 2.71, '2011-01-01', '2011-01-01 00:00:00', 'asd'])
     table.append([None, None, None, None, None])
     table.write('mysql', self.connection_string)
     self.cursor.execute('SELECT * FROM ' + self.table)
     rows = self.cursor.fetchall()
     for i in range(5):
         self.assertIs(rows[-1][i], None)
Exemple #10
0
 def test_write_should_add_rows_correctly(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['spam', 'eggs'])
     table.append(['python', 'rules'])
     table.append(['free software', 'ownz'])
     table.write('mysql', self.connection_string)
     self.cursor.execute('SELECT * FROM ' + self.table)
     rows = [row for row in self.cursor.fetchall()]
     self.assertEquals(rows, [('python', 'rules'),
                              ('free software', 'ownz')])
Exemple #11
0
    def test_write_should_slugfy_column_names(self):
        self.connection.query('DROP TABLE ' + self.table)
        self.connection.commit()
        table = Table(headers=['col with  spaces', 'col-with-dashes'])
        table.append(['testing', 123])
        table.append(['testing again', 456])
        table.write('mysql', self.connection_string)

        other_table = Table()
        other_table.read('mysql', self.connection_string)
        self.assertEquals(other_table.headers,
                          ['col_with_spaces', 'col_with_dashes'])
Exemple #12
0
 def test_should_insert_data_in_correct_order(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['name', 'age'])
     table.append(['Justen', 24])
     table.append(['Someone', 99])
     table.write('mysql', self.connection_string)
     self.cursor.execute('SELECT * FROM ' + self.table)
     rows = [x for x in self.cursor.fetchall()]
     self.assertEquals(len(rows), 2)
     self.assertEquals(rows[0][0], 'Justen')
     self.assertEquals(rows[0][1], 24)
     self.assertEquals(rows[1][0], 'Someone')
     self.assertEquals(rows[1][1], 99)
    def test_input_and_output_encoding_should_affect_method_write_csv(self):
        temp_fp = tempfile.NamedTemporaryFile(delete=False)
        temp_fp.close()
        my_table = Table(headers=['Álvaro'.decode('utf8').encode('utf16')],
                         input_encoding='utf16', output_encoding='iso-8859-1')
        my_table.append(['Píton'.decode('utf8').encode('utf16')])
        my_table.write('csv', temp_fp.name)

        fp = open(temp_fp.name)
        file_contents = fp.read()
        fp.close()
        os.remove(temp_fp.name)
        output = '"Álvaro"\n"Píton"\n'.decode('utf8').encode('iso-8859-1')
        self.assertEqual(file_contents, output)
Exemple #14
0
 def test_should_deal_correctly_with_database_encoding(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.query('CREATE TABLE %s (spam TEXT, eggs TEXT)' % \
                           self.table)
     self.connection.commit()
     table = Table(headers=[u'spam', u'eggs'], input_encoding='utf16')
     table.append([u'Álvaro'.encode('utf16'), u'álvaro'.encode('utf16')])
     table.write('mysql', self.connection_string)
     self.cursor.execute('SELECT * FROM ' + self.table)
     rows = [x for x in self.cursor.fetchall()]
     db_encoding = self.connection.character_set_name()
     self.assertEquals(len(rows), 1)
     self.assertEquals(rows[0][0].decode(db_encoding), u'Álvaro')
     self.assertEquals(rows[0][1].decode(db_encoding), u'álvaro')
     self.assertEquals(table[0][0], u'Álvaro')
     self.assertEquals(table[0][1], u'álvaro')
Exemple #15
0
 def test_to_html_with_headers_and_rows_with_some_columns_empty(self):
     my_table = Table(headers=['ham', 'spam', 'eggs'])
     my_table.append({'ham': 'spam'})
     my_table.append({'spam': 'eggs'})
     my_table.append({'eggs': 'ham'})
     output = my_table.write('html', css_classes=False)
     expected = dedent('''
     <table>
       <thead>
         <tr>
           <th>ham</th>
           <th>spam</th>
           <th>eggs</th>
         </tr>
       </thead>
       <tbody>
         <tr>
           <td>spam</td>
           <td></td>
           <td></td>
         </tr>
         <tr>
           <td></td>
           <td>eggs</td>
           <td></td>
         </tr>
         <tr>
           <td></td>
           <td></td>
           <td>ham</td>
         </tr>
       </tbody>
     </table>
     ''').strip()
     self.assertEquals(output, expected)
Exemple #16
0
 def test_read_should_accept_a_SQL_instead_of_table_name(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['number'])
     numbers = range(1000)
     for i in numbers:
         table.append([i])
     table.write('mysql', self.connection_string)
     connection_string = '/'.join(self.connection_string.split('/')[:-1])
     new_table = Table()
     sql = ('SELECT * FROM {} WHERE number > 500 AND number < 510 ORDER BY '
            'number DESC LIMIT 2, 10')
     new_table.read('mysql',
                    connection_string,
                    query=sql.format(self.table))
     self.assertEquals(new_table[:], [[x] for x in range(507, 500, -1)])
    def test_should_be_able_to_change_delimiters_on_write(self):
        temp_fp = tempfile.NamedTemporaryFile()
        temp_fp.close()
        my_table = Table(headers=['ham', 'spam', 'eggs'])
        my_table.append({'ham': 'ham spam ham', 'spam': 'spam eggs spam',
                              'eggs': 'eggs ham eggs'})
        my_table.write('csv', temp_fp.name, delimiter=';', quote_char="'",
                       line_terminator='\r\n')
        fp = open(temp_fp.name)
        contents = fp.read()
        fp.close()
        os.remove(temp_fp.name)

        self.assertEquals(contents, dedent('''\
        'ham';'spam';'eggs'\r
        'ham spam ham';'spam eggs spam';'eggs ham eggs'\r
        '''))
Exemple #18
0
 def test_write_should_create_the_table_with_correct_data_types(self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python'])
     table.append([1, 2.71, '2011-01-01', '2011-01-01 00:00:00', 'asd'])
     table.append([2, 3.14, '2011-01-02', '2011-01-01 00:00:01', 'fgh'])
     table.append([3, 1.23, '2011-01-03', '2011-01-01 00:00:02', 'jkl'])
     table.append([4, 4.56, '2011-01-04', '2011-01-01 00:00:03', 'qwe'])
     table.append([5, 7.89, '2011-01-05', '2011-01-01 00:00:04', 'rty'])
     table.write('mysql', self.connection_string)
     self.cursor.execute('DESCRIBE ' + self.table)
     data_types = dict([[x[0], x[1]] for x in self.cursor.fetchall()])
     self.assertTrue(data_types['spam'].startswith('int'))
     self.assertEquals(data_types['eggs'], 'float')
     self.assertEquals(data_types['ham'], 'date')
     self.assertEquals(data_types['Monty'], 'datetime')
     self.assertEquals(data_types['Python'], 'text')
    def test_write_csv_should_create_the_file_correctly_with_headers(self):
        temp_fp = tempfile.NamedTemporaryFile()
        temp_fp.close()

        my_table = Table(headers=['ham', 'spam', 'eggs'])
        my_table.append({'ham': 'ham spam ham', 'spam': 'spam eggs spam',
                              'eggs': 'eggs ham eggs'})
        my_table.write('csv', temp_fp.name)

        fp = open(temp_fp.name)
        contents = fp.read()
        fp.close()
        os.remove(temp_fp.name)

        self.assertEquals(contents, dedent('''\
        "ham","spam","eggs"
        "ham spam ham","spam eggs spam","eggs ham eggs"
        '''))
    def test_input_and_output_character_encoding_in_method_to_text_file(self):
        temp_fp = tempfile.NamedTemporaryFile(delete=False)
        temp_fp.close()
        my_table = Table(headers=['Álvaro'.decode('utf8').encode('utf16')],
                         input_encoding='utf16', output_encoding='iso-8859-1')
        my_table.append(['Píton'.decode('utf8').encode('utf16')])
        my_table.write('text', temp_fp.name)

        fp = open(temp_fp.name)
        file_contents = fp.read()
        fp.close()
        os.remove(temp_fp.name)
        output = dedent('''
        +--------+
        | Álvaro |
        +--------+
        |  Píton |
        +--------+
        ''').strip().decode('utf8').encode('iso-8859-1')
        self.assertEqual(file_contents, output)
Exemple #21
0
    def test_input_and_output_character_encoding_in_method_to_text_file(self):
        temp_fp = tempfile.NamedTemporaryFile(delete=False)
        temp_fp.close()
        my_table = Table(headers=['Álvaro'.decode('utf8').encode('utf16')],
                         input_encoding='utf16',
                         output_encoding='iso-8859-1')
        my_table.append(['Píton'.decode('utf8').encode('utf16')])
        my_table.write('text', temp_fp.name)

        fp = open(temp_fp.name)
        file_contents = fp.read()
        fp.close()
        os.remove(temp_fp.name)
        output = dedent('''
        +--------+
        | Álvaro |
        +--------+
        |  Píton |
        +--------+
        ''').strip().decode('utf8').encode('iso-8859-1')
        self.assertEqual(file_contents, output)
Exemple #22
0
 def test_importing_from_csv_and_exporting_to_mysql_should_handle_types_correctly(
         self):
     self.connection.query('DROP TABLE ' + self.table)
     self.connection.commit()
     csv_fp = StringIO()
     csv_fp.write(
         dedent('''
     int_col,float_col,str_col,date_col,datetime_col
     1,1.2,abc,2012-04-01,2009-04-01 00:01:02
     2,2.3,cba,2011-03-02,2010-03-02 01:02:03
     3,3.4,bca,2010-02-03,2011-02-03 02:03:04
     4,4.5,cab,2009-01-04,2012-01-04 03:04:05
     '''))
     csv_fp.seek(0)
     table = Table()
     table.read('csv', csv_fp)
     table.write('mysql', self.connection_string)
     self.assertEquals(table.types['int_col'], int)
     self.assertEquals(table.types['float_col'], float)
     self.assertEquals(table.types['str_col'], str)
     self.assertEquals(table.types['date_col'], datetime.date)
     self.assertEquals(table.types['datetime_col'], datetime.datetime)
 def test_read_csv_and_write_csv(self):
     data = dedent('''
     "spam","eggs","ham"
     "42","3.0","2011-01-02"
     "1","3.14","2012-01-11"
     "21","6.28","2010-01-03"
     "2","2.71","2"
     ''').strip() + '\n'
     temp_fp = tempfile.NamedTemporaryFile(delete=False)
     temp_fp.write(data)
     temp_fp.close()
     temp_fp_2 = tempfile.NamedTemporaryFile(delete=False)
     temp_fp_2.close()
     my_table = Table()
     my_table.read('csv', temp_fp.name)
     my_table.write('csv', temp_fp_2.name)
     temp_fp_2 = open(temp_fp_2.name)
     contents = temp_fp_2.read()
     temp_fp_2.close()
     os.remove(temp_fp.name)
     os.remove(temp_fp_2.name)
     self.assertEquals(contents, data)
 def test_write_csv_without_filename_should_return_csv_data(self):
     data = dedent('''
     "spam","eggs","ham"
     "42","3.0","2011-01-02"
     "1","3.14","2012-01-11"
     "21","6.28","2010-01-03"
     "2","2.71","2"
     ''').strip() + '\n'
     temp_fp = tempfile.NamedTemporaryFile(delete=False)
     temp_fp.write(data)
     temp_fp.close()
     my_table = Table()
     my_table.read('csv', temp_fp.name)
     contents = my_table.write('csv')
     os.remove(temp_fp.name)
     self.assertEquals(contents, data)
Exemple #25
0
 def test_to_html_with_only_headers(self):
     my_table = Table(headers=['ham', 'spam', 'eggs', 'blah'])
     output = my_table.write('html', css_classes=False)
     expected = dedent('''
     <table>
       <thead>
         <tr>
           <th>ham</th>
           <th>spam</th>
           <th>eggs</th>
           <th>blah</th>
         </tr>
       </thead>
     </table>
     ''').strip()
     self.assertEquals(output, expected)
 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)
Exemple #27
0
 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)
Exemple #28
0
 def test_to_html_should_create_CSS_classes_for_odd_and_even_rows(self):
     my_table = Table(headers=['ham', 'spam', 'eggs'])
     my_table.append(['python', 'rules', '!'])
     my_table.append({'ham': 'spam', 'spam': 'eggs', 'eggs': 'ham'})
     my_table.append(['python', 'rules', '!'])
     my_table.append({'ham': 'spam', 'spam': 'eggs', 'eggs': 'ham'})
     output = my_table.write('html', css_classes=True)
     expected = dedent('''
     <table>
       <thead>
         <tr class="header">
           <th>ham</th>
           <th>spam</th>
           <th>eggs</th>
         </tr>
       </thead>
       <tbody>
         <tr class="odd">
           <td>python</td>
           <td>rules</td>
           <td>!</td>
         </tr>
         <tr class="even">
           <td>spam</td>
           <td>eggs</td>
           <td>ham</td>
         </tr>
         <tr class="odd">
           <td>python</td>
           <td>rules</td>
           <td>!</td>
         </tr>
         <tr class="even">
           <td>spam</td>
           <td>eggs</td>
           <td>ham</td>
         </tr>
       </tbody>
     </table>
     ''').strip()
     self.assertEquals(output, expected)
    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)
Exemple #30
0
    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)
Exemple #31
0
#!/usr/bin/env python
# coding: utf-8
# title = Exporting to a CSV File
# output = 'my-data.csv'
# Using plugins we can import and export `Table` data to CSV (really, to and
# from a lot of formats). Let's create a simple table and export it to a CSV
# file.

from outputty import Table

my_table = Table(headers=["First name", "Last name"])
my_table.append({"First name": "Álvaro", "Last name": "Justen"})
my_table.append(("Flávio", "Amieiro"))
my_table.append(["Flávio", "Coelho"])
my_table.write("csv", "my-data.csv")
Exemple #32
0
 def test_write_should_not_create_table_if_it_exists(self):
     table = Table(headers=['monty', 'python'])
     table.write('mysql', self.connection_string)
     self.cursor.execute('SELECT * FROM ' + self.table)
     cols = [x[0] for x in self.cursor.description]
     self.assertEquals(set(cols), set(['field1', 'field2']))
# coding: utf-8
# title = Using MySQL plugin
#It's easy to import data from and export data to a MySQL table.
#``outputty`` automatically identify type of data and creates a table in MySQL
#for you with correct data types, so don't worry about converting everyting.
#Let's create a simple table, export it to MySQL and then import it again.
#Note: you need to change ``connection_string`` before run it.

from outputty import Table
from random import randint


# The connection string should be in the format:
#  'username:password@server[:port]/database/table_name'
connection_string = 'root:r00t@localhost/testing/test_table_' + \
                    str(randint(0, 99999))
my_table = Table(headers=['ID', 'First name', 'Last name'])
my_table.append({'First name': 'Álvaro', 'Last name': 'Justen', 'ID': '123'})
my_table.append((456, 'Flávio', 'Amieiro'))
my_table.append(['789', 'Flávio', 'Coelho'])
my_table.write('mysql', connection_string)
print 'Table saved:'
print my_table
print 'The types identified are:', my_table.types

other_table = Table()
other_table.read('mysql', connection_string)
print
print 'Table retrieved:'
print other_table
Exemple #34
0
 def test_to_html_should_without_parameters_should_return_string(self):
     my_table = Table(headers=['ham', 'spam', 'eggs'])
     self.assertTrue(isinstance(my_table.write('html'), str))
Exemple #35
0
#!/usr/bin/env python
# coding: utf-8
# title = Using MySQL plugin
#It's easy to import data from and export data to a MySQL table.
#``outputty`` automatically identify type of data and creates a table in MySQL
#for you with correct data types, so don't worry about converting everyting.
#Let's create a simple table, export it to MySQL and then import it again.
#Note: you need to change ``connection_string`` before run it.

from outputty import Table
from random import randint

# The connection string should be in the format:
#  'username:password@server[:port]/database/table_name'
connection_string = 'root:r00t@localhost/testing/test_table_' + \
                    str(randint(0, 99999))
my_table = Table(headers=['ID', 'First name', 'Last name'])
my_table.append({'First name': 'Álvaro', 'Last name': 'Justen', 'ID': '123'})
my_table.append((456, 'Flávio', 'Amieiro'))
my_table.append(['789', 'Flávio', 'Coelho'])
my_table.write('mysql', connection_string)
print 'Table saved:'
print my_table
print 'The types identified are:', my_table.types

other_table = Table()
other_table.read('mysql', connection_string)
print
print 'Table retrieved:'
print other_table
#!/usr/bin/env python
# coding: utf-8
# title = Exporting to a CSV File
# output = 'my-data.csv', 'my-data.dsv'
#Using plugins we can import and export ``Table`` data to CSV (really, to and
#from a lot of formats). Let's create a simple table and export it to a CSV
#file.
#You can also create any kind of DSV (delimiter-separeted value) files, just
#passing ``delimiter``, ``quote_char`` and ``line_terminator`` to ``write`` (the
#same parameters apply to ``read``).

from outputty import Table

my_table = Table(headers=['First name', 'Last name'])
my_table.append({'First name': 'Álvaro', 'Last name': 'Justen'})
my_table.append(('Flávio', 'Amieiro'))
my_table.append(['Flávio', 'Coelho'])
my_table.write('csv', 'my-data.csv')

#Let's create a other kind of DSV:
my_table.write('csv', 'my-data.dsv', delimiter=';', quote_char="'",
        line_terminator='\r\n')
Exemple #37
0
#!/usr/bin/env python
# coding: utf-8
# title = Exporting to a CSV File
# output = 'my-data.csv', 'my-data.dsv'
#Using plugins we can import and export ``Table`` data to CSV (really, to and
#from a lot of formats). Let's create a simple table and export it to a CSV
#file.
#You can also create any kind of DSV (delimiter-separeted value) files, just
#passing ``delimiter``, ``quote_char`` and ``line_terminator`` to ``write`` (the
#same parameters apply to ``read``).

from outputty import Table

my_table = Table(headers=['First name', 'Last name'])
my_table.append({'First name': 'Álvaro', 'Last name': 'Justen'})
my_table.append(('Flávio', 'Amieiro'))
my_table.append(['Flávio', 'Coelho'])
my_table.write('csv', 'my-data.csv')

#Let's create a other kind of DSV:
my_table.write('csv',
               'my-data.dsv',
               delimiter=';',
               quote_char="'",
               line_terminator='\r\n')
#!/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="#")
Exemple #39
0
#!/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='#')
Exemple #40
0
#!/usr/bin/env python
# coding: utf-8
# title = Reading from CSV and Exporting to HTML
# output = 'nice-software.html'
#You can export your data to HTML using the plugin HTML (that is shipped by
#default with ``outputty``). If you don't specify a filename, the HTML plugin
#will return a string (encoded with ``output_encoding``, specified in
#``Table.__init__``). If it receives the filename, the contents will be saved
#into it and it'll return nothing.

from outputty import Table

my_table = Table()
my_table.read('csv', 'nice-software.csv')
my_table.write('html', 'nice-software.html')
Exemple #41
0
#!/usr/bin/env python
# coding: utf-8
# title = Exporting to a Text File
# input = 'nice-software.csv', code
# output = 'nice-software.txt'
#We can also import data from a CSV file and export it to a text file (using
#plugins, again). The data written to the text file will be the same we saw
#when executed ``print my_table`` in Example 1.

from outputty import Table

my_table = Table()
my_table.read('csv', 'nice-software.csv')
my_table.write('text', 'nice-software.txt')
Exemple #42
0
#!/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='#')