jaraco / wolframalpha

MIT License
148 stars 26 forks source link

Fails on query 'this is a sentence' #13

Closed meetmangukiya closed 3 years ago

meetmangukiya commented 7 years ago

if pods key is accessed, then it raises a KeyError

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/wolframalpha/__init__.py", line 88, in __getattr__
    val = self[name] if name in self else self[attr_name]
KeyError: '@pods'

if the response is accessed, it raises KeyError as well


In [4]: a
Out[4]: ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/sub/ghubby/coala/corobo/.env/lib/python3.5/site-packages/wolframalpha/__init__.py in __getattr__(self, name)
     87         try:
---> 88             val = self[name] if name in self else self[attr_name]
     89         except KeyError:

KeyError: '@info'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    670                 type_pprinters=self.type_printers,
    671                 deferred_pprinters=self.deferred_printers)
--> 672             printer.pretty(obj)
    673             printer.flush()
    674             return stream.getvalue()

/usr/local/lib/python3.5/dist-packages/IPython/lib/pretty.py in pretty(self, obj)
    366                 if cls in self.type_pprinters:
    367                     # printer registered in self.type_pprinters
--> 368                     return self.type_pprinters[cls](obj, self, cycle)
    369                 else:
    370                     # deferred printer

/usr/local/lib/python3.5/dist-packages/IPython/lib/pretty.py in inner(obj, p, cycle)
    610         keys = obj.keys()
    611         # if dict isn't large enough to be truncated, sort keys before displaying
--> 612         if not (p.max_seq_length and len(obj) >= p.max_seq_length):
    613             try:
    614                 keys = sorted(keys)

/sub/ghubby/coala/corobo/.env/lib/python3.5/site-packages/wolframalpha/__init__.py in __len__(self)
    201 
    202     def __len__(self):
--> 203         return sum(1 for _ in self.info)
    204 
    205     @property

/sub/ghubby/coala/corobo/.env/lib/python3.5/site-packages/wolframalpha/__init__.py in __getattr__(self, name)
     88             val = self[name] if name in self else self[attr_name]
     89         except KeyError:
---> 90             raise AttributeError(name)
     91         return type(val)
     92 

AttributeError: info
meetmangukiya commented 7 years ago

I guess there should be a check that the response contains the used keys, if it doesn't set sane empty values?

ACCIAI0 commented 7 years ago

The code raises an exception when the result can be considered as a table formatted result. for instance: searching for "wet country", for the script there is no pod "result". I make a request for only input interpretation and result pod and if i try accessing the result via pods[1], it gives me an index out of bounds exception.

jaraco commented 5 years ago

Contributions are welcome.

jaraco commented 3 years ago

I just gave this a try.

wolframalpha main $ .tox/python/bin/python
Python 3.9.0 (v3.9.0:9cf6752276, Oct  5 2020, 11:29:23) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wolframalpha as wa
>>> cl = wa.Client.from_env()
>>> res = cl.query('this is a sentence')
>>> res
{'@success': 'false', '@error': 'false', '@numpods': '0', '@datatypes': '', '@timedout': '', '@timedoutpods': '', '@timing': '1.195', '@parsetiming': '0.511', '@parsetimedout': 'false', '@recalculate': '', '@id': '', '@parseidserver': '41', '@host': 'https://www5b.wolframalpha.com', '@server': '41', '@related': '', '@version': '2.6', 'didyoumeans': {'@count': '1', 'didyoumean': {'@score': '0.361698', '@level': 'medium', '#text': 'sentence'}}}
>>> res.pods
Traceback (most recent call last):
  File "/Users/jaraco/code/main/wolframalpha/wolframalpha/__init__.py", line 150, in __getattr__
    val = self[name] if name in self else self[attr_name]
KeyError: '@pods'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jaraco/code/main/wolframalpha/wolframalpha/__init__.py", line 152, in __getattr__
    raise AttributeError(name)
AttributeError: pods
>>> res.success
'false'

It looks like the query returned a non-success result and numpods is '0', so I wouldn't expect pods to be useful here. Probably the library could provide a nicer interface.