0

I'm new to the Wolfram|Alpha API for python and I could not find much help on the internet, so I turned to stack overflow. I receive the "NameError: name 'pod' is not defined" on SOME queries on Wolfram|Alpha. Any help would be much appreciated. When I input my query "Length of a Ferrari 458" I used to end up with the StopIteration error, now I've change the code to use "pods" method. Now I get a NameError. The out put should give me the length of the car (https://www.wolframalpha.com/input/?i=length+of+ferrari+458) I had to x out app_id as it's not mine, sorry for the inconvenience.

#!/usr/bin/python
import wolframalpha
app_id=('xxxxxx-xxxxxxxxxx')
client = wolframalpha.Client(app_id)

query = input("Query:")
if len(res.pods) > 0:
texts = ""
pod = res.pods[1]

if pod.text:
  texts = pod.text

else:
  texts = "I have no answer for that"

texts = texts.encode('ascii', 'ignore')
print (texts)

The error I get:

 Query: length of ferrari 458
   Traceback (most recent call last):
     File "Wolfram.py", line 24, in <module>
       if pod.text:
  NameError: name 'pod' is not defined
Jonas
  • 121,568
  • 97
  • 310
  • 388
Anthony.H
  • 21
  • 5

3 Answers3

0

If a sequence is empty next raises an exception.

Pass None as a second parameter to return as a default. From the linked documentation:

next(iterator[, default])
Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

If there are no results and you don't want to handle the exception:

first = next(res.results, None)
if first:
    print(first.text)

If there are no results, first will be None, which you can then check before trying to use it.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • Yup, `next()` can return a default, but that won't help solve the issue that there is no results however. And then you'll get an attribute error on `.text`. – Martijn Pieters Apr 02 '16 at 14:41
  • The question is posed as a `StopIteration` problem rather than an empty `results` attribute problem. – Peter Wood Apr 02 '16 at 14:46
  • I would like to be able to query without any error occurring obviously. For example my input: "Top speed for Ferrari 458" and I want it to return with out StopIteration. As I'm new I don't quite understand by pass none as a second parameter to return as a default. – Anthony.H Apr 02 '16 at 14:51
  • @PeterWood: no, it is a *this doesn't work, I get a `StopIteration` exception. There is no *I expected these results* part to determine one way or another what the OP actually wants to happen. – Martijn Pieters Apr 02 '16 at 14:57
0

If you plan to use the generator twice, without making the query twice, you could use itertools.tee to have two copies of the generator for you to use:

from itertools import tee

res1, res2 = tee(res, 2)

# consume the first generator:
for pod in res1:
    ...
    ...

# you have a second generator you can use:
print(next(res2.results).text)
...
...
chapelo
  • 2,519
  • 13
  • 19
0

The source code shows that res.pods and res.results share the same iterator. The error you are getting simply means there are no results. Try a different query.

The sample query works, for example:

>>> res = client.query('temperature in Washington, DC on October 3, 2012')
>>> print(next(res.results).text)
(21 to 27) °C (average: 24 °C)
(Wednesday, October 3, 2012)
>>> [p.title for p in res]
['Input interpretation', 'Result', 'History', 'Weather station information']

Your specific query doesn't return any results, apparently because there are assumptions to confirm; visiting http://api.wolframalpha.com/v2/query?input=length+of+a+Ferrari+458&appid=<your-app-id> produces:

<?xml version='1.0' encoding='UTF-8'?>
<queryresult success='false'
    error='false'
    numpods='0'
    datatypes=''
    timedout=''
    timedoutpods=''
    timing='2.742'
    parsetiming='0.79'
    parsetimedout='false'
    recalculate=''
    id='MSPa12051ddfeh1dgh883d2e000035eh08fba72b042e'
    host='http://www4f.wolframalpha.com'
    server='9'
    related=''
    version='2.6'
    profile='EnterDoQuery:0.,StartWrap:2.74235'>
 <didyoumeans count='2'>
  <didyoumean score='0.365929' level='medium'>Ferrari 458</didyoumean>
  <didyoumean score='0.26087' level='low'>length</didyoumean>
 </didyoumeans>
</queryresult>

How you can get from there to the 2015 Ferrari 458 Italia | overall length query that the web interface manages to extract is not clear from the API documentation.

You can access the didyoumean elements via the Result.tree attribute, using the ElementTree API:

>>> res = client.query('length of a Ferrari 458')
>>> for didyoumean in res.tree.findall('//didyoumean'):
...     print didyoumean.text
...
Ferrari 458
length
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343