bazyl
bazyl

Reputation: 263

Getting variable from other class

as totally beginner I need to ask you for some help. I defined class Config which take from config.ini file some information and put them into variable. Now I define class : Connection, which base of result from class Config. I was trying to do it many ways, but finally give up. Could anyone take a look ?

class Config:
    def __init__(self,system):   
        self.config = configparser.ConfigParser()
        self.config.read("config.ini")
        self.connection_source=self.config.get(system,'Source')
        self.system=system

    def getsystemSources(self):
        return self.connection_source

    def getConnection(self,source):
        self.source=source
        self.connection_string=self.config.get('CONNECTION',self.system+'_'+source+'_'+'connectstring')   ## Connection
        self.connection_user=self.config.get('CONNECTION',self.system+'_'+source+'_'+'user')   ## Connection user
        self.connection_password=self.config.get('CONNECTION',self.system+'_'+source+'_'+'password')   ## Connection pass  

class Connection(Config):
    def __init__ (self):
        self.connection_string=Config.connection_string
        self.connection_user=Config.connection_user
        self.connection_password=Config.connection_user
        self.connection_source=Config.connection_source
    def conn_function(self):
        print (self.connection_string)
        print (self.connection_user)
        print (self.connection_password)

emp1 = Config('Windows')    
value=emp1.getsystemSources()   
print (value)   
emp2 = Connection()    -> how to run it ? 

Upvotes: 0

Views: 51

Answers (1)

dheiberg
dheiberg

Reputation: 1914

You simply pass the config object into the __init__ function

class Config:
    def __init__(self,system):   
        self.config = configparser.ConfigParser()
        self.config.read("config.ini")
        self.connection_source=self.config.get(system,'Source')
        self.getConnection(self.connection_source)
        self.system=system

class Connection(Config):
    def __init__ (self, system):
        Config.__init__(self, system)

emp1 = Connection('Windows')
emp1.conn_function()

Upvotes: 1

Related Questions