def test_pickle_save(fname):
	# saving recarray as a pickle file
	f = open('%s.pickle' % fname[:-4],'wb')
	cPickle.dump(data,f,2)
	f.close()

def test_pytables_save(fname):
	# saving recarray as pytable
	f = tables.openFile('%s.h5' % fname[:-4],'w')
	f.createTable('/', 'data', data)
	f.close()

def test_pickle_load(fname):
	# loading recarray from pickle file
	f = open('%s.pickle' % fname[:-4],'rb')
	data = cPickle.load(f)
	f.close()

def test_pytables_load(fname):
	# loading recarray from pytable
	f = tables.openFile('%s.h5' % fname[:-4])
	data = f.root.data[:]
	f.close()

def test_save_sqlite(fname, table = 'data'):
	# saving recarray to an sqlite file
	conn = sqlite3.connect('%s.sqlite' % fname[:-4])

	c = conn.cursor()

	# getting the variable names
	varnm = data.dtype.names
	nr_var = len(varnm)

	# transform to types sqlite knows
	types = []
	for i in data[0]:
		if type(i) == N.string_: types.append('text')
		if type(i) == N.float_: types.append('real')
		if type(i) == N.int_: types.append('integer')
	
	create_string = ",".join(["%s %s" % (v,t) for v,t in zip(varnm,types)])

	# create a table if it doesn't exist yet
	try:
		c.execute('drop table %s' % table)
	except sqlite3.OperationalError:
		pass

	c.execute('create table %s (%s)' % (table,create_string))

	# putting the data into the table
	exec_string = 'insert into %s values %s' % (table,'(%s)' % (','.join(('?')*nr_var)))
	c.executemany(exec_string, data)

	# commiting the data to the database
	conn.commit()

	# closing the connection
	conn.close()

def test_load_sqlite(fname, table = 'data', str_length = 20):

	conn = sqlite3.connect('%s.sqlite' % fname[:-4])
	c = conn.cursor()

	# get all data
	c.execute('select * from %s' % table)

	# getting data types
	types = []
	for i in c.fetchone():
		if type(i) == unicode: types.append('S%s' % str_length)
		if type(i) == float: types.append('float')
		if type(i) == int: types.append('int')

	# variable names
	varnm = [i[0] for i in c.description]

	# autodetected dtype
	dtype = zip(varnm,types)
	data = N.fromiter(c, dtype = dtype)

	# closing the connection
	conn.close()

if __name__ == '__main__':
	from timeit import Timer
	import numpy as N
	import os, pylab, tables, cPickle, sqlite3, sys

	# making a directory to store simulate data
	if not os.path.exists('./data'): os.mkdir('./data')

	# creating simulated data and variable labels
	varnm = ['id','a','b','c','d','e','f','g','h','i','j']			# variable labels
	nobs = 5000
	data1 =	N.random.randn(nobs,5)	
	data2 =	N.random.randint(-100, high = 100, size = (nobs,5))		

	# adding a string variable
	id = [('id'+str(i)) for i in range(nobs)]

	data1 = [i for i in data1.T]
	data2 = [i for i in data2.T]

	d = []
	d.append(N.array(id))
	d.extend(data1)
	d.extend(data2)

	descr = [(varnm[i],d[i].dtype) for i in xrange(len(varnm))]
	data = N.rec.fromarrays(d, dtype=descr)

	n = 5 
	fname = './data/data.csv'

	# testing pickle
	t1 = Timer('test_pickle_save(\"%s\")' % fname, 'from __main__ import test_pickle_save')
	print "\n\nTest saving recarray using cPickle\n"
	print "%.6f sec/pass" % (t1.timeit(number=n)/n)

	# testing pytables
	t2 = Timer('test_pytables_save(\"%s\")' % fname, 'from __main__ import test_pytables_save')
	print "\n\nTest saving recarray with pytable\n"
	print "%.6f sec/pass" % (t2.timeit(number=n)/n)

	# testing sqlite
	t3 = Timer('test_save_sqlite(\"%s\")' % fname, 'from __main__ import test_save_sqlite')
	print "\n\nTest saving recarray with sqlite\n"
	print "%.6f sec/pass" % (t3.timeit(number=n)/n)

	# testing pickle
	t4 = Timer('test_pickle_load(\"%s\")' % fname, 'from __main__ import test_pickle_load')
	print "\n\nTest loading recarray using cPickle\n"
	print "%.6f sec/pass" % (t4.timeit(number=n)/n)

	# testing pytables
	t5 = Timer('test_pytables_load(\"%s\")' % fname, 'from __main__ import test_pytables_load')
	print "\n\nTest loading recarray with pytable\n"
	print "%.6f sec/pass" % (t5.timeit(number=n)/n)

	# testing sqlite
	t6 = Timer('test_load_sqlite(\"%s\")' % fname, 'from __main__ import test_load_sqlite')
	print "\n\nTest loading recarray with sqlite\n"
	print "%.6f sec/pass" % (t6.timeit(number=n)/n)
