creating a list of class objects

iamnewuser

I am trying to create a list of python class objects.

Basically I am expecting for list which should be as 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 the following code for the 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 the 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 my requirement is that "return self.client.get(url).json()" should return the above mentioned List which consists of class objects.

But it is returning the results as 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 the fact that I am missing something right here.

As I am novice user in python I am unable to find that where I am going wrong.

Can anyone be able to assist me with getting the result I expected.

Ilja Everilä

TL;DR:

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

You should not have to handle URL creation yourself. The CrudManager should take care of that for you, if you use the methods offered.

Try

report_manager.get(report_id=my_report_id)

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

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

Looking into cloudkittyclient.openstack.common.apiclient.base.BaseManager._get that self.client.get(url).json() is what it does internally, before it then deserializes the JSON to self.resource_class instance. You probably should not be doing that directly.

cloudkittyclient.common.base.CrudManager uses the _get internally and offers an override of get that will handle base_url catenation etc for you.

Also it seems you should be using cloudkittyclient.common.base.CrudManager.findAll or cloudkittyclient.common.base.CrudManager.list, since you have a method like list_invoice.

report_manager.findAll()

or

report_manager.findAll(all_tenants=all_tenants)

where all_tenants is a variable with what ever you would pass to your custom list_invoice method.

Finally, if findAll or CrudManager.list is really unsuitable to your needs, which seems to be the case since your collection url differs from the one in the class, you have to deserialize the results of self.client.get(url).json() yourself. So instead of returning that, do

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

which creates a list of instances of self.resource_class class (ReportResult here) from python dictionaries that have been deserialized from a response of JSON data.

Consider overloading the list method of CrudManager, as it seems to be the way to do such a thing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating a list of objects with a class in Python

From Dev

Serialize list of objects with json_serializable without creating extra class

From Dev

Creating List of abstract base class to fill with inherited objects

From Dev

Creating objects in a class in Python

From Dev

Creating a Class and Objects

From Dev

Creating pairs of objects in a list

From Javascript

creating list of objects in Javascript

From Java

Creating List of Objects in JAVA

From Dev

creating a list of list objects terraform

From Dev

Creating new objects with another class

From Dev

Creating an array of all objects of a class

From Dev

Dynamically creating a number of class objects

From Dev

Creating an array of objects within a class

From Dev

Creating list of Objects from Database

From Java

Creating a List of Lists/Objects in java

From Dev

Creating a List of Objects From an Array

From Dev

Creating a list of objects from a dictionary

From Java

List of Class Objects in Dart

From Dev

List comprehensions with class objects

From Dev

store list of objects in class

From Dev

Creating Reads for List[case class]

From Dev

R - Creating a class with a list attribute

From Dev

Creating a class vs using a dict for "smaller" objects

From Dev

Creating Doctrine objects from a Behat Context class

From Dev

Vector Isn't Creating Multiple Class Objects

From Dev

Creating an enclosing class for two different objects in java

From Dev

Trouble creating objects for class in windows phone

From Java

creating objects with same name as class in java

From Dev

Creating multiple class objects using a loop