1

I have a semi-large python app that runs on linux. I am trying to set it up so that i can read a config file at program startup and then save those values for use at any time while the application is running, without re-reading the config file.

So I am trying to load a configValues class in my first modual, test.py. And read the values set. Then in this example read the values again in test2.py.

I never get the values. Can someone help me?

Config.py

class config():

    def __init__(self):
        configFile = File(myPath)
        if configFile.exist():
            myXML = str(configFile.openAndRead())

    def setupValues(self):
        configValues.color = self.getElement('color')

    def getElement(self, element):
        tree=et.fromstring(self.myXML)

        for el in tree.findall('head'):
            for ch in el.findall(element):
            return ch.text


class configValues():

    def __init__(self):
        global color

test.py

import config

class test():

    def __init__(self):
        configObj = config.Config()
        configVal = config.configValues()

        configObj.setupValues()
        print configVal.color

test2.py

import config

class test2():

    def __init__(self):
        configVal = config.configValues()

        print configVal.color
Lawrence Benson
  • 1,398
  • 1
  • 16
  • 33

2 Answers2

0

Add a global variable and a function to your config.py. And rather than creating configobj in test1.py and test2.py, you can call this function to get the config object.

configobj = None

def getconfigobj():
  global configobj
  if not configobj:
    configObj = config.Config()
  return configobj

Edit as per comment. Does something like the following help(using a single instance for the class)?:-

config.py

 class config():
        _instance = None
        def __new__(cls):
            if config._instance:
                return config._instance
            return super(config, cls).__new__(cls)


        def __init__(self, myPath):
            configFile = File(myPath)
            if configFile.exist():
                myXML = str(configFile.openAndRead())
            config._dict['instance'] = self

        def setupValues(self):
            self.color = self.getElement('color')

        def getElement(self, element):
            tree=et.fromstring(self.myXML)

            for el in tree.findall('head'):
                for ch in el.findall(element):
                return ch.text

test1.py

import config
class test():
    def __init__(self):
        configObj = config.Config()
        configObj.setupValues()
        print configObj.color

test2.py

import config
class test2():
    def __init__(self):
        configObj = config.Config()
        print configObj.color
Barun Sharma
  • 1,452
  • 2
  • 15
  • 20
0

I would not use global variables in this case. You may want to assign the config values as config properties and use it like so: config.py

class Config:
    def __init__(self):
        # property of the config class
        self.color = 'my default color'
        # open your config file ..

    def setup_values(self):
        # assign new value
        self.color = self.get_color()

    def get_color(self):
        # get values from config xml file ..
        return "red"

And then import the config in other module and call the color property: main.py

from config import Config

class Main:
    def __init__(self):
        config = Config()
        config.setup_values()
        color = config.color
        print color

Main()

I would also shorten the code with one method less by getting the config values in the constructor instead in additional method "setup_values" like so:

class Config:
    def __init__(self):
        # property of the config class
        self.color = self.get_color()

    def get_color(self):
        # open your config file ..
        # get values from config xml file ..
        return "red"

and the main.py should look like:

from config import Config

class Main:
    def __init__(self):
        config = Config()
        color = config.color
        print color

Main()

Bear in mind that it is not a good practice to use global variables in your code so the suggested code above may do the job for you.

Velin Georgiev
  • 2,359
  • 1
  • 15
  • 21
  • The problem with that is that every time I instantiate Config, it will read the xml file from the file system. I'm trying to avoid that kind of hit every time I need the values. – TheBigOnion Sep 09 '15 at 15:16
  • I would use some kind of store as you did in xml or json or sqllite and get the values from there every time I need them instead of using global variables if it is even possible. – Velin Georgiev Sep 09 '15 at 16:10