-3

I have a situation in python, I need to Load a module and Instantiate a class based on value provided in INI config.

[providers]
provider=bsnl  ; provider may be bsnl/airtel/vodafone/etc..

In my class I am reading this value and in a class method I have to load bsnl_provider.py and has to instantiate BSNLClass. If provider changes to airtel then I have to load airtel_provider.py and has to instantiate AIRTElClass.

How can I load relevant module and instantiate relevant class?

Zack Tarr
  • 851
  • 1
  • 8
  • 28
Krishh
  • 680
  • 1
  • 6
  • 19
  • 1
    Perhaps you should simplify the problem by exposing a mapping `{'airtel': AIRTElClass, ...}` somewhere? But it's not clear which part of the problem you're actually stuck on, or what effort you've put into solving it so far; if you want to load a module by name, for example, see e.g. https://stackoverflow.com/q/301134/3001761. – jonrsharpe Feb 22 '18 at 13:14
  • If i add mapping like that in a class level i future, if any new provider comes, again mapping has to be changed, how can i avoid that situation? – Krishh Feb 22 '18 at 13:17
  • You could have some superclass/metaclass/decorator to register classes, but again that's a topic you should research as a separate problem. – jonrsharpe Feb 22 '18 at 13:18
  • can you elaborate it so that i can do research on it? – Krishh Feb 22 '18 at 13:20
  • Well it depends on which method you choose, that's why I'm suggesting you do the work. For the superclass option, for example, see https://stackoverflow.com/q/3862310/3001761. If you have *a specific question* about an implementation then you can ask that later, but this is currently far too broad. – jonrsharpe Feb 22 '18 at 13:22
  • currently i am doing as provider = config.get_provider() p_module = importlib.import_module(provider+'_provider') p_class = getattr(p_module, 'Manage'+provider.title()+'Class') – Krishh Feb 22 '18 at 13:22
  • Then [edit] the question to give a [mcve]. – jonrsharpe Feb 22 '18 at 13:24
  • I got it and i can read all the subclasses of a class. But, how can i define which class i have to instantiate it? – Krishh Feb 22 '18 at 13:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165649/discussion-between-krishh-and-jonrsharpe). – Krishh Feb 22 '18 at 13:40
  • Let me extend my question, As Suggested, I declared the dictionary with provider_modules = {'bsnl':'bsnl_sms', 'airtel':'airtel_sms'}. But, in future if i want to add one more provider, then without touching the code, who can i do that? – Krishh Mar 02 '18 at 09:01

1 Answers1

-1

If I understand well, you want to get the name of future class from the ini file?

  import ConfigParser

  config = ConfigParser.ConfigParser()
  config.read('providers.ini')

  try:
      provider_name = config.get("providers", "provider")
  except ConfigParser.NoOptionError:
      print("Unable to find provider")
      raise

After that, you can create dynamically your class (without any superclass):

provider_class = type(provider_name.upper(), (,), {}) 

or with:

provider_class = type(provider_name.upper(), (MySuperClass,), {}) 
ibt23sec5
  • 369
  • 3
  • 13