[Tutor] Configuaration files and paths?
Allen Fowler
allen.fowler at yahoo.com
Fri Aug 7 01:39:08 CEST 2009
> Martin Walsh <mwalsh at mwalsh.org>
>
> Allen Fowler wrote:
> >
>
> >
> > 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])
>
>
I hear your point. It makes sense.
The above sample code seems like the way to go... if I can structure my modules like that.
For an object that needs many settings, what about passing in an instance of ConfigParser? (Or should I extract the settings to a dict, first?)
Thank you again,
:)
More information about the Tutor
mailing list