コード例 #1
0
ファイル: tools.py プロジェクト: esheldon/espy
def test_simple(verbose=False):
    """
    This is designed to test all the basic datatypes in scalar and array
    forms
    """
    dt = [('i4','i4'),('f4','f4')]

    narr = 2
    arr=numpy.zeros(narr, dtype=dt)

    # initialize fields
    # f4 fields
    arr['i4'] = [3,25]
    arr['f4'] = [1.2, 66.3]

    table = 'test_npy_input_output'

    if table_exists(table):
        sys.stdout.write('removing existing test table %s\n' % table)
        pgqueryln('drop table %s' % table)
    sys.stdout.write("Testing database input to table '%s'\n" % table)
    array2table(arr, table, verbose=verbose, keep=True)

    sys.stdout.write("\nTesting database extraction from table '%s'\n" % table)
    res = pgquery('select * from %s' % table)

    sys.stdout.write('Testing that input and output match\n')
    compare_arrays(arr, res, verbose=True)
    
    sys.stdout.write("removing temporary table '%s'\n" % table)
    pgqueryln('truncate %s' % table)
    pgqueryln('vacuum full %s' % table)
    pgqueryln('drop table %s' % table)
コード例 #2
0
ファイル: tools.py プロジェクト: esheldon/espy
def tables(conninfo=None):
    query="""
    select 
        tablename 
    from 
        pg_tables 
    where 
        schemaname = 'public'
    """
    tb=pgquery(query, flengths={'tablename':100}, 
               conninfo=conninfo)
    return tb['tablename']
コード例 #3
0
ファイル: tools.py プロジェクト: esheldon/espy
def test(verbose=False, keep=False, pad_strings=True):
    """
    This is designed to test all the basic datatypes in scalar and array
    forms
    """
    
    # use postgres user to avoid any permissions issues
    conninfo='user=postgres'

    f4array_shape = (3,2,2)
    f8array_shape = (2,)
    strarray_shape = (2,2)
    i2array_shape = (3,)
    i4array_shape = (2,3)
    i8array_shape = (1,2)
    dt = [('f4','f4'), ('f4array','f4',f4array_shape),
          ('f8','f8'), ('f8array','f8',f8array_shape),
          ('str','S5'),('strarray','S5',strarray_shape),
          ('i2','i2'), ('i2array','i2',i2array_shape),
          ('i4','i4'), ('i4array','i4',i4array_shape),
          ('i8','i8'), ('i8array','i8',i8array_shape)]

    narr = 5
    arr=numpy.zeros(narr, dtype=dt)

    # initialize fields
    # f4 fields
    arr['f4'] = numpy.arange(len(arr))
    for i in range(narr):
        arr[i]['f4array'][:] = numpy.random.random(f4array_shape)[:]

    # f8 fields
    arr['f8'] = numpy.arange(narr)
    for i in range(narr):
        arr[i]['f8array'][:] = numpy.random.random(f8array_shape)[:]

   
    # string fields
    # no nul characters are allowed.  array2table will pad the strings
    # with spaces automatically, so we add them here by hand in order
    # for the tests to succeed
    if pad_strings:
        arr['str'][0] = 'hello'
        arr['str'][1] = 'world'
        arr['str'][2] = 'test '
        arr['str'][3] = 'str  '
        arr['str'][4] = 'field'
    else:
        arr['str'][0] = 'hello'
        arr['str'][1] = 'world'
        arr['str'][2] = 'test'
        arr['str'][3] = 'str'
        arr['str'][4] = 'field'


    for i in range(narr):
        arr[i]['strarray'][:] = numpy.random.random(strarray_shape)[:]
    

    # i2 fields
    arr['i2'] = numpy.arange(narr)
    for i in range(narr):
        arr[i]['i2array'][:] = \
                numpy.random.randint(-32000,32000,arr[i]['i2array'].size)

    # i4 fields
    arr['i4'] = numpy.arange(narr)
    for i in range(narr):
        arr[i]['i4array'][:] = \
                numpy.random.random(i4array_shape)*100000

    # i8 fields
    arr['i8'] = numpy.arange(narr)
    for i in range(narr):
        arr[i]['i8array'][:] = \
                numpy.random.random(i8array_shape)*100000



    table = 'test_npy_input_output'

    if table_exists(table):
        sys.stdout.write('removing existing test table %s\n' % table)
        pgqueryln('truncate %s' % table, conninfo=conninfo)
        pgqueryln('vacuum full %s' % table, conninfo=conninfo)
        pgqueryln('drop table %s' % table, conninfo=conninfo)
    sys.stdout.write("Testing database input to table '%s'\n" % table)
    array2table(arr, table, verbose=verbose, keep=keep, conninfo=conninfo,
                pad_strings=pad_strings)

    sys.stdout.write("Testing database extraction from table '%s'\n" % table)
    res = pgquery('select * from %s' % table, conninfo=conninfo)

    sys.stdout.write('Testing that input and output match\n')
    compare_arrays(arr, res, verbose=True)
    
    if not keep:
        sys.stdout.write("\nremoving temporary table '%s'\n" % table)
        pgqueryln('truncate %s' % table, conninfo=conninfo)
        pgqueryln('vacuum full %s' % table, conninfo=conninfo)
        pgqueryln('drop table %s' % table, conninfo=conninfo)