python - creating a list of class objects -


i trying create list of python class objects.

basically expecting list should follows:

[<report {u'invoice_id': u'demo-1-2016-dummy', u'tenant_id': u'27724b57e3b744f89cbf1336da062b43'}>, <report {u'invoice_id': u'demo-2-2016', u'tenant_id': u'27724b57e3b744f89cbf1336da062b43'}>, <report {u'invoice_id': u'admin-2-2016', u'tenant_id': u'6cd02afa3f0f403489153c41e98d07bc'}>] 

i have tried following code same.

from cloudkittyclient.common import base import json  class reportresult(base.resource):      key = 'report'      def __repr__(self):         return "<report %s>" % self._info  class reportmanager(base.crudmanager):      base_url = '/v1/report'     resource_class = reportresult     key = "report"     collection_key = "reports"      # list invoices, can accept all-tenants arg     def list_invoice(self, all_tenants=none):         url = self.base_url + "/list_invoice"         filters = list()         if all_tenants:             filters.append("all_tenants=%s" % all_tenants)         if filters:             url += "?%s" % ('&'.join(filters))         return self.client.get(url).json() 

so here requirement "return self.client.get(url).json()" should return above mentioned list consists of class objects.

but returning results follows:

[{u'invoice_id': u'demo-1-2016-dummy', u'tenant_id': u'27724b57e3b744f89cbf1336da062b43'}, {u'invoice_id': u'demo-2-2016', u'tenant_id': u'27724b57e3b744f89cbf1336da062b43'}, {u'invoice_id': u'admin-2-2016', u'tenant_id': u'6cd02afa3f0f403489153c41e98d07bc'}] 

i know fact missing right here.

as novice user in python unable find going wrong.

can able assist me getting result expected.

tl;dr:

return [self.resource_class(self, j, loaded=true)         j in self.client.get(url).json() if j] 

you should not have handle url creation yourself. crudmanager should take care of you, if use methods offered.

try

report_manager.get(report_id=my_report_id) 

where my_report_id variable containing id, if you're trying fetch single report, instead of

report_manager.client.get(url).json() 

looking cloudkittyclient.openstack.common.apiclient.base.basemanager._get self.client.get(url).json() internally, before deserializes json self.resource_class instance. should not doing directly.

cloudkittyclient.common.base.crudmanager uses _get internally , offers override of get handle base_url catenation etc you.

also seems should using cloudkittyclient.common.base.crudmanager.findall or cloudkittyclient.common.base.crudmanager.list, since have method list_invoice.

report_manager.findall() 

or

report_manager.findall(all_tenants=all_tenants) 

where all_tenants variable ever pass custom list_invoice method.

finally, if findall or crudmanager.list unsuitable needs, seems case since collection url differs 1 in class, have deserialize results of self.client.get(url).json() yourself. instead of returning that, do

return [self.resource_class(self, j, loaded=true)         j in self.client.get(url).json() if j] 

which creates list of instances of self.resource_class class (reportresult here) python dictionaries have been deserialized response of json data.

consider overloading list method of crudmanager, seems way such thing.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -