Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiate a Class as an Iterable
    primarykey
    data
    text
    <p>I have a Class that I'm instantiating and then passing into a Tornado Web template. Both functions return a list, but I'm missing something in making the Class itself an iterable object. I'm afraid it's something fundamental I'm doing incorrectly. I'm making REST API calls, parsing the returned XML and returning some of the data to the webapp. Here's the code:</p> <p>The API calls:</p> <pre><code>class GetVMList: def __init__(self): user = 'contoso\\administrator' password = "apassword" url = "http://scspf:8090/SC2012/VMM/Microsoft.Management.Odata.svc/VirtualMachines?$filter=VMMServer%20eq%20'scvmm'" passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, user, password) # create the NTLM authentication handler auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman) # create and install the opener opener = urllib2.build_opener(auth_NTLM) urllib2.install_opener(opener) # retrieve the result self.response = urllib2.urlopen(url) self.data = self.response.read() def name(self): dom = parseString(self.data) raw_xml = dom.getElementsByTagName('d:Name') clean_xml = [] clean_data = [] for i in raw_xml: clean_xml.append(i.toxml()) for i in clean_xml: clean_data.append(i.replace('&lt;d:Name&gt;', '').replace('&lt;/d:Name&gt;', '')) return clean_data def os(self): dom = parseString(self.data) raw_xml = dom.getElementsByTagName('d:OperatingSystem') clean_xml = [] clean_data = [] for i in raw_xml: clean_xml.append(i.toxml()) for i in clean_xml: clean_data.append(i.replace('&lt;d:OperatingSystem&gt;', '').replace('&lt;/d:OperatingSystem&gt;', '')) return clean_data </code></pre> <p>The instantiation:</p> <pre><code>class ListHandler(tornado.web.RequestHandler): def get(self): self.render('temp/search.html', data='') def post(self): vm_list = GetVMList() self.render('temp/search.html', data=vm_list) </code></pre> <p>And then the template contains this: </p> <pre><code>{% for vm in data %} &lt;li&gt;{{ vm.name }} running {{ vm.os }}&lt;/li&gt; {% end %} </code></pre> <p>The error is: <code>TypeError: iteration over non-sequence</code>. I would imagine I need to use <code>__iter__</code> in my Class, but I'm not sure I understand exactly how it works.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload