diff --git a/CHANGES.rst b/CHANGES.rst index fbc4148..a8d30fa 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,8 @@ * ``Client.query`` now accepts keyword arguments and parameters passed directly to Wolfram|Alpha as URL parameters. +* ``Result.pods`` now returns an iterator and not + a list. 2.4 === diff --git a/wolframalpha/__init__.py b/wolframalpha/__init__.py index 33b1668..3e72f59 100644 --- a/wolframalpha/__init__.py +++ b/wolframalpha/__init__.py @@ -74,28 +74,31 @@ class Result(ErrorHandler, object): def __init__(self, stream): self.tree = xmltodict.parse(stream, dict_constructor=dict)['queryresult'] self._handle_error(self.tree) - self.info = [] - try: - self.pods = list(map(Pod, self.tree['pod'])) - self.info.append(self.pods) - except KeyError: - self.pods = None - try: - self.assumptions = list(map(Assumption, self.tree['assumptions'])) - self.info.append(self.assumptions) - except KeyError: - self.assumptions = None - try: - self.warnings = list(map(Warning, self.tree['warnings'])) - self.info.append(self.warnings) - except KeyError: - self.warnings = None + + @property + def info(self): + """ + The pods, assumptions, and warnings of this result. + """ + return itertools.chain(self.pods, self.assumptions, self.warnings) + + @property + def pods(self): + return map(Pod, self.tree.get('pod', [])) + + @property + def assumptions(self): + return map(Assumption, self.tree.get('assumptions', [])) + + @property + def warnings(self): + return map(Warning, self.tree.get('warnings', [])) def __iter__(self): - return iter(self.info) + return self.info def __len__(self): - return len(self.info) + return sum(1 for _ in self.info) @property def results(self): diff --git a/wolframalpha/test_client.py b/wolframalpha/test_client.py index b33b6b9..004294e 100644 --- a/wolframalpha/test_client.py +++ b/wolframalpha/test_client.py @@ -9,7 +9,7 @@ import pytest import wolframalpha -@pytest.fixture +@pytest.fixture(scope='session') def API_key(): """ To run the tests fully, the environment must be configured @@ -22,26 +22,49 @@ def API_key(): pytest.skip("Need WOLFRAMALPHA_API_KEY in environment") -@pytest.fixture +@pytest.fixture(scope='session') def client(API_key): return wolframalpha.Client(API_key) -def test_basic(client): - res = client.query('30 deg C in deg F') - assert len(res.pods) > 0 + +@pytest.fixture(scope='module') +def temp_result(client): + return client.query('30 deg C in deg F') + +def test_basic(temp_result): + res = temp_result + assert len(list(res.pods)) > 0 result, = res.results assert result.text == '86 °F (degrees Fahrenheit)' assert result.texts == ['86 °F (degrees Fahrenheit)'] -def test_results_iterator(client): +def test_results_iterator(temp_result): """ A Result.results should be an iterator. """ - res = client.query('30 deg C in deg F') + res = temp_result next(res.results) +def test_properties(temp_result): + """ + A result should have a number of properties. + """ + res = temp_result + info = list(res.info) + warnings = list(res.warnings) + assert all(isinstance(item, wolframalpha.Warning) for item in warnings) + assumptions = list(res.assumptions) + assert all( + isinstance(item, wolframalpha.Assumption) + for item in assumptions + ) + pods = list(res.pods) + assert all(isinstance(item, wolframalpha.Pod) for item in pods) + assert len(info) == len(pods) + len(warnings) + len(assumptions) + + def test_invalid_app_id(): client = wolframalpha.Client('abcdefg') with pytest.raises(Exception):