loading configuration files that are themselves python

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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.
 
M

Matthew Vernon

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
 
P

Peter Otten

Matthew said:
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
 
M

Matthew Vernon

Peter Otten said:
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
 
T

Terry Reedy

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
?
 
L

Lawrence D'Oliveiro

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?
 
J

Jean-Michel Pichavant

Ben said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top