0

I am using a function to instansiate the python classes .

Hers is the class structure

from DB.models import ApiKey,ServiceProvider

class SMSMrg( object ):
    _instance = None
    class Singleton:
        def __init__(self):
            self.username = None
            self.password = None
            self.allsp = []
            self.classnames = {}
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(SMSMrg, cls).__new__(
                                cls, *args, **kwargs)
        return cls._instance

    def loadsettings(self):

        get_all_sp = ServiceProvider.objects.filter(status = False)
        for (options,obj) in enumerate(get_all_sp):
            cla = str(obj.class_Name)
            self.classnames[cla] = cla
        print self.classnames

        for (options,obj) in enumerate(get_all_sp):
            cla = str(obj.class_Name)
            class_object = self.classnames[cla](obj.userName,obj.password,obj.sendingurl)

       # self.allsp = get_all_sp 
    def send(self):
        print "+++++++++++++++++++== Global send "


if __name__ == "__main__":

    b = SMSMrg()
    b.loadsettings()

I have stored the classnames in database and I have defined each class structures on different files .

Like cla will contain a class name .

But when i am calling above function i am getting the type error .

Traceback (most recent call last):
  File "allsms.py", line 30, in <module>
    b.loadsettings()
  File "allsms.py", line 21, in loadsettings
    class_object = cla(obj.userName,obj.password,obj.sendingurl)
TypeError: 'str' object is not callable

Please tell me how can instansiate all the classes which names are present in my db .

3
  • What is obj? Is it an instance of the class? Commented Feb 5, 2013 at 6:41
  • yes it is instance of the model class Commented Feb 5, 2013 at 6:45
  • Why are you writing Singleton as a class inside a class? Also is __new__ badly indented or is it really part of SMSMrg and not of Singleton? Commented Feb 5, 2013 at 8:08

4 Answers 4

1

On the line cla = str(SERVIVEPROVIDER) you convert SERVIVEPROVIDER to string. And on the next line you are trying to call it, thus you get an error...

Sign up to request clarification or add additional context in comments.

Comments

1
 # Means `cla` is pointing to a string
cla = str(SERVIVEPROVIDER)

# there is no function called `cla` now it contains a string
cla(obj.userName,obj.password,obj.sendingurl)

Comments

0

As you said cla contains the name of the class, which means that you can't use it as a callable.

You can build a dict and take the class object from there:

from somemodule import SomeClass

class TheClass(object):
    def __init__(self, username, password, url):
        #do stuff

class AnOtherClass(object):
    def __init__(self, username, password, url):
        # do stuff

CLASS_NAMES_TO_CLASSES = {
    # Note: TheClass is *not* a string, is the class!!!
    'FirstName': TheClass,
    'SecondName': AnOtherClass,
    'SomeClass': SomeClass,
    }

class SMSMrg(object):
    #do stuff
    def loadsettings(self):
       get_all_sp = ServiceProvider.objects.filter(status = True)
       for obj in get_all_sp:
           SERVIVEPROVIDER = obj.class_Name
           cla = str(SERVIVEPROVIDER)
           class_object = CLASS_NAMES_TO_CLASSES[cla](obj.userName,obj.password,obj.sendingurl)

This method requires you to be able to build such a dict, so either you know ahead which classes could end up in the db or you can't use this method.

Note that CLASS_NAMES_TO_CLASSES is not a dictionary that maps strings to strings. It maps strings to class objects. If you import the class SomeClass from a module then you have to put it inside the dictionary.

An other method could be to use eval to evaluate the class name, but you should avoid this if the db contains data from users(which is not safe).

An other option that might turn out useful is to avoid saving the class names and instead use pickle to save the instances directly.

7 Comments

@masterofdestiny You are not doing what I showed here. I'll update my answer to make clearer what I meant.
MY DICT IS RETURNING {u'MessageNet': u'MessageNet'}
@masterofdestiny As I said you are doing it wrong. The dict should not be created from the database. You must create it before. Look at my example. The dict is created at the module level with all the classes that could come up. If that's not your situation then you must provide more information about where the classes are defined etc, otherwise we can't tell you how to solve this.
as i said "I have stored the classnames in database and I have defined each class structures on different files" every information like classname are stored in the database only and my intention is whenever i run this script all the classes present in the database must instansiated so that i am creating this application as singleton .
@masterofdestiny To instantiate the classes you must be able to access them in some way. If you can do that, then you can also build a dict of names to classes like the one I showed here. You can use getattr if you don't want to hardcode the class names. Also you can pickle the dict obtained in the database and retrieve it for later use. On a side note I think it would have been easier to simply pickle the instance objects into the database, in this way everything is handled for you by pickle.
|
0

Please tell me how can instansiate all the classes which names are present in my db .

Try this:

class A(object): pass
class B(object): pass

class_names = {'first': A, 'second': B}
obj = class_names['first']()
type(obj)
<class 'yourmodule.A'>

Or, if your classes are stored somewhere else, say in a module called mymodule:

import mymodule
obj = getattr(mymodule, 'A')()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.