Provide an interface to supply multiple parameters (even with the same name) to the query. Renamed 'query' to 'input' for clarity and to align with the API.

This commit is contained in:
Jason R. Coombs 2016-09-03 11:18:40 -04:00
parent 788746c595
commit bee228a1d5
2 changed files with 24 additions and 6 deletions

View File

@ -1,7 +1,12 @@
3.0 3.0
=== ===
Changed to using dictionaries instead of parsing XML. * Models now parse the results using xmltodict.
* Changed parameter of ``Client.query`` from
``query`` to ``input``.
* ``Client.query`` now accepts keyword arguments
and parameters passed directly to Wolfram|Alpha
as URL parameters.
2.4 2.4
=== ===

View File

@ -1,4 +1,7 @@
import itertools
from six.moves import urllib from six.moves import urllib
import xmltodict import xmltodict
from . import compat from . import compat
@ -16,22 +19,32 @@ class Client(object):
def __init__(self, app_id='Q59EW4-7K8AHE858R'): def __init__(self, app_id='Q59EW4-7K8AHE858R'):
self.app_id = app_id self.app_id = app_id
def query(self, query, **data): def query(self, input, params=(), **kwargs):
""" """
Query Wolfram|Alpha using the v2.0 API Query Wolfram|Alpha using the v2.0 API
Allows for arbitrary parameters (data) to be passed in Allows for arbitrary parameters to be passed in
the query. For example, to pass assumptions: the query. For example, to pass assumptions:
client.query(query='pi', assumption='*C.pi-_*NamedConstant-') client.query(input='pi', assumption='*C.pi-_*NamedConstant-')
To pass multiple assumptions, pass multiple items
as params:
params = (
('assumption', '*C.pi-_*NamedConstant-'),
('assumption', 'DateOrder_**Day.Month.Year--'),
)
client.query(input='pi', params=params)
For more details on Assumptions, see For more details on Assumptions, see
https://products.wolframalpha.com/api/documentation.html#6 https://products.wolframalpha.com/api/documentation.html#6
""" """
data.update( data = dict(
input=query, input=input,
appid=self.app_id, appid=self.app_id,
) )
data = itertools.chain(params, data.items(), kwargs.items())
query = urllib.parse.urlencode(data) query = urllib.parse.urlencode(data)
url = 'https://api.wolframalpha.com/v2/query?' + query url = 'https://api.wolframalpha.com/v2/query?' + query