From c4ac852a68bf4fc592ab46f3c73f6f0948091871 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 3 Oct 2012 18:44:06 -0400 Subject: [PATCH] Added some objects for working with wolframalpha --- wolframalpha/__init__.py | 27 +++++++++++++++++++++++++++ wolframalpha/test_client.py | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/wolframalpha/__init__.py b/wolframalpha/__init__.py index 2e2ce87..18ab64c 100644 --- a/wolframalpha/__init__.py +++ b/wolframalpha/__init__.py @@ -6,6 +6,33 @@ class Result(object): def __init__(self, stream): self.tree = etree.parse(stream) + @property + def pods(self): + return map(Pod, self.tree.findall('pod')) + +class Pod(object): + def __init__(self, node): + self.node = node + self.__dict__.update(node.attrib) + + def __iter__(self): + return (Content(node) for node in self.nodes.findall('subpod')) + + @property + def main(self): + "The main content of this pod" + return next(iter(self)) + + @property + def text(self): + return self.main.text + +class Content(object): + def __init__(self, node): + self.node = node + self.__dict__.update(node.attrib) + self.text = node.find('plaintext').text + class Client(object): """ Wolfram|Alpha v2.0 client diff --git a/wolframalpha/test_client.py b/wolframalpha/test_client.py index 0989ff5..246cb14 100644 --- a/wolframalpha/test_client.py +++ b/wolframalpha/test_client.py @@ -5,4 +5,5 @@ app_id = 'Q59EW4-7K8AHE858R' def test_basic(): client = wolframalpha.Client(app_id) - client.query('30 deg C in deg F') + res = client.query('30 deg C in deg F') + import pytest; pytest.set_trace()