def test_odb_module_appends_semicolon_if_needed(self): """ Check that semicolon at the end of SQL statement is not obligatory (ODB-114) Expect IOError, and not SyntaxError. """ with self.assertRaises(IOError) as ex: for row in odb.sql('select * from "non_existing_file.odb"'): pass
def indexing_result_set_row(file_name = test_file): """ Various ways of indexing row object.""" for row in odb.sql('select * from "%s"' % file_name): # Row can be indexed with a tuple containing column names expver, analysis_date, analysis_time = row['expver', 'andate', 'antime'] print 'expver: "%s", analysis date: %d analysis time: %d' % (expver, analysis_date, analysis_time) break for row in odb.sql('select lat,lon,varno,obsvalue from "%s"' % file_name): # Row can be indexed with integers representing position of the column in the result set, # starting from 0 print row[0], row[1], row[2], row[3] # We can also index with a tuple containing several integers; # the result will be a tuple of values. print row[0,1,2,3] break
def sql_select(file_name = test_file): """ SQL filtering / calculations. """ s = '''select varno,count(*),min(obsvalue),max(obsvalue) from "%s" order by 2 desc''' % file_name for row in odb.sql(s): print ', '.join(map(str, row[:]))
def printStats(dataFile, fs = statsFunctions): columns = getColumns(dataFile) maxColumn = max([len(c[0]) for c in columns]) columnFormat = '%-' + str(maxColumn) + 's' valueFormat = '%20s' sql = genStatSQL([c[0] for c in columns], fs) + ' from ' + quote(dataFile) for r in odb.sql(sql): print columnFormat % 'column', "".join([valueFormat % v for v in fs]) values = [str(v) for v in r[:]] for (c, vs) in zip(columns, chunks(values, len(fs))): #if c[1] in [3,4]: vs = ['NA' for v in vs] print columnFormat % c[0], "".join([valueFormat % v for v in vs])
############################################################################### # IMPORTANT : before running you have to set-up your LD_LIBRARY_PATH # to make sure it content /usr/local/apps/odb_api/0.9.26/lib # setenv LD_LIBRARY_PATH /usr/local/apps/odb_api/0.9.26/lib:LD_LIBRARY_PATH ############################################################################### from Magics.macro import * import numpy as np import sys, os sys.path.insert(0, '/usr/local/apps/odb_api/0.9.26/lib/python2.7.1/site-packages') import odb odb = np.array([r[:] for r in odb.sql( "select lat,lon,obsvalue from '%s'" % 'data.odb')]) lat = odb[:,0] lon = odb[:,1] obsvalue = odb[:,2] - 273.15 ref = 'odb2' # Setting of the output file name output = output(output_formats=['png'], output_name_first_page_number='off', output_name=ref)
def rows(s): return [r[:] for r in odb.sql(s)]
def testSelectFromFile(self): sql = 'select lat,lon,obsvalue from "' + self.fn + '"' self.assertTrue(15719 == len([r for r in odb.sql(sql)]))
# granted to it by virtue of its status as an intergovernmental organisation nor # does it submit to any jurisdiction. # importing Magics module from Magics.macro import * import numpy as np import sys, os sys.path.insert( 0, '/usr/local/lib/metaps/lib/odalib/0.9.8/python2.7/site-packages/') import odb data = 'data.odb' odb = np.array( [r[:] for r in odb.sql("select lat,lon,obsvalue from '%s'" % data)]) print odb lat = odb[:, 0] lon = odb[:, 1] obsvalue = odb[:, 2] - 273.15 ref = 'odb' # Setting of the output file name output = output(output_formats=['png'], output_name_first_page_number='off', output_name=ref) # Setting the coordinates of the geographical area
def reading_metadata_of_result_set(file_name = test_file): for row in odb.sql('select * from "%s"' % file_name): columns = [c.name() for c in row.columns()] break assert columns[:5] == ['expver@desc', 'andate@desc', 'antime@desc', 'seqno@hdr', 'obstype@hdr']
def test_syntax_error_throws_exception(self): with self.assertRaises(SyntaxError) as ex: for row in odb.sql('select * from '): pass
def test_open_non_existing_file(self): with self.assertRaises(IOError) as ex: for row in odb.sql('select * from "non_existing_file.odb";'): pass
def select_non_existing_file(): return open_non_existing_file(f = lambda fn: odb.sql('select * from "%s"' % fn))