[Tutor] Configuaration files and paths?
Martin Walsh
mwalsh at mwalsh.org
Thu Aug 6 22:57:30 CEST 2009
Allen Fowler wrote:
>
<snip>
>
> As a follow-up question, how do give my modules stored under ./lib access to the data in my ConfigParser object? (For instance, database connection string, storage path, etc.)
>
> I guess a global ConfigParser object would work, but that seems wrong.
>
And yet, to me it seems wrong to have more than one instance of config
data floating around. Instead of using a global you can pass the config
object, or just the appropriate attributes, around to your lib
functions/classes, as necessary -- and keep the flow in your main
script. For example (highly speculative, FWIW),
# app.py
from database.connection import getcursor
from lib.persist import Storage
def main():
confp = ConfigParser()
confp.read(join_relative(__file__, 'config/prefs.ini'))
config = confp.defaults()
curs = getcursor(config[db_connection_string])
...
storage = Storage(config[storage_path])
...
HTH,
Marty
More information about the Tutor
mailing list