from pyamf.remoting.gateway.wsgi import WSGIGateway
import adodbapi
import dtuple
import ols
import numpy
import csv
db = adodbapi.connect ("Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=TxDB;Integrated Security=SSPI;")
def stats(data,d,i):
"""
Returns summary statistics
y: y-vector
x: x-array
d: Name of dependent variable
i: List of independent variables
"""
dd = numpy.array(data)
y = dd[:,0]
x = dd[:,1:]
try:
m = ols.ols(y, x, y_varnm = d, x_varnm = i)
return m.report()
except:
return "Error in calculation"
def csv(fname,data):
"""
write data to csv file
"""
w = csv.writer(open(fname, "wb"))
w.writerows(data)
return 1
def doSQL(sql,mode):
"""
Run SQL on db
mode 0 = put
mode 1 = get
"""
global db
c = db.cursor()
c.execute(sql)
if mode:
raw_rows = c.fetchall()
desc = dtuple.TupleDescriptor(c.description)
res=[]
for row in raw_rows:
res.append(dtuple.DatabaseTuple(desc, row).asMapping())
return res
else:
db.commit()
def dbClose():
"""
Close db connection
"""
global db
db.close()
def testLogin():
return True;
def auth(uid, pwd):
"""
Authenticate the user.
"""
if uid == 'your encoded uid' and pwd == 'your encoded pwd' :
return True
return False
services = {
'stats' : stats,
'csv' : csv,
'doSQL' : doSQL,
'dbClose' : dbClose,
'testLogin' : testLogin
}
application = WSGIGateway(services,authenticator=auth)
Thursday, July 9, 2009
Call MSSQL database using pyAMF
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment