Skip to content

Instantly share code, notes, and snippets.

@e000
Created February 11, 2011 00:01
Show Gist options
  • Select an option

  • Save e000/821654 to your computer and use it in GitHub Desktop.

Select an option

Save e000/821654 to your computer and use it in GitHub Desktop.
def parseWolframAlphaResponse(self, response, redirected = False, results = None):
results = results if results is not None else []
xmlTree = fromstring(response)
recalculate = xmlTree.get('recalculate')
success = xmlTree.get('success')
if success == 'true':
for pod in xmlTree.findall('pod'):
title = pod.get('title')
plaintext = pod.find('subpod/plaintext')
if plaintext is not None and plaintext.text:
results.append((title, plaintext.text.split('\n')))
if recalculate:
if not redirected:
return self.getPage(recalculate).addCallback(self.parseWolframAlphaResponse, True, results)
elif results:
return True, results
else:
return error, 'Too many redirects.'
elif success == 'true':
return True, results
else:
return False, [tip.get('text') for tip in xmlTree.findall('tips/tip')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment