-
Notifications
You must be signed in to change notification settings - Fork 1
/
wap.py
268 lines (208 loc) · 6.49 KB
/
wap.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/python
#
# Copyright 2009 Derik Pereira. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''A library that provides a python interface to the Wolfram|Alpha API'''
__author__ = '[email protected]'
__version__ = '1.1-devel'
import urllib2
from xml.dom import minidom
import simplejson as json
class WolframAlphaEngine:
def __init__(self, appid='', server=''):
self.appid = appid
self.server = server
self.ScanTimeout = ''
self.PodTimeout = ''
self.FormatTimeout = ''
self.Async = ''
def CreateQuery(self, query=''):
waeq = WolframAlphaQuery(query)
waeq.appid = self.appid
waeq.ScanTimeout = self.ScanTimeout
waeq.PodTimeout = self.PodTimeout
waeq.FormatTimeout = self.FormatTimeout
waeq.Async = self.Async
waeq.ToURL()
return waeq.Query
def PerformQuery(self, query=''):
try:
result = urllib2.urlopen(self.server, query)
result = result.read()
except:
result = '<error>urllib2.urlopen ' + self.server + ' ' + query + '</error>'
return result
class WolframAlphaQuery:
def __init__(self, query='', appid=''):
self.Query = query
self.appid = appid
self.ScanTimeout = ''
self.PodTimeout = ''
self.FormatTimeout = ''
self.Async = ''
def ToURL(self):
self.Query = 'input=' + self.Query
self.Query = self.Query + '&appid=' + self.appid
if self.ScanTimeout:
self.Query = self.Query + '&scantimeout=' + self.ScanTimeout
if self.PodTimeout:
self.Query = self.Query + '&podtimeout=' + self.PodTimeout
if self.FormatTimeout:
self.Query = self.Query + '&formattimeout=' + self.FormatTimeout
if self.Async:
self.Query = self.Query + '&async=' + self.Async
return
def AddPodTitle(self, podtitle=''):
self.Query = self.Query + '&podtitle=' + podtitle
return
def AddPodIndex(self, podindex=''):
self.Query = self.Query + '&podindex=' + podindex
return
def AddPodScanner(self, podscanner=''):
self.Query = self.Query + '&podscanner=' + podscanner
return
def AddPodState(self, podstate=''):
self.Query = self.Query + '&podstate=' + podstate
return
def AddAssumption(self, assumption=''):
self.Query = self.Query + '&assumption=' + assumption
return
class WolframAlphaQueryResult:
def __init__(self, result=''):
self.XmlResult = result
self.dom = minidom.parseString(result)
self.tree = runtree(self.dom.documentElement)
def JsonResult(self):
return json.dumps(self.tree)
def IsSuccess(self):
return scanbranches(self.tree, 'success')
def IsError(self):
try:
return [scanbranches(self.tree, 'error')[0]]
except:
return scanbranches(self.tree, 'error')
def NumPods(self):
return scanbranches(self.tree, 'numpods')
def DataTypes(self):
return scanbranches(self.tree, 'datatypes')
def TimedoutScanners(self):
return scanbranches(self.tree, 'timedout')
def Timing(self):
return scanbranches(self.tree, 'timing')
def ParseTiming(self):
return scanbranches(self.tree, 'parsetiming')
def Error(self):
try:
return scanbranches(self.tree, 'error')[1]
except:
return []
def ErrorCode(self):
try:
return [scanbranches(self.Error(), 'code')[0]]
except:
return []
def ErrorMessage(self):
try:
return [scanbranches(self.Error(), 'msg')[0]]
except:
return []
def Pods(self):
return scanbranches(self.tree, 'pod')
def XMLPods(self):
return asxml(self.dom, 'pod')
def Assumptions(self):
assumptions = scanbranches(self.tree, 'assumptions')
try:
return scanbranches(assumptions[0], 'assumption')
except:
return []
def Warnings(self):
return scanbranches(self.tree, 'warnings')
def Sources(self):
return scanbranches(self.tree, 'sources')
class Pod:
def __init__(self, pod=''):
self.pod = pod
return
def IsError(self):
return scanbranches(self.pod, 'error')
def NumSubpods(self):
return scanbranches(self.pod, 'numsubpods')
def Title(self):
return scanbranches(self.pod, 'title')
def Scanner(self):
return scanbranches(self.pod, 'scanner')
def Position(self):
return scanbranches(self.pod, 'position')
def AsynchURL(self):
return scanbranches(self.pod, 'asynchurl')
def Subpods(self):
return scanbranches(self.pod, 'subpod')
def PodStates(self):
return scanbranches(self.pod, 'states')
def Infos(self):
return scanbranches(self.pod, 'infos')
def AsXML(self):
return self.pod
class Subpod:
def __init__(self, subpod=''):
self.subpod = subpod
return
def Title(self):
return scanbranches(self.subpod, 'title')
def Plaintext(self):
return scanbranches(self.subpod, 'plaintext')
def Img(self):
return scanbranches(self.subpod, 'img')
class Assumption:
def __init__(self, assumption=''):
self.assumption = assumption
return
def Type(self):
return scanbranches(self.assumption, 'type')
def Word(self):
return scanbranches(self.assumption, 'word')
def Count(self):
return scanbranches(self.assumption, 'count')
def Value(self):
return scanbranches(self.assumption, 'value')
def runtree(node):
tree = []
if node.nodeType != node.TEXT_NODE:
tree = [node.nodeName]
for index in range(node.attributes.length):
attr = node.attributes.item(index)
tree = tree + [(attr.nodeName, attr.nodeValue)]
for child in node.childNodes:
if child.nodeType != child.TEXT_NODE:
tree = tree + [runtree(child)]
else:
if child.data[0] != '\n':
tree = child.parentNode.nodeName, child.data
return tree
def scanbranches(tree, name):
branches = []
for branch in tree:
if branch[0] == name:
if type(branch) == type(('', '')):
branches = branches + [branch[1]]
else:
branches = branches + [branch[1:]]
return branches
def asxml(dom, name):
xml = []
for child in dom.documentElement.childNodes:
if child.nodeName == name:
xml = xml + [child.toxml()]
return xml