pythonic way of making a config module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew.fabbro@gmail.com

    #1

    pythonic way of making a config module

    I'm working on an app that will be deployed on several different
    servers. In each case, I'll want to change some config info (database
    name, paths, etc.)

    In perl, I would have done something like this:

    Package Config;
    <some exporting mechanics>
    $dbname = "somename";
    etc.

    And then use'd the module and referenced the vars as $Config::dbname .

    What is a pythonic equivalent? These vars are only changed usually at
    install time.

    I could set up a class and then instantiate it, but that seems a
    waste...I guess what I'm looking for is either (a) a way to suck
    variables into a namespace keeping a prefix (without having to prefix
    all the vars with something like config_) - e.g., reference them as
    config.dbname or something - is this execfile? or (b) class variables,
    which is the route I usually take in Java if not using preferences,
    etc.

    I'm aware of configParser and such, and could certainly write a module
    that parses a configuration file, but using a native python module
    seems the easiest. I want to do something reasonably pythonic so the
    python Gods do not smite me for some stupid perlism ;)

    Thanks.

  • Fuzzyman

    #2
    Re: pythonic way of making a config module


    andrew.fab...@g mail.com wrote:[color=blue]
    > I'm working on an app that will be deployed on several different
    > servers. In each case, I'll want to change some config info (database
    > name, paths, etc.)
    >
    > In perl, I would have done something like this:
    >
    > Package Config;
    > <some exporting mechanics>
    > $dbname = "somename";
    > etc.
    >[/color]

    Create a python module that defines the variables you want, and then
    import it (or from it).

    You can then edit that module as a config file.

    That's one suggestion - with the security risks associated with an
    import statement...

    Alternatively, my usual reccomendation for a config file reader is
    ConfigObj... ;-)

    All the best,

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml

    Comment

    • skip@pobox.com

      #3
      Re: pythonic way of making a config module


      andrew> I'm working on an app that will be deployed on several different
      andrew> servers. In each case, I'll want to change some config info (database
      andrew> name, paths, etc.)

      andrew> In perl, I would have done something like this:
      ...
      andrew> What is a pythonic equivalent?

      Take a look at the ConfigParser module. It reads and writes Windows-style
      INI files. Also, you can do something similar to what you do in Perl. It's
      just that you import the config data from the places that need it instead of
      exporting it from the place it's defined.

      Skip

      Comment

      • Fredrik Lundh

        #4
        Re: pythonic way of making a config module

        andrew.fabbro@g mail.com wrote:
        [color=blue]
        > I'm working on an app that will be deployed on several different
        > servers. In each case, I'll want to change some config info (database
        > name, paths, etc.)
        >
        > In perl, I would have done something like this:
        >
        > Package Config;
        > <some exporting mechanics>
        > $dbname = "somename";
        > etc.
        >
        > And then use'd the module and referenced the vars as $Config::dbname .
        >
        > What is a pythonic equivalent? These vars are only changed usually at
        > install time.
        >
        > I could set up a class and then instantiate it, but that seems a
        > waste...I guess what I'm looking for is either (a) a way to suck
        > variables into a namespace keeping a prefix (without having to prefix
        > all the vars with something like config_) - e.g., reference them as
        > config.dbname or something - is this execfile?[/color]

        how about a plain

        import config

        ?

        where config.py contains

        # configuration file
        dbname = "somename"

        and is used like

        import config
        db = db_open(config. dbname)

        etc.

        </F>



        Comment

        Working...