models now derive from a dict (via Document), and store their content natively in self.

This commit is contained in:
Jason R. Coombs 2016-09-03 13:43:13 -04:00
parent be0938dded
commit 8ed3be43e1

View File

@ -67,13 +67,19 @@ class ErrorHandler(object):
raise Exception(template.format(code=code, msg=msg)) raise Exception(template.format(code=code, msg=msg))
class Result(ErrorHandler, object): class Document(dict):
pass
class Result(ErrorHandler, Document):
""" """
Handles processing the response for the programmer. Handles processing the response for the programmer.
""" """
def __init__(self, stream): def __init__(self, stream):
self._doc = xmltodict.parse(stream, dict_constructor=dict)['queryresult'] super(Result, self).__init__()
self._handle_error(self._doc) doc = xmltodict.parse(stream, dict_constructor=dict)['queryresult']
self.update(doc)
self._handle_error(self)
@property @property
def info(self): def info(self):
@ -84,15 +90,15 @@ class Result(ErrorHandler, object):
@property @property
def pods(self): def pods(self):
return map(Pod, self._doc.get('pod', [])) return map(Pod, self.get('pod', []))
@property @property
def assumptions(self): def assumptions(self):
return map(Assumption, self._doc.get('assumptions', [])) return map(Assumption, self.get('assumptions', []))
@property @property
def warnings(self): def warnings(self):
return map(Warning, self._doc.get('warnings', [])) return map(Warning, self.get('warnings', []))
def __iter__(self): def __iter__(self):
return self.info return self.info
@ -115,32 +121,26 @@ class Result(ErrorHandler, object):
return {pod.title: pod.text for pod in self.pods} return {pod.title: pod.text for pod in self.pods}
class Pod(ErrorHandler, object): class Pod(ErrorHandler, Document):
""" """
Groups answers and information contextualizing those answers. Groups answers and information contextualizing those answers.
""" """
def __init__(self, doc): def __init__(self, *args, **kwargs):
self._doc = doc super(Pod, self).__init__(*args, **kwargs)
self.error = doc['@error'] self.error = self['@error']
self._handle_error(doc) self._handle_error(self)
self.title = doc['@title'] self.title = self['@title']
self.scanner = doc['@scanner'] self.scanner = self['@scanner']
self.id = doc['@id'] self.id = self['@id']
self.position = float(doc['@position']) self.position = float(self['@position'])
self.number_of_subpods = int(doc['@numsubpods']) self.number_of_subpods = int(self['@numsubpods'])
self.subpods = doc['subpod'] self.subpods = self['subpod']
# Allow subpods to be accessed in a consistent way, # Allow subpods to be accessed in a consistent way,
# as a list, regardless of how many there are. # as a list, regardless of how many there are.
if type(self.subpods) != list: if type(self.subpods) != list:
self.subpods = [self.subpods] self.subpods = [self.subpods]
self.subpods = list(map(Subpod, self.subpods)) self.subpods = list(map(Subpod, self.subpods))
self.primary = '@primary' in doc and doc['@primary'] != 'false' self.primary = '@primary' in self and self['@primary'] != 'false'
def __iter__(self):
return iter(self.subpods)
def __len__(self):
return self.number_of_subpods
@property @property
def texts(self): def texts(self):
@ -151,18 +151,18 @@ class Pod(ErrorHandler, object):
@property @property
def text(self): def text(self):
return next(iter(self)).text return next(iter(self.subpods)).text
class Subpod(object): class Subpod(Document):
""" """
Holds a specific answer or additional information relevant to said answer. Holds a specific answer or additional information relevant to said answer.
""" """
def __init__(self, doc): def __init__(self, *args, **kwargs):
self._doc = doc super(Subpod, self).__init__(*args, **kwargs)
self.title = doc['@title'] self.title = self['@title']
self.text = doc['plaintext'] self.text = self['plaintext']
self.img = doc['img'] self.img = self['img']
# Allow images to be accessed in a consistent way, # Allow images to be accessed in a consistent way,
# as a list, regardless of how many there are. # as a list, regardless of how many there are.
if type(self.img) != list: if type(self.img) != list:
@ -170,18 +170,7 @@ class Subpod(object):
self.img = list(map(Image, self.img)) self.img = list(map(Image, self.img))
# Needs work. At the moment this should be considered a placeholder. class Assumption(Document):
class Assumption(object):
def __init__(self, doc):
self._doc = doc
#self.description = self._doc[0]['desc'] # We only care about our given assumption.
def __iter__(self):
return iter(self._doc)
def __len__(self):
return len(self._doc)
@property @property
def text(self): def text(self):
text = self.template.replace('${desc1}', self.description) text = self.template.replace('${desc1}', self.description)
@ -192,27 +181,19 @@ class Assumption(object):
return text[:text.index('. ') + 1] return text[:text.index('. ') + 1]
# Needs work. At the moment this should be considered a placeholder. class Warning(Document):
class Warning(object): pass
def __init__(self, doc):
self._doc = doc
def __iter__(self):
return iter(self._doc)
def __len__(self):
return len(self._doc)
class Image(object): class Image(Document):
""" """
Holds information about an image included with an answer. Holds information about an image included with an answer.
""" """
def __init__(self, node): def __init__(self, *args, **kwargs):
self.node = node super(Image, self).__init__(*args, **kwargs)
self.title = node['@title'] self.title = self['@title']
self.alt = node['@alt'] self.alt = self['@alt']
self.height = node['@height'] self.height = self['@height']
self.width = node['@width'] self.width = self['@width']
self.src = node['@src'] self.src = self['@src']