Esempio n. 1
0
def test_rename():

    table = (("foo", "bar"), ("M", 12), ("F", 34), ("-", 56))

    result = rename(table, "foo", "foofoo")
    assert fieldnames(result) == ["foofoo", "bar"]

    result = rename(table, 0, "foofoo")
    assert fieldnames(result) == ["foofoo", "bar"]

    result = rename(table, {"foo": "foofoo", "bar": "barbar"})
    assert fieldnames(result) == ["foofoo", "barbar"]

    result = rename(table)
    result["foo"] = "spong"
    assert fieldnames(result) == ["spong", "bar"]
Esempio n. 2
0
def test_rename():

    table = (('foo', 'bar'), ('M', 12), ('F', 34), ('-', 56))

    result = rename(table, 'foo', 'foofoo')
    assert fieldnames(result) == ('foofoo', 'bar')

    result = rename(table, 0, 'foofoo')
    assert fieldnames(result) == ('foofoo', 'bar')

    result = rename(table, {'foo': 'foofoo', 'bar': 'barbar'})
    assert fieldnames(result) == ('foofoo', 'barbar')

    result = rename(table)
    result['foo'] = 'spong'
    assert fieldnames(result) == ('spong', 'bar')
Esempio n. 3
0
def test_rename():

    table = (('foo', 'bar'),
             ('M', 12),
             ('F', 34),
             ('-', 56))

    result = rename(table, 'foo', 'foofoo')
    assert fieldnames(result) == ('foofoo', 'bar')

    result = rename(table, 0, 'foofoo')
    assert fieldnames(result) == ('foofoo', 'bar')

    result = rename(table, {'foo': 'foofoo', 'bar': 'barbar'})
    assert fieldnames(result) == ('foofoo', 'barbar')

    result = rename(table)
    result['foo'] = 'spong'
    assert fieldnames(result) == ('spong', 'bar')
Esempio n. 4
0
def test_fieldnames():
    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = fieldnames(table)
    expect = ['foo', 'bar']
    eq_(expect, actual)
    
    class CustomField(object):
        def __init__(self, key, description):
            self.key = key
            self.description = description
        def __str__(self):
            return self.key
        def __repr__(self):
            return 'CustomField(%r, %r)' % (self.key, self.description)
        
    table = ((CustomField('foo', 'Get some foo.'), CustomField('bar', 'A lot of bar.')), 
             ('a', 1), 
             ('b', 2))
    actual = fieldnames(table)
    expect = ['foo', 'bar']
    eq_(expect, actual)
Esempio n. 5
0
def test_rename_strict():

    table = (('foo', 'bar'),
             ('M', 12),
             ('F', 34),
             ('-', 56))

    result = rename(table, 'baz', 'quux')
    try:
        fieldnames(result)
    except FieldSelectionError:
        pass
    else:
        assert False, 'exception expected'

    result = rename(table, 2, 'quux')
    try:
        fieldnames(result)
    except FieldSelectionError:
        pass
    else:
        assert False, 'exception expected'

    result = rename(table, 'baz', 'quux', strict=False)
    assert fieldnames(result) == ('foo', 'bar')
    result = rename(table, 2, 'quux', strict=False)
    assert fieldnames(result) == ('foo', 'bar')
Esempio n. 6
0
def test_rename_strict():

    table = (('foo', 'bar'), ('M', 12), ('F', 34), ('-', 56))

    result = rename(table, 'baz', 'quux')
    try:
        fieldnames(result)
    except FieldSelectionError:
        pass
    else:
        assert False, 'exception expected'

    result = rename(table, 2, 'quux')
    try:
        fieldnames(result)
    except FieldSelectionError:
        pass
    else:
        assert False, 'exception expected'

    result = rename(table, 'baz', 'quux', strict=False)
    assert fieldnames(result) == ('foo', 'bar')
    result = rename(table, 2, 'quux', strict=False)
    assert fieldnames(result) == ('foo', 'bar')
Esempio n. 7
0
File: io.py Progetto: deytao/petl
def appenddb(table, connection_or_cursor, tablename, commit=True):
    """
    Load data into an existing database table via a DB-API 2.0
    connection or cursor. Note that the database table will be appended, 
    i.e., the new data will be inserted into the table, and any existing
    rows will remain.
    
    E.g.::
    
        >>> from petl import look, appenddb
        >>> look(table)
        +-------+-------+
        | 'foo' | 'bar' |
        +=======+=======+
        | 'a'   | 1     |
        +-------+-------+
        | 'b'   | 2     |
        +-------+-------+
        | 'c'   | 2     |
        +-------+-------+

    ... using :mod:`sqlite3`::
    
        >>> import sqlite3
        >>> connection = sqlite3.connect('test.db')
        >>> # assuming table "foobar" already exists in the database
        ... appenddb(table, connection, 'foobar')    
        
    ... using :mod:`psycopg2`::

        >>> import psycopg2 
        >>> connection = psycopg2.connect("dbname=test user=postgres")
        >>> # assuming table "foobar" already exists in the database
        ... appenddb(table, connection, 'foobar')    
        
    ... using :mod:`MySQLdb`::

        >>> import MySQLdb
        >>> connection = MySQLdb.connect(passwd="moonpie", db="thangs")
        >>> # assuming table "foobar" already exists in the database
        ... appenddb(table, connection, 'foobar')    
        
    .. versionchanged:: 0.10.2
    
    A cursor can also be provided instead of a connection, e.g.::

        >>> import psycopg2 
        >>> connection = psycopg2.connect("dbname=test user=postgres")
        >>> cursor = connection.cursor()
        >>> appenddb(table, cursor, 'foobar')    
    
    """

    # deal with polymorphic arg
    if hasattr(connection_or_cursor, 'cursor'):
        connection = connection_or_cursor
        cursor = connection.cursor()
    elif hasattr(connection_or_cursor, 'execute'):
        cursor = connection_or_cursor
        if hasattr(cursor, 'connection'):
            connection = cursor.connection
        else:
            connection = None
    else:
        raise Exception('expected connection or cursor, found: %r' % connection_or_cursor)
            
    # sanitise table and field names
    tablename = _quote(tablename)
    names = [_quote(n) for n in fieldnames(table)]
    placeholders = _placeholders(connection, names)
    
    # insert some data
    _insert(cursor, tablename, placeholders, table)

    # finish up
    if hasattr(connection_or_cursor, 'cursor'):
        # close the cursor we created
        cursor.close()
    if commit and connection is not None:
        connection.commit()
Esempio n. 8
0
File: io.py Progetto: deytao/petl
def tosqlite3(table, filename_or_connection, tablename, create=True, commit=True):
    """
    Load data into a table in an :mod:`sqlite3` database. Note that if
    the database table exists, it will be truncated, i.e., all
    existing rows will be deleted prior to inserting the new
    data. E.g.::

        >>> from petl import tosqlite3, look
        >>> look(table)
        +-------+-------+
        | 'foo' | 'bar' |
        +=======+=======+
        | 'a'   | 1     |
        +-------+-------+
        | 'b'   | 2     |
        +-------+-------+
        | 'c'   | 2     |
        +-------+-------+
        
        >>> # by default, if the table does not already exist, it will be created
        ... tosqlite3(table, 'test.db', 'foobar')
        >>> # look what it did
        ... from petl import fromsqlite3
        >>> look(fromsqlite3('test.db', 'select * from foobar'))
        +-------+-------+
        | 'foo' | 'bar' |
        +=======+=======+
        | u'a'  | 1     |
        +-------+-------+
        | u'b'  | 2     |
        +-------+-------+
        | u'c'  | 2     |
        +-------+-------+

    .. versionchanged:: 0.10.2
    
    Either a database file name or a connection object can be given as the
    second argument. (Note that `cachetag()` is only implemented if a file name
    is given.)

    """
    
    tablename = _quote(tablename)
    names = [_quote(n) for n in fieldnames(table)]

    if isinstance(filename_or_connection, basestring):
        conn = sqlite3.connect(filename_or_connection)
    elif isinstance(filename_or_connection, sqlite3.Connection):
        conn = filename_or_connection
    else:
        raise Exception('filename_or_connection argument must be filename or connection; found %r' % filename_or_connection)
    
    cursor = conn.cursor()
    
    if create: # force table creation
        cursor.execute('DROP TABLE IF EXISTS %s' % tablename)
        cursor.execute('CREATE TABLE %s (%s)' % (tablename, ', '.join(names)))
    
    # truncate table
    cursor.execute('DELETE FROM %s' % tablename)
    
    placeholders = ', '.join(['?'] * len(names))
    _insert(cursor, tablename, placeholders, table)

    # tidy up
    cursor.close()
    if commit:
        conn.commit()

    return conn # in case people want to close it