Example #1
0
def test_head():

    table1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 5), ('d', 7),
              ('f', 42), ('f', 3), ('h', 90), ('k', 12), ('l', 77), ('q', 2))

    table2 = head(table1, 4)
    expect = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 5), ('d', 7))
    ieq(expect, table2)
Example #2
0
def create_table(table,
                 dbo,
                 tablename,
                 schema=None,
                 commit=True,
                 constraints=True,
                 metadata=None,
                 dialect=None,
                 sample=1000):
    """
    Create a database table based on a sample of data in the given `table`.

    Keyword arguments:

    table : table container
        Table data to load
    dbo : database object
        DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
        SQLAlchemy connection, engine or session
    tablename : text
        Name of the table
    schema : text
        Name of the database schema to create the table in
    commit : bool
        If True commit the changes
    constraints : bool
        If True use length and nullable constraints
    metadata : sqlalchemy.MetaData
        Custom table metadata
    dialect : text
        One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
        'oracle', 'maxdb', 'postgresql', 'mssql'}
    sample : int
        Number of rows to sample when inferring types etc., set to 0 to use
        the whole table

    """

    if sample > 0:
        table = head(table, sample)
    sql = make_create_table_statement(table,
                                      tablename,
                                      schema=schema,
                                      constraints=constraints,
                                      metadata=metadata,
                                      dialect=dialect)
    _execute(sql, dbo, commit=commit)
Example #3
0
def test_head():

    table1 = (('foo', 'bar'),
              ('a', 1),
              ('b', 2),
              ('c', 5),
              ('d', 7),
              ('f', 42),
              ('f', 3),
              ('h', 90),
              ('k', 12),
              ('l', 77),
              ('q', 2))

    table2 = head(table1, 4)
    expect = (('foo', 'bar'),
              ('a', 1),
              ('b', 2),
              ('c', 5),
              ('d', 7))
    ieq(expect, table2)
Example #4
0
def people_list(request, uuid):
    try:
        csvdownload = CSVDownload.objects.get(uuid=uuid)
    except CSVDownload.DoesNotExist:
        return HttpResponseNotFound("Not found.")

    fname = '{0}.csv'.format(csvdownload.uuid)
    full_fname = os.path.join(settings.CSV_DIR, fname)
    people = fromcsv(full_fname)

    sortby = request.GET.get('sortby', 'name')
    ordering = request.GET.get('ordering', 'asc')
    count_str = request.GET.get('count', '10')

    if sortby not in header(people):
        return HttpResponseBadRequest('Bad request.')
    if ordering not in ('asc', 'desc'):
        return HttpResponseBadRequest('Bad request.')
    try:
        count = int(count_str)
    except ValueError:
        return HttpResponseBadRequest('Bad request.')
    if count < 1:
        return HttpResponseBadRequest('Bad request.')

    people = sort(people, sortby, reverse=ordering == 'desc')
    people = head(people, count)

    return render(
        request, 'people_list.html', {
            'csvdownload': csvdownload,
            'headers': header(people),
            'people': data(people),
            'has_more': len(people) > count,
            'queryparams': {
                'sortby': sortby,
                'ordering': ordering,
                'count': str(count + 10)
            }
        })
Example #5
0
def create_table(
    table, dbo, tablename, schema=None, commit=True, constraints=True, metadata=None, dialect=None, sample=1000
):
    """
    Create a database table based on a sample of data in the given `table`.

    Keyword arguments:

    table : table container
        Table data to load
    dbo : database object
        DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
        SQLAlchemy connection, engine or session
    tablename : text
        Name of the table
    schema : text
        Name of the database schema to create the table in
    commit : bool
        If True commit the changes
    constraints : bool
        If True use length and nullable constraints
    metadata : sqlalchemy.MetaData
        Custom table metadata
    dialect : text
        One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
        'oracle', 'maxdb', 'postgresql', 'mssql'}
    sample : int
        Number of rows to sample when inferring types etc., set to 0 to use
        the whole table

    """

    if sample > 0:
        table = head(table, sample)
    sql = make_create_table_statement(
        table, tablename, schema=schema, constraints=constraints, metadata=metadata, dialect=dialect
    )
    _execute(sql, dbo, commit=commit)
Example #6
0
def test_head_raises_stop_iteration_for_header_only():
    table1 = (('foo', 'bar', 'baz'), )
    table = iter(head(table1))
    next(table)  # header
    with pytest.raises(StopIteration):
        next(table)
Example #7
0
def test_head_raises_stop_iteration_for_empty_table():
    table = iter(head([]))
    with pytest.raises(StopIteration):
        next(table)  # header