Re-use more of the from_doc constructor and attribute type transformation in the Result object. Restore Pod.subpods through an alias property.
This commit is contained in:
parent
273cd73330
commit
1a4dc2fc62
|
@ -23,11 +23,11 @@ Result objects have `pods` (a Pod is an answer group from Wolfram Alpha)::
|
||||||
for pod in res.pods:
|
for pod in res.pods:
|
||||||
do_something_with(pod)
|
do_something_with(pod)
|
||||||
|
|
||||||
Pod objects have `subpods` (a Subpod is a specific response with the plaintext
|
Pod objects have ``subpods`` (a Subpod is a specific response with the plaintext
|
||||||
reply and some additional info)::
|
reply and some additional info)::
|
||||||
|
|
||||||
for pod in res.pods:
|
for pod in res.pods:
|
||||||
for sub in pod.subpod:
|
for sub in pod.subpods:
|
||||||
print(sub.text)
|
print(sub.text)
|
||||||
|
|
||||||
You may also query for simply the pods which have 'Result' titles or are
|
You may also query for simply the pods which have 'Result' titles or are
|
||||||
|
|
|
@ -82,7 +82,6 @@ class Document(dict):
|
||||||
doc = [doc]
|
doc = [doc]
|
||||||
return map(cls, doc)
|
return map(cls, doc)
|
||||||
|
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
type = self._attr_types.get(name, lambda x: x)
|
type = self._attr_types.get(name, lambda x: x)
|
||||||
attr_name = '@' + name
|
attr_name = '@' + name
|
||||||
|
@ -148,6 +147,10 @@ class Pod(ErrorHandler, Document):
|
||||||
super(Pod, self).__init__(*args, **kwargs)
|
super(Pod, self).__init__(*args, **kwargs)
|
||||||
self._handle_error(self)
|
self._handle_error(self)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def subpods(self):
|
||||||
|
return self.subpod
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def primary(self):
|
def primary(self):
|
||||||
return '@primary' in self and xml_bool(self['@primary'])
|
return '@primary' in self and xml_bool(self['@primary'])
|
||||||
|
@ -168,6 +171,10 @@ class Result(ErrorHandler, Document):
|
||||||
"""
|
"""
|
||||||
Handles processing the response for the programmer.
|
Handles processing the response for the programmer.
|
||||||
"""
|
"""
|
||||||
|
_attr_types = dict(
|
||||||
|
pod=Pod.from_doc,
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
super(Result, self).__init__()
|
super(Result, self).__init__()
|
||||||
doc = xmltodict.parse(stream, dict_constructor=dict)['queryresult']
|
doc = xmltodict.parse(stream, dict_constructor=dict)['queryresult']
|
||||||
|
@ -183,15 +190,15 @@ class Result(ErrorHandler, Document):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pods(self):
|
def pods(self):
|
||||||
return map(Pod, self.get('pod', []))
|
return self.pod
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def assumptions(self):
|
def assumptions(self):
|
||||||
return map(Assumption, self.get('assumptions', []))
|
return Assumption.from_doc(self.get('assumptions', []))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def warnings(self):
|
def warnings(self):
|
||||||
return map(Warning, self.get('warnings', []))
|
return Warning.from_doc(self.get('warnings', []))
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self.info
|
return self.info
|
||||||
|
|
Loading…
Reference in New Issue
Block a user