-
Notifications
You must be signed in to change notification settings - Fork 0
/
bayesdb.py
90 lines (81 loc) · 2.77 KB
/
bayesdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- Mode: Python; coding: utf-8; python-indent-offset: 2 -*-
import bayeslite
import venture.lite.psp as psp
import venture.lite.sp_help as sp
import venture.lite.types as t
import venture.lite.value as vv
class BayesDB_PSP(psp.DeterministicPSP):
def simulate(self, args):
pathname = args.operandValues()[0]
bdb = bayeslite.bayesdb_open(pathname)
return vv.VentureForeignBlob(bdb)
class MML_PSP(psp.DeterministicPSP):
def simulate(self, args):
bdb = args.operandValues()[0].datum
phrase = args.operandValues()[1]
if len(args.operandValues()) == 2:
parameters = args.operandValues()[2].asPythonList()
else:
parameters = None
bdb.execute(phrase, parameters)
class BQL_PSP(psp.DeterministicPSP):
def simulate(self, args):
bdb = args.operandValues()[0].datum
phrase = args.operandValues()[1]
if len(args.operandValues()) == 3:
parameters = tuple(venture2bql(x) for x in args.operandValues()[2])
else:
parameters = None
results = bdb.execute(phrase, parameters).fetchall()
return tuple(bql2venture(x) for row in results for x in row)
class SQL_PSP(psp.DeterministicPSP):
def simulate(self, args):
bdb = args.operandValues()[0].datum
phrase = args.operandValues()[1]
if len(args.operandValues()) == 3:
parameters = tuple(venture2bql(x) for x in args.operandValues()[2])
else:
parameters = None
results = bdb.sql_execute(phrase, parameters).fetchall()
return tuple(bql2venture(x) for row in results for x in row)
def bql2venture(x):
if isinstance(x, float):
return vv.VentureNumber(x)
if isinstance(x, int):
return vv.VentureInteger(x)
if isinstance(x, unicode):
return vv.VentureString(x.encode('UTF-8'))
if x is None:
return vv.VentureNil()
raise ValueError('Unrepresentable value from BQL: %r' % (x,))
def venture2bql(x):
if isinstance(x, vv.VentureNumber):
return x.getNumber()
if isinstance(x, vv.VentureInteger):
return x.getInteger()
if isinstance(x, vv.VentureString):
return unicode(x.getString(), 'UTF-8')
if isinstance(x, vv.VentureBool):
return x.getBool()
if isinstance(x, vv.VentureNil):
return None
raise ValueError('Unrepresentable value for BQL: %r' % (x,))
BAYESDB_SPS = [
('bayesdb_open',
sp.typed_nr(BayesDB_PSP(), [t.StringType()], t.AnyType('population'))),
('bayesdb_sql',
sp.typed_nr(SQL_PSP(),
[t.AnyType('population'), t.StringType(), t.ArrayType()],
t.ArrayType(),
min_req_args=2)),
('bayesdb_bql',
sp.typed_nr(BQL_PSP(),
[t.AnyType('population'), t.StringType(), t.ArrayType()],
t.ArrayType(),
min_req_args=2)),
('bayesdb_mml',
sp.typed_nr(MML_PSP(),
[t.AnyType('population'), t.StringType(), t.ArrayType()],
t.ArrayType(),
min_req_args=2)),
]