This is poor programming practice, yes, I know, but these scripts are purely mine and this technique would ease my coding a lot.
Right now, I have an SQLite database containing a set of key-value pairs representing configuration directives for my script. The script itself is a class which I import into other scripts.
So, right now, when I want to access a config variable, I call something like:
myLib.myClass.getConfigVariable("theItemIWant")
This gets really ugly when using config variables in scripts.
So I want to simplify the access to these variables. I could use a dictionary, that is pre-populated when the class is loaded and do:
myLib.myClass.config['theItemIWant']
But I was thinking even a bit more elegantly. I wrote a separate Config class that I'd like to offer variable-level access to the config entries.
So what I want to be able to do is:
myLib.Config().theItemIWant
Or to instantiate an object in a script like this:
def myRoutine(self):
cfg = myLib.Config()
print cfg.theItemIWant
I've read about ugly (using exec) ways to accomplish this, and I'm actually OK with that, but I can't figure out how to set CLASS level variables this way. Most people suggest either using exec or altering either vars or globals, but I am not sure if this will accomplish setting the variables directly on the Config class and not somewhere else.
Using exec failed:
SyntaxError: unqualified exec is not allowed in function '__init__' it contains a nested function with free variables
So the only way I see to do it is to alter vars() but I'm not sure how this applies to classes.