loading configuration files that are themselves python

Discussion in 'Python' started by Bruno Desthuilliers, Jul 3, 2010.

  1. Matthew Vernon a écrit :
    > Hi,
    >
    > Is there a more idiomatic way of loading in a configuration file
    > that's python code than:
    >
    > _temp=__import__(path,fromlist='cachestrs')
    > cachestrs=_temp.cachestrs
    >
    > ? I mean, that's pretty ugly...Plain "import" doesn't work in this
    > case because 'path' is a variable defined elsewhere


    At least you have a way to do it, so you should be happy !-)

    Another solution would be to add the path to sys.path, but it wouldn't
    necessarily be the best thing to do here.
     
    Bruno Desthuilliers, Jul 3, 2010
    #1
    1. Advertising

  2. Hi,

    Is there a more idiomatic way of loading in a configuration file
    that's python code than:

    _temp=__import__(path,fromlist='cachestrs')
    cachestrs=_temp.cachestrs

    ? I mean, that's pretty ugly...Plain "import" doesn't work in this
    case because 'path' is a variable defined elsewhere

    TIA,

    Matthew

    --
    Rapun.sel - outermost outpost of the Pick Empire
    http://www.pick.ucam.org
     
    Matthew Vernon, Jul 3, 2010
    #2
    1. Advertising

  3. Bruno Desthuilliers

    Peter Otten Guest

    Matthew Vernon wrote:

    > Is there a more idiomatic way of loading in a configuration file
    > that's python code than:
    >
    > _temp=__import__(path,fromlist='cachestrs')
    > cachestrs=_temp.cachestrs
    >
    > ? I mean, that's pretty ugly...Plain "import" doesn't work in this
    > case because 'path' is a variable defined elsewhere


    execfile(path)

    in a module with a fixed name that you can import wherever you need access
    to your configuration data?

    Peter
     
    Peter Otten, Jul 3, 2010
    #3
  4. Peter Otten <> writes:

    > Matthew Vernon wrote:
    >
    > > Is there a more idiomatic way of loading in a configuration file
    > > that's python code than:
    > >
    > > _temp=__import__(path,fromlist='cachestrs')
    > > cachestrs=_temp.cachestrs
    > >
    > > ? I mean, that's pretty ugly...Plain "import" doesn't work in this
    > > case because 'path' is a variable defined elsewhere

    >
    > execfile(path)
    >
    > in a module with a fixed name that you can import wherever you need access
    > to your configuration data?


    That looks like what I want, thanks :)

    Matthew

    --
    Rapun.sel - outermost outpost of the Pick Empire
    http://www.pick.ucam.org
     
    Matthew Vernon, Jul 3, 2010
    #4
  5. Bruno Desthuilliers

    Terry Reedy Guest

    On 7/3/2010 5:15 AM, Matthew Vernon wrote:
    > Hi,
    >
    > Is there a more idiomatic way of loading in a configuration file
    > that's python code than:
    >
    > _temp=__import__(path,fromlist='cachestrs')
    > cachestrs=_temp.cachestrs
    >
    > ? I mean, that's pretty ugly...Plain "import" doesn't work in this
    > case because 'path' is a variable defined elsewhere


    cachestrs=__import__(path,fromlist='cachestrs').cachestrs
    ?

    --
    Terry Jan Reedy
     
    Terry Reedy, Jul 3, 2010
    #5
  6. In message <>, Matthew Vernon wrote:

    > Is there a more idiomatic way of loading in a configuration file
    > that's python code ...


    Is it really a good idea to have a configuration language that’s Turing-
    complete?
     
    Lawrence D'Oliveiro, Jul 24, 2010
    #6
  7. Ben Finney wrote:
    > Lawrence D'Oliveiro <_zealand> writes:
    >
    >
    >> In message <>, Matthew Vernon wrote:
    >>
    >>
    >>> Is there a more idiomatic way of loading in a configuration file
    >>> that's python code ...
    >>>

    >> Is it really a good idea to have a configuration language that’s Turing-
    >> complete?
    >>

    >
    > I think not. Configuration files should be read as data; they should be
    > declarative only, not executable languages. That way, a different
    > program can read and parse them without having to be a parser for an
    > entire programming language.
    >


    For local non distributed applications, configuration files written in
    python are just fine.

    JM
     
    Jean-Michel Pichavant, Jul 29, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Stone
    Replies:
    1
    Views:
    553
    Anne Marie
    Aug 21, 2006
  2. Paula Radetzky
    Replies:
    12
    Views:
    711
    =?ISO-8859-1?Q?G=E9rard_Talbot?=
    Feb 20, 2006
  3. tshad
    Replies:
    0
    Views:
    348
    tshad
    Jun 28, 2006
  4. WENDUM Denis 47.76.11 (agent)

    Characters contain themselves?

    WENDUM Denis 47.76.11 (agent), Apr 7, 2006, in forum: Python
    Replies:
    14
    Views:
    469
    Graham Fawcett
    Apr 11, 2006
  5. Replies:
    5
    Views:
    420
    Tom Hawtin
    Jan 17, 2007
Loading...

Share This Page