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 .
As you said
clacontains the name of the class, which means that you can’t use it as a callable.You can build a
dictand take the class object from there: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_CLASSESis not a dictionary that maps strings to strings. It maps strings to class objects. If you import the classSomeClassfrom a module then you have to put it inside the dictionary.An other method could be to use
evalto 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
pickleto save the instances directly.